Documentation

CslibFrontier.Algorithms.Reachability.ShortestWalk

Multi-source shortest walks by breadth-first search #

A digraph is given by a list V of vertices and a Boolean arc test arc; two Boolean tests src and tgt mark the sources and the targets. Breadth-first search from all sources at once (Cslib.Reachability.findWalk) either returns a src-to-tgt walk with the fewest vertices or returns the list of vertices reachable from the sources, which then contains every source, is closed under arcs inside V and contains no target (Cslib.Reachability.IsSrcTgtSeparator). Such a separator rules out every src-to-tgt walk, so the two outcomes are exclusive and decide whether a walk exists.

The search stores, for each vertex found, a walk from a source that reaches it. Round k adds the vertices first reached by walks with k + 2 vertices, so the first target found carries a shortest walk. The number of rounds is bounded by V.length, since each round before the last finds a new vertex of V.

Cslib.Reachability.search in CslibFrontier.Algorithms.Reachability.BreadthFirst computes the reachable set from a single start element; the search here starts from many sources, records a walk for every vertex it finds and stops at the first target.

Main definitions #

Main results #

References #

def Cslib.Reachability.IsWalk {α : Type u_1} (V : List α) (arc : α → α → Bool) (p : List α) :

p is a walk in the digraph with vertex list V and arc test arc: every vertex of p lies in V and consecutive vertices of p are joined by arcs.

def Cslib.Reachability.IsSrcTgtWalk {α : Type u_1} (V : List α) (arc : α → α → Bool) (src tgt : α → Bool) (p : List α) :

p is a nonempty walk from a vertex accepted by src to a vertex accepted by tgt. A single vertex accepted by both tests is such a walk.

def Cslib.Reachability.IsShortestSrcTgtWalk {α : Type u_1} (V : List α) (arc : α → α → Bool) (src tgt : α → Bool) (p : List α) :

p is a src-to-tgt walk with the fewest vertices.

def Cslib.Reachability.IsSrcTgtSeparator {α : Type u_1} (V : List α) (arc : α → α → Bool) (src tgt : α → Bool) (R : List α) :

R separates the sources from the targets: it is a list of vertices of V that contains every source in V, is closed under arcs into V, and contains no target.

theorem Cslib.Reachability.IsSrcTgtSeparator.not_isSrcTgtWalk {α : Type u_1} {V : List α} {arc : α → α → Bool} {src tgt : α → Bool} {R : List α} (hR : IsSrcTgtSeparator V arc src tgt R) {p : List α} :
¬IsSrcTgtWalk V arc src tgt p

A separator rules out every src-to-tgt walk.

def Cslib.Reachability.bfsInit {α : Type u_1} (V : List α) (src : α → Bool) :
List (α × List α)

The entries found before the first round: each source of V with the one-vertex walk.

def Cslib.Reachability.bfsNew {α : Type u_1} [DecidableEq α] (V : List α) (arc : α → α → Bool) (found : List (α × List α)) :
List (α × List α)

The entries added by one round: each vertex v of V not found yet that some found entry (u, w) has an arc to, with the walk w ++ [v] (for the first such entry).

def Cslib.Reachability.bfsLoop {α : Type u_1} [DecidableEq α] (V : List α) (arc : α → α → Bool) (tgt : α → Bool) (fuel : ℕ) (found : List (α × List α)) :
List α ⊕ List α

Rounds of breadth-first search, each found entry (v, w) carrying a walk w to v. A found target returns its walk; a round adding nothing, or running out of rounds, returns the vertices found.

def Cslib.Reachability.findWalk {α : Type u_1} [DecidableEq α] (V : List α) (arc : α → α → Bool) (src tgt : α → Bool) :
List α ⊕ List α

Multi-source breadth-first search: .inl p for a shortest src-to-tgt walk p, or .inr R for the list R of vertices reachable from the sources when no target is.

Correctness #

theorem Cslib.Reachability.isShortestSrcTgtWalk_of_findWalk_eq_inl {α : Type u_1} [DecidableEq α] {V : List α} {arc : α → α → Bool} {src tgt : α → Bool} {p : List α} (h : findWalk V arc src tgt = Sum.inl p) :
IsShortestSrcTgtWalk V arc src tgt p

A walk returned by findWalk is a shortest src-to-tgt walk.

theorem Cslib.Reachability.isSrcTgtSeparator_of_findWalk_eq_inr {α : Type u_1} [DecidableEq α] {V : List α} {arc : α → α → Bool} {src tgt : α → Bool} {R : List α} (h : findWalk V arc src tgt = Sum.inr R) :
IsSrcTgtSeparator V arc src tgt R

A vertex list returned by findWalk separates the sources from the targets.

theorem Cslib.Reachability.findWalk_isLeft_iff {α : Type u_1} [DecidableEq α] {V : List α} {arc : α → α → Bool} {src tgt : α → Bool} :
(findWalk V arc src tgt).isLeft = true ↔ ∃ (p : List α), IsSrcTgtWalk V arc src tgt p

findWalk returns a walk exactly when some src-to-tgt walk exists.