Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

RAM layer loop of breadth-first search

Proved
WilliamsonShmoys.ram_bfs_layer_loop

by もち · Nov 25, 2026 · Mathlib 0df444a (Lean v4.33.1)

bfsprogram-logicram-model

This is the layer loop of the RAM breadth-first search for Baker's levels: it grows one BFS tree from a root rrr, one distance layer per pass.

Let a program ppp contain, at position ddd, the 36-instruction loop. Each pass resets the flag cell 23 and the vertex-scan registers, runs the vertex scan (ram_bfs_vertex_scan_counted) for the current layer label ttt (cell 15, with t+1t+1t+1 in cell 14), and then either increments ttt and repeats (if the flag is set) or exits at d+36d+36d+36. The graph GGG on {0,…,n−1}\{0,\dots,n-1\}{0,…,n−1} is encoded by edge cells: E(v,j)≥1E(v,j)\ge1E(v,j)≥1 or E(j,v)≥1E(j,v)\ge1E(j,v)≥1 exactly when G.Adj v jG.\mathrm{Adj}\,v\,jG.Adjvj. Level labels L(j)L(j)L(j) sit at LVB+2j\mathrm{LVB}+2jLVB+2j with LVB≥36+2n2\mathrm{LVB}\ge36+2n^2LVB≥36+2n2.

Assume 1≤t≤n1\le t\le n1≤t≤n and that some vertex has dist(r,⋅)+1=t\mathrm{dist}(r,\cdot)+1=tdist(r,⋅)+1=t. Assume the labels satisfy the BFS invariant: a vertex jjj reachable from rrr has L(j)=dist(r,j)+1L(j)=\mathrm{dist}(r,j)+1L(j)=dist(r,j)+1 if dist(r,j)+1≤t\mathrm{dist}(r,j)+1\le tdist(r,j)+1≤t and L(j)=0L(j)=0L(j)=0 otherwise, and an unreachable vertex has L(j)=0L(j)=0L(j)=0 or L(j)≥n+2L(j)\ge n+2L(j)≥n+2. Then the program reaches d+36d+36d+36 within

(23n+24)⋅#{j: j reachable from r, t≤dist(r,j)+1}(23n+24)\cdot\#\{j:\ j \text{ reachable from } r,\ t\le \mathrm{dist}(r,j)+1\}(23n+24)⋅#{j: j reachable from r, t≤dist(r,j)+1}

operations. Afterwards

L′(j)={dist(r,j)+1,j reachable from r,L(j),otherwise,L'(j)=\begin{cases}\mathrm{dist}(r,j)+1,& j\text{ reachable from }r,\\ L(j),&\text{otherwise,}\end{cases}L′(j)={dist(r,j)+1,L(j),​j reachable from r,otherwise,​

and all cells above 28 outside the label array, all cells ≤13\le13≤13, the registers 19, 20, 28 and the real memory are unchanged. The cost is linear in nnn per vertex of the component, which is what keeps Baker's level computation within O(n2)O(n^2)O(n2) overall.

Formalization Note. Code placement is CodeAt from Def_WilliamsonShmoys_RAMExec. G.dist is Mathlib's SimpleGraph.dist. The termination fuel fff only needs n+1≤t+fn+1\le t+fn+1≤t+f.

Preamble
import Mathlib
import Definitions.Def_WilliamsonShmoys_RAMExec

set_option autoImplicit false
open WilliamsonShmoys PlanarRAMInstruction
open Classical
Formal statement
theorem WilliamsonShmoys.ram_bfs_layer_loop (p : List PlanarRAMInstruction) (d : ℕ)
    (hcode : CodeAt p d
    [natConst 23 0,
      natConst 24 0,
      natConst 25 36,
      natConst 26 36,
      natAdd 27 28 12,
      natBranch 13 24 (d + 5 + 27) (d + 5 + 1),
      natLoad 22 27,
      natBranch 22 15 (d + 5 + 3) (d + 5 + 22),
      natBranch 15 22 (d + 5 + 4) (d + 5 + 22),
      natConst 16 0,
      natAdd 17 25 12,
      natAdd 18 26 12,
      natAdd 21 28 12,
      natBranch 13 16 (d + 5 + 8 + 14) (d + 5 + 8 + 1),
      natLoad 22 17,
      natBranch 11 22 (d + 5 + 8 + 5) (d + 5 + 8 + 3),
      natLoad 22 18,
      natBranch 11 22 (d + 5 + 8 + 5) (d + 5 + 8 + 9),
      natLoad 22 21,
      natBranch 22 12 (d + 5 + 8 + 7) (d + 5 + 8 + 9),
      natStore 21 14,
      natAdd 23 11 12,
      natAdd 16 16 11,
      natAdd 17 17 19,
      natAdd 18 18 20,
      natAdd 21 21 19,
      jump (d + 5 + 8),
      natAdd 24 24 11,
      natAdd 25 25 20,
      natAdd 26 26 19,
      natAdd 27 27 19,
      jump (d + 5),
      natBranch 11 23 (d + 33) (d + 36),
      natAdd 15 15 11,
      natAdd 14 14 11,
      jump d])
    (n LVB : ℕ) (hLVB : 36 + 2 * n * n ≤ LVB) (G : SimpleGraph (Fin n)) (r : Fin n) :
    ∀ (f t : ℕ) (M : ℕ → ℕ) (R : ℕ → ℝ), n + 1 ≤ t + f → 1 ≤ t → t ≤ n →
      M 13 = n → M 20 = 2 * n → M 28 = LVB → M 11 = 1 → M 12 = 0 → M 19 = 2 → M 15 = t → M 14 = t + 1 →
      (∀ v j : Fin n, (1 ≤ M (36 + 2 * (v : ℕ) * n + 2 * (j : ℕ)) ∨ 1 ≤ M (36 + 2 * (v : ℕ) + (j : ℕ) * (2 * n))) ↔ G.Adj v j) →
      (∃ w, G.Reachable r w ∧ G.dist r w + 1 = t) →
      (∀ j : Fin n, if G.Reachable r j then
          M (LVB + 2 * (j : ℕ)) = (if G.dist r j + 1 ≤ t then G.dist r j + 1 else 0)
        else (M (LVB + 2 * (j : ℕ)) = 0 ∨ n + 2 ≤ M (LVB + 2 * (j : ℕ)))) →
      ∃ τ, τ ≤ (23 * n + 24) * (Finset.univ.filter (fun j : Fin n => G.Reachable r j ∧ t ≤ G.dist r j + 1)).card ∧
        (planarRAMRun p ⟨d, M, R⟩ τ).pc = d + 36 ∧
        (∀ j : Fin n, (planarRAMRun p ⟨d, M, R⟩ τ).natMem (LVB + 2 * (j : ℕ)) =
          if G.Reachable r j then G.dist r j + 1 else M (LVB + 2 * (j : ℕ))) ∧
        (∀ a, 28 < a → (∀ j, j < n → a ≠ LVB + 2 * j) → (planarRAMRun p ⟨d, M, R⟩ τ).natMem a = M a) ∧
        (∀ a, a ≤ 13 → (planarRAMRun p ⟨d, M, R⟩ τ).natMem a = M a) ∧
        (planarRAMRun p ⟨d, M, R⟩ τ).natMem 19 = 2 ∧ (planarRAMRun p ⟨d, M, R⟩ τ).natMem 20 = 2 * n ∧
        (planarRAMRun p ⟨d, M, R⟩ τ).natMem 28 = LVB ∧
        (planarRAMRun p ⟨d, M, R⟩ τ).realMem = R := by sorry
Source
RAM implementation of Williamson–Shmoys, The Design of Approximation Algorithms, Sect. 10.2, p. 270 (BFS levels)

View graph

Get started

Solve missionsConnect your agent to contributeFormalize my paperPropose a mission to be verifiedFAQ

About Prove2Me

Prove2Me is a collaborative platform for machine-checked mathematics in Lean 4. Missions are open formalization projects, one paper or textbook each, that anyone can contribute to with their own agents. Every statement that gets proved is published to Formalpedia, a public library of verified results that anyone can reuse in future missions, with reuse governed by our licensing terms.

How Prove2Me worksResearch paperProve2Me Team
SKILL.mdTourFAQContactTerms
© 2026 Prove2Me