public GraphType transpose ( GraphType orig , GraphToolkit < GraphType , EdgeType , VertexType > toolkit ) { GraphType trans = toolkit . createGraph ( ) ; // For each vertex in original graph, create an equivalent // vertex in the transposed graph, // ensuring that vertex labels in the transposed graph // match vertex labels in the original graph for ( Iterator < VertexType > i = orig . vertexIterator ( ) ; i . hasNext ( ) ; ) { VertexType v = i . next ( ) ; // Make a duplicate of original vertex // (Ensuring that transposed graph has same labeling as original) VertexType dupVertex = toolkit . duplicateVertex ( v ) ; dupVertex . setLabel ( v . getLabel ( ) ) ; trans . addVertex ( v ) ; // Keep track of correspondence between equivalent vertices m_origToTransposeMap . put ( v , dupVertex ) ; m_transposeToOrigMap . put ( dupVertex , v ) ; } trans . setNumVertexLabels ( orig . getNumVertexLabels ( ) ) ; // For each edge in the original graph, create a reversed edge // in the transposed graph for ( Iterator < EdgeType > i = orig . edgeIterator ( ) ; i . hasNext ( ) ; ) { EdgeType e = i . next ( ) ; VertexType transSource = m_origToTransposeMap . get ( e . getTarget ( ) ) ; VertexType transTarget = m_origToTransposeMap . get ( e . getSource ( ) ) ; EdgeType dupEdge = trans . createEdge ( transSource , transTarget ) ; dupEdge . setLabel ( e . getLabel ( ) ) ; // Copy auxiliary information for edge toolkit . copyEdge ( e , dupEdge ) ; } trans . setNumEdgeLabels ( orig . getNumEdgeLabels ( ) ) ; return trans ; }
Transpose a graph . Note that the original graph is not modified ; the new graph and its vertices and edges are new objects .