Collections.sort( currentNodes, sorter );
DNVNode tempNode;
DNVNode tempNode2;
DNVNode newNode;
DNVEdge tempEdge;
System.out.println( "First node neighbors : " + currentNodes.get( 0 ).getNeighbors().size() );
System.out.println( "Last node neighbors : " + currentNodes.get( currentNodes.size() - 1 ).getNeighbors().size() );
Map<Integer, Integer> nodeDistance = new HashMap<Integer, Integer>();
Map<Integer, DNVNode> nodeToParent = new HashMap<Integer, DNVNode>();
Map<Integer, List<DNVNode>> parentToNodes = new HashMap<Integer, List<DNVNode>>();
List<DNVNode> tempList;
// Assign all nodes with a parent node
for( int i = 0; i < numberOfClusters; i++ )
{
tempNode = currentNodes.get( i );
newNode = createParentNode( tempNode, newLevel, graph, nodeDistance, nodeToParent, parentToNodes );
if( Settings.DEBUG )
{
System.out.println( "Parent node has id " + newNode.getId() + " subnode has id " + tempNode.getId() );
System.out.println( "Subnode[" + tempNode.getId() + "] has " + tempNode.getNeighbors().size() + " neighbors" );
}
tempNodeList = tempNode.getNeighbors();
handleNeighbors( 1, tempNodeList, newNode, nodeDistance, nodeToParent );
}
// Handle nodes that are not connected to any other nodes
tempList = graph.getNodes( currentLevel );
for( int i = 0; i < tempList.size(); i++ )
{
tempNode = tempList.get( i );
if( tempNode.getNeighbors().size() == 0 )
{
newNode = createParentNode( tempNode, newLevel, graph, nodeDistance, nodeToParent, parentToNodes );
if( Settings.DEBUG )
{
System.out.println( "Node with id " + tempNode.getId() + " at level " + currentLevel + " has no neighbors. Create parent node." );
System.out.println( "Parent node has id " + newNode.getId() );
System.out.println( "" );
}
}
}
if( Settings.DEBUG )
{
System.out.println( "Number of nodes that have a parent node: " + nodeToParent.keySet().size() );
System.out.println( "Number of nodes at current level: " + currentNodes.size() );
for( int i = 0; i < currentNodes.size(); i++ )
{
if( nodeToParent.get( currentNodes.get( i ).getId() ) == null )
{
System.out.println( "node " + currentNodes.get( i ).getId() + " has no parent." );
}
}
}
// Set connections from parent to sub nodes
Iterator<Integer> ids = nodeToParent.keySet().iterator();
Integer tempId;
while( ids.hasNext() )
{
tempId = ids.next();
tempNode = nodeToParent.get( tempId );
tempList = parentToNodes.get( tempNode.getId() );
tempNode2 = (DNVNode)graph.getNodeById( tempId );
if( tempNode2 != null && !tempList.contains( tempNode2 ) )
{
tempList.add( tempNode2 );
parentToNodes.put( tempNode.getId(), tempList );
tempNode.setLabel( "" + tempList.size() );
}
if( tempNode2 == null )
{
System.out.println( "Trying to add a link from a non-existing node" );
}
}
// Create the edges
List<DNVEdge> edgeList;
ids = parentToNodes.keySet().iterator();
DNVNode tempParent;
DNVNode tempParent2;
while( ids.hasNext() )
{
tempId = ids.next();
tempParent2 = (DNVNode)graph.getNodeById( tempId );
tempList = parentToNodes.get( tempId );
tempParent2.setSubNodes( tempList );
for( int i = 0; i < tempList.size(); i++ )
{
tempNode = tempList.get( i );
edgeList = tempNode.getFromEdges();
for( int j = 0; j < edgeList.size(); j++ )
{
tempEdge = edgeList.get( j );
tempNode2 = tempEdge.getTo();
tempParent = nodeToParent.get( tempNode2.getId() );
if( tempParent != null && !tempParent.getId().equals( tempId ) && tempParent != tempParent2 )
{
if( !tempParent.getNeighbors().contains( tempParent2 ) )
{
tempEdge = new DNVEdge( newLevel.intValue(), DNVEdge.DEFAULT_RESTING_DISTANCE, false, tempParent2, tempParent, graph );
tempParent2.addFromEdge( tempEdge );
tempParent.addToEdge( tempEdge );
graph.addNode( newLevel, tempEdge );
}
}
}
edgeList = tempNode.getToEdges();
for( int j = 0; j < edgeList.size(); j++ )
{
tempEdge = edgeList.get( j );
tempNode2 = tempEdge.getFrom();
tempParent = nodeToParent.get( tempNode2.getId() );
if( tempParent != null && !tempParent.getId().equals( tempId ) && tempParent != tempParent2 )
{
tempParent2 = (DNVNode)graph.getNodeById( tempId );
if( !tempParent.getNeighbors().contains( tempParent2 ) )
{
tempEdge = new DNVEdge( newLevel.intValue(), DNVEdge.DEFAULT_RESTING_DISTANCE, false, tempParent, tempParent2, graph );
tempParent2.addToEdge( tempEdge );
tempParent.addFromEdge( tempEdge );
graph.addNode( newLevel, tempEdge );
}
}
}
}
}
// float constant = 0.1f;
for( int i = 0; i < currentNodes.size(); i++ )
{
tempNode = currentNodes.get( i );
edgeList = tempNode.getFromEdges();
for( int j = 0; j < edgeList.size(); j++ )
{
tempEdge = edgeList.get( j );
tempNode2 = tempEdge.getTo();
// tempEdge.setRestingDistance( DNVEdge.DEFAULT_RESTING_DISTANCE
// + constant * ( tempNode.getTotalNumberOfSubNodes() +
// tempNode2.getTotalNumberOfSubNodes() ) );
}
edgeList = tempNode.getToEdges();
for( int j = 0; j < edgeList.size(); j++ )
{
tempEdge = edgeList.get( j );
tempNode2 = tempEdge.getFrom();
// tempEdge.setRestingDistance( DNVEdge.DEFAULT_RESTING_DISTANCE
// + constant * ( tempNode.getTotalNumberOfSubNodes() +
// tempNode2.getTotalNumberOfSubNodes() ) );
}
}