1-- Bounded reachability as sets of vertices.2-- Part of Cumulus, the Grace17 mathematics library.3-- Copyright (c) 2026 Grant Passmore.4-- Released under the Apache License 2.0. See LICENSE in the repository root.5-- Authors: Grant Passmore67module Grace.Cumulus.GraphTheory.Reachability8import Complement9import Set.Core1011-- AtMost n contains vertices reachable in at most n edges. The successor12-- set is bounded by V, so recursion stays within mainstream set theory.13def nextNeighbors (V E A : Set) : Set:=14 { y ∈ V |∃ z ∈ A, Adjacent(E, z, y) }1516def reachableWithin (V E x : Set, n : Nat) →Set17| V, E, x, zero => {x}18| V, E, x, succ(n) =>19 reachableWithin(V, E, x, n) ∪ nextNeighbors(V, E, reachableWithin(V, E, x, n))2021theorem reachableWithin.zero (V E x : Set)22shows reachableWithin(V, E, x, 0) = {x}23proof24unfold reachableWithin25rfl2627theorem reachableWithin.successor (V E x : Set, n : Nat)28shows reachableWithin(V, E, x, succ(n)) =29 reachableWithin(V, E, x, n) ∪ nextNeighbors(V, E, reachableWithin(V, E, x, n))30proof31unfold reachableWithin32rfl3334theorem reachableWithin.monotone (V E x : Set, n : Nat)35shows reachableWithin(V, E, x, n) ⊆ reachableWithin(V, E, x, succ(n))36proof37rw reachableWithin.successor38exact union2.subset_left3940theorem reachableWithin.source (V E x : Set, n : Nat)41shows x ∈ reachableWithin(V, E, x, n)42proof43induction n44| zero =>45rw reachableWithin.zero46apply singleton.membership.backward47rfl48| succ(k) using ih =>49rw reachableWithin.successor50apply union2.membership.backward51left52exact ih5354theorem reachableWithin.advance (V E x y z : Set, n : Nat)55requires reached : y ∈ reachableWithin(V, E, x, n)56requires hz : z ∈ V57requires edge : Adjacent(E, y, z)58shows z ∈ reachableWithin(V, E, x, succ(n))59proof60rw reachableWithin.successor61apply union2.right62exact nextNeighbors.membership.backward with⟨hz, y, reached, edge⟩6364predicate Reachable (V E x y : Set) :=65∃ n ∈ ℕ, y ∈ reachableWithin(V, E, x, n)6667theorem Reachable.reflexive (V E x : Set)68shows Reachable(V, E, x, x)69proof70unfold Reachable71have source := reachableWithin.source at V, E, x, 0with Naturals.constructor.zero72have zeroNat := nat.omega.forward with Naturals.constructor.zero73refine⟨0, zeroNat, source⟩7475theorem Reachable.of_edge (V E x y : Set)76requires hy : y ∈ V77requires edge : Adjacent(E, x, y)78shows Reachable(V, E, x, y)79proof80unfold Reachable81have source := reachableWithin.source at V, E, x, 0with Naturals.constructor.zero82have reached := reachableWithin.advance with Naturals.constructor.zero, source, hy, edge83have oneNat := Naturals.constructor.succ with Naturals.constructor.zero84have oneInOmega := nat.omega.forward with oneNat85refine⟨1, oneInOmega, reached⟩86