Documentation

CslibFrontier.Algorithms.Lean.DFS.SCC

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:

  1. computes the finishing order ord = topologicalSort adj vs of depth-first search on adj;
  2. builds the transpose adjacency lists transposeAdj adj vs by scanning vs and adj u;
  3. traverses ord in 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 #

Main results #

References #

Condensation specification #

structure Cslib.Algorithms.Lean.DFS.IsSCCCondensation {V : Type u_1} (adj : V → List V) (comps : List (List V)) :

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.

  • ne_nil (c : List V) : c ∈ comps → c ≠ []

    Every component is nonempty.

  • nodup_flatten : comps.flatten.Nodup

    Across all components, no vertex appears twice.

  • mem_flatten (v : V) : v ∈ comps.flatten

    Every vertex appears in some component.

  • same_comp_iff (u v : V) : (∃ c ∈ comps, u ∈ c ∧ v ∈ c) ↔ StronglyConnected adj u v

    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 → v goes from component i to component j with i ≤ j.

Transpose adjacency lists #

def Cslib.Algorithms.Lean.DFS.transposeAdj {V : Type u_1} [DecidableEq V] (adj : V → List V) (vs : List V) :
V → List V

Reverse every adjacency-list arc emanating from vs.

theorem Cslib.Algorithms.Lean.DFS.mem_transposeAdj {V : Type u_1} [DecidableEq V] {adj : V → List V} {vs : List V} {w x : V} :
x ∈ transposeAdj adj vs w ↔ x ∈ vs ∧ w ∈ adj x
theorem Cslib.Algorithms.Lean.DFS.edge_transposeAdj_iff {V : Type u_1} [DecidableEq V] {adj : V → List V} {vs : List V} (hvs : ∀ (v : V), v ∈ vs) {u v : V} :
Edge (transposeAdj adj vs) u v ↔ Edge adj v u
theorem Cslib.Algorithms.Lean.DFS.reflTransGen_edge_transposeAdj_iff {V : Type u_1} [DecidableEq V] {adj : V → List V} {vs : List V} (hvs : ∀ (v : V), v ∈ vs) {u v : V} :
theorem Cslib.Algorithms.Lean.DFS.sum_length_update_cons {α : Type u_2} {β : Type u_3} [DecidableEq α] [Fintype α] (f : α → List β) (u : α) (v : β) :
∑ w : α, (Function.update f u (v :: f u) w).length = ∑ w : α, (f w).length + 1

Consing onto one adjacency-list entry increases the total adjacency-list length by 1.

theorem Cslib.Algorithms.Lean.DFS.sum_length_transposeAdj {V : Type u_1} [DecidableEq V] [Fintype V] (adj : V → List V) (vs : List V) :
∑ w : V, (transposeAdj adj vs w).length = outDegreeSum adj vs
theorem Cslib.Algorithms.Lean.DFS.outDegreeSum_append {V : Type u_1} (g : V → List V) (a b : List V) :

outDegreeSum is additive over list concatenation.

Kosaraju's second pass #

def Cslib.Algorithms.Lean.DFS.peelSCCs {V : Type u_1} [DecidableEq V] (revAdj : V → List V) (k : ℕ) :
Finset V → List V → List (List V)

Peel strongly connected components in revAdj along finishing order rs.

def Cslib.Algorithms.Lean.DFS.sccs {V : Type u_1} [DecidableEq V] (adj : V → List V) (vs : List V) :

Kosaraju's algorithm: compute strongly connected components in condensation topological order from a vertex list vs.

theorem Cslib.Algorithms.Lean.DFS.isSCCCondensation_sccs {V : Type u_1} [DecidableEq V] {adj : V → List V} {vs : List V} (hvs : ∀ (v : V), v ∈ vs) :
IsSCCCondensation adj (sccs adj 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 #

def Cslib.Algorithms.Lean.DFS.indexComponentsAux {V : Type u_1} [DecidableEq V] :
ℕ → List (List V) → (V → ℕ) → V → ℕ

Auxiliary pass assigning each vertex in c its component index idx.

Map every vertex to the topological index of its component in comps.

theorem Cslib.Algorithms.Lean.DFS.foldl_update_const {V : Type u_1} [DecidableEq V] (idx : ℕ) (c : List V) (f : V → ℕ) (u : V) :
List.foldl (fun (f : V → ℕ) (v : V) => Function.update f v idx) f c u = if u ∈ c then idx else f u

Folding constant updates over c sets exactly the members of c to idx.

theorem Cslib.Algorithms.Lean.DFS.indexComponents_eq_of_mem {V : Type u_1} [DecidableEq V] {comps : List (List V)} (hnd : comps.flatten.Nodup) {i : ℕ} (hi : i < comps.length) {u : V} (hu : u ∈ comps[i]) :
indexComponents comps u = i
theorem Cslib.Algorithms.Lean.DFS.IsSCCCondensation.length_le_card {V : Type u_1} {adj : V → List V} [Fintype V] {comps : List (List V)} (h : IsSCCCondensation adj comps) :
theorem Cslib.Algorithms.Lean.DFS.IsSCCCondensation.indexComponents_lt {V : Type u_1} [DecidableEq V] {adj : V → List V} {comps : List (List V)} (h : IsSCCCondensation adj comps) (v : V) :
indexComponents comps v < comps.length
theorem Cslib.Algorithms.Lean.DFS.IsSCCCondensation.mem_getElem_indexComponents {V : Type u_1} [DecidableEq V] {adj : V → List V} {comps : List (List V)} (h : IsSCCCondensation adj comps) (v : V) :
v ∈ comps[indexComponents comps v]
theorem Cslib.Algorithms.Lean.DFS.IsSCCCondensation.indexComponents_eq_iff {V : Type u_1} [DecidableEq V] {adj : V → List V} {comps : List (List V)} (h : IsSCCCondensation adj comps) (u v : V) :
theorem Cslib.Algorithms.Lean.DFS.IsSCCCondensation.indexComponents_le_of_edge {V : Type u_1} [DecidableEq V] {adj : V → List V} {comps : List (List V)} (h : IsSCCCondensation adj comps) {u v : V} (huv : Edge adj u v) :
theorem Cslib.Algorithms.Lean.DFS.IsSCCCondensation.indexComponents_le_of_reach {V : Type u_1} [DecidableEq V] {adj : V → List V} {comps : List (List V)} (h : IsSCCCondensation adj comps) {u v : V} (huv : Relation.ReflTransGen (Edge adj) u v) :
theorem Cslib.Algorithms.Lean.DFS.IsSCCCondensation.indexComponents_lt_of_edge {V : Type u_1} [DecidableEq V] {adj : V → List V} {comps : List (List V)} (h : IsSCCCondensation adj comps) {u v : V} (huv : Edge adj u v) (hn : ¬StronglyConnected adj u v) :