pub fn kosaraju_scc<G>(graph: &G) -> Vec<Vec<NodeId>>Expand description
Finds all Strongly Connected Components (SCCs) using Kosaraju’s algorithm.
Kosaraju’s algorithm uses two DFS passes:
- Run DFS on the original graph, recording nodes in finish order.
- Run DFS on the transposed graph in reverse finish order — each DFS tree in this pass is one SCC.
§Returns
A Vec of SCCs, each SCC being a Vec<NodeId>. SCCs are returned in
topological order of the condensed DAG (the first SCC has no incoming
edges from other SCCs in the condensed graph).
§Complexity
O(V + E) — two DFS passes plus one graph transposition.
§Examples
use graph_core::{AdjacencyList, Graph};
use graph_advanced::kosaraju_scc;
// 0 → 1 → 0 (cycle), 1 → 2 (singleton)
let mut g: AdjacencyList<()> = AdjacencyList::directed();
let n: Vec<_> = (0..3).map(|_| g.add_node(())).collect();
g.add_edge(n[0], n[1], 1.0).unwrap();
g.add_edge(n[1], n[0], 1.0).unwrap();
g.add_edge(n[1], n[2], 1.0).unwrap();
let sccs = kosaraju_scc(&g);
assert_eq!(sccs.len(), 2);