def kosaraju_strongly_connected_components(G,source=None): """Return nodes in strongly connected components of graph. Parameters ---------- G : NetworkX Graph An directed graph. Returns ------- comp : list of lists A list of nodes for each component of G. The list is ordered from largest connected component to smallest. See Also -------- connected_components Notes ----- Uses Kosaraju's algorithm. """ components=[] post=nx.dfs_postorder(G,source=source,reverse_graph=True) seen={} while post: r=post.pop() if r in seen: continue c=nx.dfs_preorder(G,r) new=[v for v in c if v not in seen] seen.update([(u,True) for u in new]) components.append(new) components.sort(key=len,reverse=True) return components
def restrict_to_target(self, target): revgraph = self.graph.reverse() nodes = nx.dfs_postorder(revgraph, target) new_graph = self.graph.subgraph(nodes) new_pipeline = Pipeline() new_pipeline.graph = new_graph new_pipeline.env = self.env return new_pipeline
def test_postorder(self): assert_equal(nx.dfs_postorder(self.G,source=0),[3, 4, 2, 1, 0])
def test_postorder(self): assert_equal(nx.dfs_postorder(self.G, source=0), [3, 4, 2, 1, 0])