Documentation

CslibFrontier.Algorithms.Lean.DFS.TopologicalSort

Topological sort #

Topological sort of a directed graph given by adjacency lists: the vertices in reverse order of finishing time under the depth-first search of CslibFrontier.Algorithms.Lean.DFS.Basic, following Cormen et al., Section 22.4. Linear-time iterative execution on the standard-basis word RAM is proved in CslibFrontier.Algorithms.WordRAM.DFS.

Main definitions #

Main results #

References #

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

A topological order of the graph: a list of distinct vertices containing every vertex, in which the source of each edge comes before its target.

  • nodup : l.Nodup

    No vertex is repeated.

  • mem (v : V) : v ∈ l

    Every vertex occurs.

  • sublist (u v : V) : Edge adj u v → [u, v].Sublist l

    The source of every edge comes before its target.

theorem Cslib.Algorithms.Lean.DFS.IsTopologicalOrder.acyclic {V : Type u_1} {adj : V → List V} {l : List V} (hl : IsTopologicalOrder adj l) :

A graph with a topological order is acyclic: positions in the order strictly increase along every edge.

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

Topological sort: the vertices in reverse order of finishing time under depth-first search from the vertices vs in turn.

theorem Cslib.Algorithms.Lean.DFS.card_le_length {V : Type u_1} [Fintype V] {vs : List V} (hvs : ∀ (v : V), v ∈ vs) :
theorem Cslib.Algorithms.Lean.DFS.nodup_topologicalSort {V : Type u_1} {adj : V → List V} [DecidableEq V] {vs : List V} (hvs : ∀ (v : V), v ∈ vs) :

From a list of all the vertices, topological sort lists each vertex at most once.

theorem Cslib.Algorithms.Lean.DFS.mem_topologicalSort {V : Type u_1} {adj : V → List V} [DecidableEq V] {vs : List V} (hvs : ∀ (v : V), v ∈ vs) (v : V) :

From a list of all the vertices, topological sort lists every vertex.

theorem Cslib.Algorithms.Lean.DFS.isTopologicalOrder_topologicalSort_iff {V : Type u_1} {adj : V → List V} [DecidableEq V] {vs : List V} (hvs : ∀ (v : V), v ∈ vs) :

Topological sort is correct exactly on acyclic graphs Cormen et al., Theorem 22.12: from a list vs of all the vertices, topologicalSort adj vs is a topological order if and only if the graph is acyclic.

A finite graph is acyclic if and only if it has a topological order.

Topological sort of List.finRange N has length N.