Documentation

CslibFrontier.Algorithms.Reachability.SavitchWalk

Following a walk by Savitch's midpoint recursion #

Savitch's midpoint recursion (Cslib.Reachability.reach) decides whether v is reachable from u within 2 ^ d steps between words of width w. This file turns the recursion into a walk that visits the vertices of such a path in order, one per step, holding only a stack of pending targets with their levels.

A state of the walk (Cslib.Reachability.Walk.State) is the current vertex and the stack. A target at level 0 is one step away: the walk moves to it and pops it. A target t at level i + 1 is split at the midpoint m of the current vertex and t (Cslib.Reachability.Walk.midpoint): the walk replaces it by m and t, both at level i. The midpoint is the first word of width w, in the order of the binary counter, that the current vertex reaches and that reaches t at level i (Cslib.Reachability.Walk.search); when there is none, it is the word of zeros.

The midpoint is always a word of width w, so the shape of the run does not depend on the edges: a target at level d is reached in exactly 2 ^ (d + 1) - 1 steps (Cslib.Reachability.Walk.iterate_step_frame), a whole stack in Cslib.Reachability.Walk.stackSteps steps (Cslib.Reachability.Walk.iterate_step_stack), every word on the way has width w (Cslib.Reachability.Walk.width_iterate), and a run from a target at level d holds at most d + 1 targets, at levels at most d (Cslib.Reachability.Walk.levelsLE_iterate). When v is reachable from u within 2 ^ d steps, each step of the run from u to v stays put or follows an edge (Cslib.Reachability.Walk.eq_or_edge_iterate), because each target remains reachable from the previous one at its level (Cslib.Reachability.Walk.chain_iterate).

The space of a step on multi-tape machines is in Cslib.Computability.Machines.Turing.MultiTape.Complexity.Space.SavitchWalk.

Main definitions #

Main results #

References #

Search in counter order #

One round of a search through the words of a width in counter order: keep a word that passes test, and otherwise move to the next word of the binary counter.

Search in counter order. From the word of w zeros, 2 ^ w rounds of searchStep stop at the first word of width w, in the order of the binary counter, that passes test; when there is none, the counter wraps around to the word of zeros.

@[simp]

A round of the search keeps the width.

@[simp]

Rounds of the search keep the width.

theorem Cslib.Reachability.Walk.search_eq {test : List Bool → Bool} {w k : ℕ} (hk : k < 2 ^ w) (htest : test (BitString.increment^[k] (List.replicate w false)) = true) (hmin : ∀ j < k, test (BitString.increment^[j] (List.replicate w false)) = false) :

The search finds the first word that passes test: if the k-th word of width w in counter order passes it and no earlier word does, the search returns it.

theorem Cslib.Reachability.Walk.search_congr {test test' : List Bool → Bool} {w : ℕ} (h : ∀ (m : List Bool), m.length = w → test m = test' m) :
search test w = search test' w

The search depends only on the words of width w: tests that agree on them give the same search, since every round keeps the width.

theorem Cslib.Reachability.Walk.search_of_forall {test : List Bool → Bool} {w : ℕ} (h : ∀ (m : List Bool), m.length = w → test m = false) :

Without a word that passes test, the search returns the word of zeros: the counter wraps around after 2 ^ w rounds.

The midpoint of a call #

def Cslib.Reachability.Walk.midpoint (edge : List Bool → List Bool → Bool) (w i : ℕ) (u v : List Bool) :

The midpoint of a call from u to v at level i + 1: the first word of width w, in counter order, that u reaches and that reaches v at level i (search). It is the word of zeros when there is no such word, and when u or v does not have width w.

@[simp]
theorem Cslib.Reachability.Walk.length_midpoint (edge : List Bool → List Bool → Bool) (w i : ℕ) (u v : List Bool) :
(midpoint edge w i u v).length = w

The midpoint has width w.

theorem Cslib.Reachability.Walk.reach_midpoint {edge : List Bool → List Bool → Bool} {w i : ℕ} {u v : List Bool} (hu : u.length = w) (hv : v.length = w) (h : reach edge w (i + 1) u v = true) :
reach edge w i u (midpoint edge w i u v) = true ∧ reach edge w i (midpoint edge w i u v) v = true

The midpoint splits a call: if v is reachable from u at level i + 1, the midpoint is reachable from u, and reaches v, at level i.

theorem Cslib.Reachability.Walk.midpoint_of_reach_eq_false {edge : List Bool → List Bool → Bool} {w i : ℕ} {u v : List Bool} (h : reach edge w (i + 1) u v = false) :

A failed call has the word of zeros as midpoint.

The walk #

@[reducible, inline]

A state of the walk: the current vertex, and the stack of pending targets with their levels, the next target first. A target at level i is reachable within 2 ^ i steps from the vertex or target before it.

One step of the walk, with the edge test edge and words of width w.

  • An empty stack is a fixed point: the walk has arrived.
  • A target at level 0 is one step away: move to it and pop it.
  • A target t at level i + 1 is replaced by the midpoint of the current vertex and t, followed by t, both at level i.
theorem Cslib.Reachability.Walk.step_nil (edge : List Bool → List Bool → Bool) (w : ℕ) (cur : List Bool) :
step edge w (cur, []) = (cur, [])

An empty stack is a fixed point of a step.

theorem Cslib.Reachability.Walk.step_zero (edge : List Bool → List Bool → Bool) (w : ℕ) (cur t : List Bool) (rest : List (List Bool × ℕ)) :
step edge w (cur, (t, 0) :: rest) = (t, rest)

A target at level 0 is reached in one step, and popped.

theorem Cslib.Reachability.Walk.step_succ (edge : List Bool → List Bool → Bool) (w i : ℕ) (cur t : List Bool) (rest : List (List Bool × ℕ)) :
step edge w (cur, (t, i + 1) :: rest) = (cur, (midpoint edge w i cur t, i) :: (t, i) :: rest)

A target at level i + 1 is split at the midpoint of the current vertex and the target.

theorem Cslib.Reachability.Walk.iterate_step_nil (edge : List Bool → List Bool → Bool) (w : ℕ) (cur : List Bool) (n : ℕ) :
(step edge w)^[n] (cur, []) = (cur, [])

An empty stack is a fixed point of the walk.

The number of steps that clear a stack: 2 ^ (ℓ + 1) - 1 for each target at level ℓ.

theorem Cslib.Reachability.Walk.iterate_step_frame (edge : List Bool → List Bool → Bool) (w d : ℕ) (cur t : List Bool) (rest : List (List Bool × ℕ)) :
(step edge w)^[2 ^ (d + 1) - 1] (cur, (t, d) :: rest) = (t, rest)

A target at level d is reached in 2 ^ (d + 1) - 1 steps, whatever the edges: the walk then stands on the target, with the rest of the stack untouched.

theorem Cslib.Reachability.Walk.iterate_step_stack (edge : List Bool → List Bool → Bool) (w : ℕ) (cur : List Bool) (stack : List (List Bool × ℕ)) :
(step edge w)^[stackSteps stack] (cur, stack) = ((List.map Prod.fst stack).getLastD cur, [])

A stack is cleared in stackSteps stack steps, whatever the edges: the walk then stands on the last target, or where it started when the stack is empty.

theorem Cslib.Reachability.Walk.iterate_step_run (edge : List Bool → List Bool → Bool) (w d : ℕ) (u v : List Bool) :
(step edge w)^[2 ^ (d + 1) - 1] (u, [(v, d)]) = (v, [])

The run from u to a target v at level d arrives at v with an empty stack after 2 ^ (d + 1) - 1 steps, whatever the edges.

The shape of the states #

The words of a state have width w: the current vertex and every pending target.

theorem Cslib.Reachability.Walk.width_step (edge : List Bool → List Bool → Bool) {w : ℕ} {s : State} (h : Width w s) :
Width w (step edge w s)

A step keeps the width.

theorem Cslib.Reachability.Walk.width_iterate (edge : List Bool → List Bool → Bool) {w : ℕ} {s : State} (h : Width w s) (n : ℕ) :
Width w ((step edge w)^[n] s)

Every state of a run keeps the width.

The levels of a stack within depth d: a target with n targets below it is at level at most d - n.

The stack of a single target at level d has its levels within depth d.

theorem Cslib.Reachability.Walk.LevelsLE.length_le {d : ℕ} {stack : List (List Bool × ℕ)} :
LevelsLE d stack → stack.length ≤ d + 1

A stack with its levels within depth d holds at most d + 1 targets.

theorem Cslib.Reachability.Walk.LevelsLE.le_of_mem {d : ℕ} {stack : List (List Bool × ℕ)} :
LevelsLE d stack → ∀ e ∈ stack, e.2 ≤ d

A stack with its levels within depth d has its targets at levels at most d.

theorem Cslib.Reachability.Walk.levelsLE_step (edge : List Bool → List Bool → Bool) (w : ℕ) {d : ℕ} {s : State} (h : LevelsLE d s.2) :
LevelsLE d (step edge w s).2

A step keeps the levels within depth d.

theorem Cslib.Reachability.Walk.levelsLE_iterate (edge : List Bool → List Bool → Bool) (w : ℕ) {d : ℕ} {s : State} (h : LevelsLE d s.2) (n : ℕ) :
LevelsLE d ((step edge w)^[n] s).2

Every state of a run keeps the levels within depth d.

A run between reachable vertices #

def Cslib.Reachability.Walk.Chain (edge : List Bool → List Bool → Bool) (w : ℕ) (cur : List Bool) :

The chain of a state: the current vertex reaches the first target, and each target the next, at their levels, between words of width w.

theorem Cslib.Reachability.Walk.Chain.length_eq {edge : List Bool → List Bool → Bool} {w : ℕ} {cur : List Bool} {stack : List (List Bool × ℕ)} :
Chain edge w cur stack → cur.length = w

The current vertex of a chain has width w.

theorem Cslib.Reachability.Walk.chain_step {edge : List Bool → List Bool → Bool} {w : ℕ} {s : State} (h : Chain edge w s.1 s.2) :
Chain edge w (step edge w s).1 (step edge w s).2

A step keeps the chain.

theorem Cslib.Reachability.Walk.chain_iterate {edge : List Bool → List Bool → Bool} {w : ℕ} {s : State} (h : Chain edge w s.1 s.2) (n : ℕ) :
Chain edge w ((step edge w)^[n] s).1 ((step edge w)^[n] s).2

Every state of a run keeps the chain.

theorem Cslib.Reachability.Walk.eq_or_edge_step {edge : List Bool → List Bool → Bool} {w : ℕ} {s : State} (h : Chain edge w s.1 s.2) :
(step edge w s).1 = s.1 ∨ edge s.1 (step edge w s).1 = true

A step from a chain stays put or follows an edge.

theorem Cslib.Reachability.Walk.eq_or_edge_iterate {edge : List Bool → List Bool → Bool} {w d : ℕ} {u v : List Bool} (hu : u.length = w) (hv : v.length = w) (h : reach edge w d u v = true) (n : ℕ) :
((step edge w)^[n + 1] (u, [(v, d)])).1 = ((step edge w)^[n] (u, [(v, d)])).1 ∨ edge ((step edge w)^[n] (u, [(v, d)])).1 ((step edge w)^[n + 1] (u, [(v, d)])).1 = true

Following a walk. If v is reachable from u within 2 ^ d steps between words of width w, every step of the run from u to the target v at level d stays put or follows an edge.