public void mergeVertices ( Set < VertexType > vertexSet , GraphType g , VertexCombinator < VertexType > combinator , GraphToolkit < GraphType , EdgeType , VertexType > toolkit ) { // Special case: if the vertex set contains a single vertex // or is empty, there is nothing to do if ( vertexSet . size ( ) <= 1 ) { return ; } // Get all vertices to which we have outgoing edges // or from which we have incoming edges, since they'll need // to be fixed TreeSet < EdgeType > edgeSet = new TreeSet <> ( ) ; for ( Iterator < EdgeType > i = g . edgeIterator ( ) ; i . hasNext ( ) ; ) { EdgeType e = i . next ( ) ; if ( vertexSet . contains ( e . getSource ( ) ) || vertexSet . contains ( e . getTarget ( ) ) ) { edgeSet . add ( e ) ; } } // Combine all of the vertices into a single composite vertex VertexType compositeVertex = combinator . combineVertices ( vertexSet ) ; // For each original edge into or out of the vertex set, // create an equivalent edge referencing the composite // vertex for ( EdgeType e : edgeSet ) { VertexType source = vertexSet . contains ( e . getSource ( ) ) ? compositeVertex : e . getSource ( ) ; VertexType target = vertexSet . contains ( e . getTarget ( ) ) ? compositeVertex : e . getTarget ( ) ; // if (source != compositeVertex && target != compositeVertex) // System.out.println("BIG OOPS!"); // Don't create a self edge for the composite vertex // unless one of the vertices in the vertex set // had a self edge if ( source == compositeVertex && target == compositeVertex && e . getSource ( ) != e . getTarget ( ) ) { continue ; } // Don't create duplicate edges. if ( g . lookupEdge ( source , target ) != null ) { continue ; } EdgeType compositeEdge = g . createEdge ( source , target ) ; // FIXME: we really should have an EdgeCombinator here toolkit . copyEdge ( e , compositeEdge ) ; } // Remove all of the vertices in the vertex set; this will // automatically remove the edges into and out of those // vertices for ( VertexType aVertexSet : vertexSet ) { g . removeVertex ( aVertexSet ) ; } }
Merge the specified set of vertices into a single vertex .