Depth-first search #
Depth-first search on a directed graph given by adjacency lists adj : V → List V, with the
finishing order of the vertices, after Tarjan and Cormen et al., Section
22.3.
Cslib.Algorithms.Lean.DFS.visit adj k u s visits u from the state s (the discovered vertices
and the finished vertices, most recently finished first): if u is undiscovered it is discovered,
its successors are visited in turn, and it is finished. The recursion budget k makes the
definition structural; Fintype.card V ≤ k suffices, because each nested call discovers a new
vertex. Cslib.Algorithms.Lean.DFS.dfs adj k roots visits the roots in turn from the empty
state. Linear-time iterative execution on the standard-basis word RAM is proved in
CslibFrontier.Algorithms.WordRAM.DFS.
Main definitions #
Cslib.Algorithms.Lean.DFS.Edge: the edge relation of adjacency lists.Cslib.Algorithms.Lean.DFS.StronglyConnected: mutual reachability inEdge adj.Cslib.Algorithms.Lean.DFS.State: discovered-set and finishing-list state of depth-first search.Cslib.Algorithms.Lean.DFS.visit,Cslib.Algorithms.Lean.DFS.dfs: depth-first search.Cslib.Algorithms.Lean.DFS.outDegreeSum: total out-degree of a list of vertices.
Main results #
Cslib.Algorithms.Lean.DFS.mem_dfs_visited_iff,Cslib.Algorithms.Lean.DFS.mem_dfs_finished_iff: depth-first search discovers and finishes exactly the vertices reachable from the roots.Cslib.Algorithms.Lean.DFS.nodup_dfs_finished: each vertex finishes at most once.Cslib.Algorithms.Lean.DFS.dfs_finished_order: for every edgeu → vout of a finished vertex, eitherufinishes aftervorvreachesu, so that the edge closes a cycle. This is the finishing-time argument in the proof of Cormen et al., Theorem 22.12, which rests on the absence of back edges in acyclic graphs (Lemma 22.11 there).Cslib.Algorithms.Lean.DFS.dfs_scc_finished_order: if a finished vertexureachesvwhilevdoes not reachu, some vertex mutually reachable withufinishes afterv(the component finishing-time property underlying Kosaraju's algorithm Cormen et al.).
References #
Two vertices lie in the same strongly connected component when each reaches the other.
Visit u with recursion budget k.
Depth-first search from each root in turn.
visit only prepends newly finished vertices onto s.finished, without reading it.
Starting with an empty finishing list, the visit lists each newly discovered vertex exactly once. This holds for every fuel and starting visited set.
A visit increases the visited cardinality by the number of newly finished vertices.
Depth-first search finds the reachable vertices.
No back edges in the finishing order, up to cycles.
Component finishing order: if a finished vertex u reaches v while v does not reach
u, then some vertex z mutually reachable with u finishes after v (appears before v in
finished).