kosaraju_strongly_connected_components#
- kosaraju_strongly_connected_components(G, source=None)[source]#
Generate nodes in strongly connected components of graph.
- Parameters:
- GNetworkX Graph
A directed graph.
- sourcenode, optional (default=None)
If provided, only components that can reach source are yielded.
- Yields:
- set
A set of all nodes in a strongly connected component of G.
- Raises:
- NetworkXNotImplemented
If G is undirected.
- NetworkXError
If source is not a node in G.
See also
Notes
Uses Kosaraju’s algorithm.
Examples
Generate a list of strongly connected components of a graph:
>>> G = nx.cycle_graph(4, create_using=nx.DiGraph()) >>> nx.add_cycle(G, [10, 11, 12]) >>> sorted(nx.kosaraju_strongly_connected_components(G), key=len, reverse=True) [{0, 1, 2, 3}, {10, 11, 12}]
If you only want the largest component, it’s more efficient to use max() instead of sorted().
>>> max(nx.kosaraju_strongly_connected_components(G), key=len) {0, 1, 2, 3}