Documentation

CslibFrontier.Algorithms.WordRAM.DFS.Programs

End-to-end Word-RAM topological sort and Kosaraju SCC programs #

Main definitions #

Main results #

Forward DFS, transpose and Kosaraju peeling as one pass (O(N + M)) #

Kosaraju's strongly connected components on the CSR graph at kOffAddr wb: forward DFS finishing order, CSR transpose, and peeling the transpose in reverse finishing order, writing the topological component index of every vertex to kCompAddr wb N Mc.

theorem Cslib.Algorithms.WordRAM.DFS.sccIndexCode_spec {N w : ℕ} (hN : 0 < N) (adj : Fin N → List (Fin N)) (wb Mc : ℕ) (hmc : csrOffset adj N ≤ Mc) (hfit : WordRAM.Fits w (kTopAddr wb N Mc + Mc + 4)) {x : WordRAM.Data 28} (hregs : DfsRegs (kOffAddr wb) (kEdgeAddr wb N) (kVisAddr wb N Mc) (kOrdAddr wb N Mc) (kStkAddr wb N Mc) x) (hNv : x.regs Reg.nVerts = N) (hcur : x.regs Reg.curB = kCurAddr wb N Mc) (hroff : x.regs Reg.revOffB = kRevOffAddr wb N Mc) (hredge : x.regs Reg.revEdgeB = kRevEdgeAddr wb N Mc) (hordB : x.regs Reg.ordB = kOrdAddr wb N Mc) (hcompB : x.regs Reg.compB = kCompAddr wb N Mc) (hcsr : CSRAt adj (kOffAddr wb) (kEdgeAddr wb N) x.mem) :
∃ (y : WordRAM.Data 28), WordRAM.ExecWithin w sccIndexCode x y (220 * N + 92 * Mc + 42) ∧ (∀ (u : Fin N), y.mem (kCompAddr wb N Mc + ↑u) = Lean.DFS.indexComponents (Lean.DFS.sccs adj (List.finRange N)) u) ∧ (∀ a < wb, y.mem a = x.mem a) ∧ ∀ (r : Fin 28), ↑r < 18 → r ≠ Reg.offB → r ≠ Reg.edgeB → r ≠ Reg.finB → y.regs r = x.regs r

Kosaraju's algorithm on the word RAM in O(N + M) instructions. From the scratch layout based at wb (with a CSR graph adj of at most Mc arcs at kOffAddr wb), sccIndexCode writes indexComponents (sccs adj (List.finRange N)) u to kCompAddr wb N Mc + u for every vertex u, leaving memory below wb unchanged.

Graphs in CSR form #

A directed multigraph on Fin n given by successor lists; parallel arcs and self-loops are allowed.

  • n : ℕ

    Number of vertices.

  • adj : Fin self.n → List (Fin self.n)

    Successor lists.

Number of arcs ∑ u, (G.adj u).length.

CSR (offset-array) input encoding [n, m, off 0, …, off n] ++ arcs, where off u = csrOffset G.adj u counts the arcs leaving vertices below u and arcs lists the successors of 0, …, n - 1 in successor-list order.

Linear word-size envelope 16 * (n + m + 1) covering the input and the scratch layout.

The CSR input encoding installs G.adj as a CSR graph at kOffAddr 3.

Every word of G.encodeCSR, and its length, fits any word width admitting G.wordBound.

End-to-end programs #

Topological-sort output: topologicalSort G.adj (List.finRange G.n) (the vertices in reverse DFS finishing order) as vertex numbers.

Strongly-connected-component output: the topological component index indexComponents (sccs G.adj (List.finRange G.n)) v of each vertex v = 0, …, n - 1.

Structured topological sort: setup, forward DFS, and reversed copy of the finishing order.

Structured strongly-connected-component indexing: setup, then sccIndexCode.

Flat topological-sort program returning topoWords G via ExecutesList.

Flat strongly-connected-component program returning sccWords G via ExecutesList.

Linear-time topological sort on the word RAM. From the CSR input G.encodeCSR, topoCode leaves topoWords G (the reverse DFS finishing order, which by Cslib.Algorithms.Lean.DFS.isTopologicalOrder_topologicalSort_iff is a topological order exactly when G is acyclic) at the address in outReg, within 128 * (G.n + G.m + 1) - 1 instructions at every word width w with Fits w G.wordBound.

Linear-time Kosaraju strongly connected components on the word RAM. From the CSR input G.encodeCSR, sccCode leaves sccWords G (each vertex's topological component index in Cslib.Algorithms.Lean.DFS.sccs; by Cslib.Algorithms.Lean.DFS.IsSCCCondensation.indexComponents_eq_iff two vertices receive the same index exactly when they are strongly connected) at the address in outReg, within 256 * (G.n + G.m + 1) - 1 instructions at every word width w with Fits w G.wordBound.

theorem Cslib.Algorithms.WordRAM.DFS.computesListInTime_topoProgram :
topoProgram.ComputesListInTime (fun (x : ℕ) (G : Graph) => G.encodeCSR) (fun (G : Graph) (x : ℕ) => 128 * (G.n + G.m + 1)) (fun (G : Graph) (w : ℕ) => WordRAM.Fits w G.wordBound) topoWords

topoProgram computes topoWords in O(n + m) instructions (Program.ComputesListInTime).

theorem Cslib.Algorithms.WordRAM.DFS.computesListInTime_sccProgram :
sccProgram.ComputesListInTime (fun (x : ℕ) (G : Graph) => G.encodeCSR) (fun (G : Graph) (x : ℕ) => 256 * (G.n + G.m + 1)) (fun (G : Graph) (w : ℕ) => WordRAM.Fits w G.wordBound) sccWords

sccProgram computes sccWords in O(n + m) instructions (Program.ComputesListInTime).