Strongly connected components #
Kosaraju's algorithm for the strongly connected components of a directed graph given by adjacency
lists adj : V → List V, together with their condensation topological order, following
Cormen et al. (see also Tarjan). Linear-time iterative execution on the
standard-basis word RAM is proved in CslibFrontier.Algorithms.WordRAM.DFS.
From a vertex list vs covering V, Cslib.Algorithms.Lean.DFS.sccs adj vs:
- computes the finishing order
ord = topologicalSort adj vsof depth-first search onadj; - builds the transpose adjacency lists
transposeAdj adj vsby scanningvsandadj u; - traverses
ordin the transpose graph (peelSCCs), emitting each newly reached tree as one strongly connected component.
Because the component finishing lemma (dfs_scc_finished_order) places at least one vertex of
every upstream component before every vertex of a downstream component in ord, each root peeled
in transposeAdj adj vs sees no unvisited upstream vertices and visits its exact strongly
connected component. Moreover, emitted components appear in forward topological order of the
condensation DAG: every edge u → v goes from a component of index i to a component of index
j with i ≤ j.
Main definitions #
Cslib.Algorithms.Lean.DFS.IsSCCCondensation: specification of a duplicate-free partition ofVinto nonemptyStronglyConnectedclasses ordered topologically.Cslib.Algorithms.Lean.DFS.transposeAdj: transpose adjacency lists.Cslib.Algorithms.Lean.DFS.peelSCCs,Cslib.Algorithms.Lean.DFS.sccs: Kosaraju's second pass and two-pass SCC decomposition.Cslib.Algorithms.Lean.DFS.indexComponentsAux,Cslib.Algorithms.Lean.DFS.indexComponents: map each vertex to its component's topological index.
Main results #
Cslib.Algorithms.Lean.DFS.sum_length_transposeAdj: total length of transpose adjacency lists equalsoutDegreeSum adj vs.Cslib.Algorithms.Lean.DFS.isSCCCondensation_sccs: whenvslists every vertex,sccs adj vssatisfiesIsSCCCondensation adj.Cslib.Algorithms.Lean.DFS.IsSCCCondensation.indexComponents_eq_iff,Cslib.Algorithms.Lean.DFS.IsSCCCondensation.indexComponents_le_of_edge: component-index equivalence and condensation monotonicity.
References #
Condensation specification #
A topological decomposition of (V, adj) into strongly connected components: nonempty lists
partitioning V without duplicates, each an equivalence class of StronglyConnected adj, ordered
so every edge u → v has u's component index at most v's.
Every component is nonempty.
Across all components, no vertex appears twice.
Every vertex appears in some component.
Two vertices share a component if and only if they are mutually reachable.
- condensation_le {i j : ℕ} (hi : i < comps.length) (hj : j < comps.length) {u v : V} : u ∈ comps[i] → v ∈ comps[j] → Edge adj u v → i ≤ j
Every edge
u → vgoes from componentito componentjwithi ≤ j.
Transpose adjacency lists #
Reverse every adjacency-list arc emanating from vs.
Consing onto one adjacency-list entry increases the total adjacency-list length by 1.
outDegreeSum is additive over list concatenation.
Kosaraju's second pass #
Peel strongly connected components in revAdj along finishing order rs.
Kosaraju's algorithm: compute strongly connected components in condensation topological
order from a vertex list vs.
Correctness of Kosaraju's algorithm Cormen et al.: when vs lists every
vertex of V, sccs adj vs partitions V into nonempty strongly connected components in forward
condensation topological order.
Component-index table #
Auxiliary pass assigning each vertex in c its component index idx.
Map every vertex to the topological index of its component in comps.
Folding constant updates over c sets exactly the members of c to idx.