strongly_connected_components#
- strongly_connected_components(G)[source]#
Generate nodes in strongly connected components of graph.
- Parameters:
- GNetworkX Graph
A directed graph.
- Returns:
- compgenerator of sets
A generator of sets of nodes, one for each strongly connected component of G.
- Raises:
- NetworkXNotImplemented
If G is undirected.
Notes
Iterative version of Tarjan’s algorithm using the improvements described by Tarjan and Zwick in their survey [1], plus a trick borrowed from the Rust implementation of the WebGraph framework [2].
References
[1]Robert E. Tarjan and Uri Zwick, “Finding strong components using depth-first search”, European Journal of Combinatorics, 119, 2024. https://doi.org/10.1016/j.ejc.2023.103815
[2]Tommaso Fontana, Sebastiano Vigna, and Stefano Zacchiroli, “WebGraph: The next generation (is in Rust)”, Companion Proceedings of the ACM Web Conference 2024, pp. 686-689, 2024. https://doi.org/10.1145/3589335.3651581
Examples
Generate a sorted list of strongly connected components, largest first.
>>> G = nx.cycle_graph(4, create_using=nx.DiGraph()) >>> nx.add_cycle(G, [10, 11, 12]) >>> [ ... len(c) ... for c in sorted(nx.strongly_connected_components(G), key=len, reverse=True) ... ] [4, 3]
If you only want the largest component, it’s more efficient to use max instead of sort.
>>> largest = max(nx.strongly_connected_components(G), key=len)