pub fn tarjan_scc<G>(graph: &G) -> Vec<Vec<NodeId>>Expand description
Finds all Strongly Connected Components (SCCs) using Tarjan’s algorithm.
A strongly connected component is a maximal set of nodes where every node is reachable from every other node via directed edges.
§Algorithm
A single DFS pass assigns each node a discovery time (disc) and a
low-link value (low[u]): the smallest discovery time reachable from
the subtree rooted at u via tree edges and at most one back-edge.
Nodes are pushed onto a stack as they are discovered. When we finish
processing node u and find low[u] == disc[u], u is the root of an
SCC: we pop the stack until we reach u, and all popped nodes form one SCC.
§Returns
A Vec of SCCs, each SCC being a Vec<NodeId>. Components are returned
in reverse topological order of the condensed DAG: if there is an edge
from SCC A to SCC B, then B appears before A in the result.
§Complexity
O(V + E) — a single DFS pass over all nodes and edges.
§Examples
use graph_core::{AdjacencyList, Graph};
use graph_advanced::tarjan_scc;
// Two-node cycle: {0, 1} form one SCC; {2} is its own.
// 0 → 1 → 0
// 1 → 2
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 = tarjan_scc(&g);
assert_eq!(sccs.len(), 2);