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)
Specify a node from which to start the depth-first search. If not provided, the algorithm will start from an arbitrary node.
- 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 inG
.
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 ofsorted()
.>>> max(nx.kosaraju_strongly_connected_components(G), key=len) {0, 1, 2, 3}