Algorithm Search⌘K

Proofs: Whatever-first search

These are the human proofs of Whatever-first search, at full length.

Whatever-first search marks exactly the reachable vertices and builds a tree full

This proof shows: correctness, termination.

Let G=(V,E)G = (V, E) be a directed or undirected graph, where an undirected edge is in both adjacency lists, and let s∈Vs \in V. The bag BB may follow any discipline: we only use that TAKE removes and returns some item that is in BB. Let RR be the set of vertices reachable from ss.

Lemma 1 (pairs come from marked vertices). At each moment, each pair (p,v)(p, v) in BB is either (NIL,s)(\mathrm{NIL}, s) or has pp marked and (p,v)∈E(p, v) \in E.

Proof. The pair (NIL,s)(\mathrm{NIL}, s) is put at line L2. All other pairs are put at line L7, as (v,w)(v, w) with w∈Adj[v]w \in \mathrm{Adj}[v], right after line L6 marked vv. Marks are never removed. □\square

Lemma 2 (each vertex is marked once). Line L6 runs at most one time for each vertex, and line L7 scans each list at most one time.

Proof. Line L6 runs for vv only after line L5 found vv unmarked, and it marks vv, which then stays marked. Line L7 runs only right after line L6. □\square

Termination. By Lemma 2, line L7 runs at most nn times, so at most 1+∑v∣Adj[v]∣1 + \sum_v |\mathrm{Adj}[v]| pairs are ever put. Each iteration of the loop of line L3 takes one pair, so there are at most that many iterations, and the loop ends with BB empty.

Lemma 3 (marked means reachable). Each marked vertex is in RR, and if v≠sv \ne s is marked, then π[v]\pi[v] was marked before vv and (π[v],v)∈E(\pi[v], v) \in E.

Proof. By induction on the order of marking. The first pair taken is (NIL,s)(\mathrm{NIL}, s), because it is the only pair until a vertex is marked, so ss is marked first, with π[s]=NIL\pi[s] = \mathrm{NIL}, and s∈Rs \in R. If v≠sv \ne s is marked later by the pair (p,v)(p, v), then this pair is not (NIL,s)(\mathrm{NIL}, s), since ss was already marked when it could be taken again; by Lemma 1, pp is marked and (p,v)∈E(p, v) \in E. Line L6 sets π[v]=p\pi[v] = p, and pp was marked before vv. By the induction hypothesis p∈Rp \in R, so v∈Rv \in R too. □\square

Lemma 4 (reachable means marked). When the algorithm ends, each vertex of RR is marked.

Proof. Assume that some v∈Rv \in R is unmarked at the end, and let s=x0,x1,…,xk=vs = x_0, x_1, \ldots, x_k = v be a path in GG. The vertex ss is marked, so there is a first unmarked vertex xix_i on the path, with i≥1i \ge 1. The vertex xi−1x_{i-1} is marked, so line L7 ran for it and put the pair (xi−1,xi)(x_{i-1}, x_i), because xi∈Adj[xi−1]x_i \in \mathrm{Adj}[x_{i-1}]. The loop ends only when BB is empty, so this pair was taken at some step. At that step, line L5 either found xix_i marked or marked it. Either way xix_i is marked at the end, which contradicts its choice. □\square

Theorem. WHATEVER-FIRST-SEARCH(G,s)(G, s) terminates for every discipline of the bag. When it ends, mark[v]=TRUE\mathrm{mark}[v] = \mathrm{TRUE} exactly for v∈Rv \in R, and the edges (π[v],v)(\pi[v], v) for v∈R∖{s}v \in R \setminus \{s\} form a tree rooted at ss that spans RR, in which each path from ss is a path of GG.

Proof. Termination was shown above. Lemmas 3 and 4 give the marked set. For the tree, take v∈Rv \in R and follow v,π[v],π[π[v]],…v, \pi[v], \pi[\pi[v]], \ldots. By Lemma 3, each step goes to a vertex that was marked strictly earlier, along an edge of GG read backwards, so no vertex repeats, and the chain ends at the only marked vertex with parent NIL, which is ss, because line L6 sets π[v]=p≠NIL\pi[v] = p \ne \mathrm{NIL} for each v≠sv \ne s. Each v∈R∖{s}v \in R \setminus \{s\} has exactly one parent, so the ∣R∣−1|R| - 1 edges connect all of RR to ss without a cycle, and they form a tree rooted at ss. Its path from ss to vv is the chain read forwards, a path of GG. ■\blacksquare

Back to Whatever-first search