Algorithm Search⌘K

Proofs: Breadth-first search

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

Breadth-first search computes the shortest-path distances full

This proof shows: correctness, termination.

Let G=(V,E)G = (V, E) be a directed or undirected graph and s∈Vs \in V. For an undirected graph, read each edge {u,v}\{u, v\} as the two directed edges (u,v)(u, v) and (v,u)(v, u). Let δ(s,v)\delta(s, v) be the least number of edges on a path from ss to vv, and δ(s,v)=∞\delta(s, v) = \infty if there is no path. We use ∞+1=∞\infty + 1 = \infty.

Lemma 1 (edge bound). For each edge (u,v)∈E(u, v) \in E, δ(s,v)≤δ(s,u)+1\delta(s, v) \le \delta(s, u) + 1.

Proof. If δ(s,u)=∞\delta(s, u) = \infty, there is nothing to prove. Else take a path from ss to uu with δ(s,u)\delta(s, u) edges, and add the edge (u,v)(u, v). This is a walk from ss to vv with δ(s,u)+1\delta(s, u) + 1 edges. A walk contains a path with at most as many edges, so δ(s,v)≤δ(s,u)+1\delta(s, v) \le \delta(s, u) + 1. □\square

Lemma 2 (upper bound and single entry). At each moment of the run:

  1. dist[v]≥δ(s,v)\mathrm{dist}[v] \ge \delta(s, v) for each v∈Vv \in V;
  2. a finite value of dist[v]\mathrm{dist}[v] never changes;
  3. each vertex enters the queue at most one time, and it enters exactly when its distance becomes finite.

Proof. Lines L1 and L2 set dist[s]=0=δ(s,s)\mathrm{dist}[s] = 0 = \delta(s, s) and dist[v]=∞\mathrm{dist}[v] = \infty for v≠sv \ne s, so statement 1 holds, and line L3 puts ss, the only vertex with a finite distance, in the queue. After that, only line L8 writes to dist\mathrm{dist}. It runs only if dist[v]=∞\mathrm{dist}[v] = \infty (line L7), so it never changes a finite value, which is statement 2. It sets dist[v]=dist[u]+1\mathrm{dist}[v] = \mathrm{dist}[u] + 1 for an edge (u,v)(u, v), and by the induction hypothesis and Lemma 1, dist[u]+1≥δ(s,u)+1≥δ(s,v)\mathrm{dist}[u] + 1 \ge \delta(s, u) + 1 \ge \delta(s, v), which keeps statement 1. Line L10 enqueues vv exactly when line L8 makes its distance finite. Because this happens at most one time for each vv by statement 2, statement 3 holds. □\square

Lemma 3 (the queue is sorted). While the loop body of lines L5 to L10 handles a vertex uu, let ⟨u,v1,…,vr⟩\langle u, v_1, \ldots, v_r \rangle be uu followed by the queue from front to back. Outside the loop body, let ⟨v1,…,vr⟩\langle v_1, \ldots, v_r \rangle be the queue. In both cases, write x1,x2,…,xkx_1, x_2, \ldots, x_k for the sequence. Then

dist[x1]≤dist[x2]≤⋯≤dist[xk]≤dist[x1]+1.\mathrm{dist}[x_1] \le \mathrm{dist}[x_2] \le \cdots \le \mathrm{dist}[x_k] \le \mathrm{dist}[x_1] + 1.

Proof. By induction on the steps of the run. After line L3, the sequence is ⟨s⟩\langle s \rangle, and the claim holds. There are three kinds of steps.

  • Line L5. The sequence ⟨v1,…,vr⟩\langle v_1, \ldots, v_r \rangle becomes ⟨u,v2,…,vr⟩\langle u, v_2, \ldots, v_r \rangle with u=v1u = v_1. This is the same sequence of vertices, so the claim still holds.
  • Line L10. The vertex vv goes to the back, with dist[v]=dist[u]+1\mathrm{dist}[v] = \mathrm{dist}[u] + 1 and u=x1u = x_1. Before the step, dist[xk]≤dist[x1]+1=dist[v]\mathrm{dist}[x_k] \le \mathrm{dist}[x_1] + 1 = \mathrm{dist}[v], so the sequence stays sorted. The new last value is dist[x1]+1\mathrm{dist}[x_1] + 1, so the last inequality still holds.
  • End of the loop body. The vertex u=x1u = x_1 leaves the sequence. A sorted sequence stays sorted when its first item is removed. If the rest is not empty, dist[xk]≤dist[x1]+1≤dist[x2]+1\mathrm{dist}[x_k] \le \mathrm{dist}[x_1] + 1 \le \mathrm{dist}[x_2] + 1, and x2x_2 is the new first item.

No other line changes the queue or a finite distance, by Lemma 2. □\square

Corollary 4 (order of scanning). The vertices are dequeued in non-decreasing order of dist\mathrm{dist}. If xx is dequeued before yy, then dist[x]≤dist[y]\mathrm{dist}[x] \le \mathrm{dist}[y].

Proof. When uu is dequeued, it is the first item of the sequence of Lemma 3, so dist[u]\mathrm{dist}[u] is at most the distance of each vertex in the queue at that moment. The next vertex dequeued is in the queue at that moment, or is enqueued later during the scan of uu with distance dist[u]+1\mathrm{dist}[u] + 1. In both cases its distance is at least dist[u]\mathrm{dist}[u]. Distances do not change after they are finite (Lemma 2), so the claim follows by induction along the order of dequeues. □\square

Termination. By Lemma 2, at most nn vertices enter the queue. Each pass of the loop of line L4 removes one of them. So the loop runs at most nn times, and each pass runs the loop of line L6 over a finite list. When the loop stops, the queue is empty, so each vertex that was discovered has also been dequeued and scanned.

Theorem. When BFS(G,s)(G, s) returns, dist[v]=δ(s,v)\mathrm{dist}[v] = \delta(s, v) for each v∈Vv \in V. For each vv with v≠sv \ne s and δ(s,v)<∞\delta(s, v) < \infty, (π[v],v)∈E(\pi[v], v) \in E and δ(s,π[v])=δ(s,v)−1\delta(s, \pi[v]) = \delta(s, v) - 1.

Proof. Assume that some vertex has dist[v]≠δ(s,v)\mathrm{dist}[v] \ne \delta(s, v) at the end. Among these vertices, choose vv with the least δ(s,v)\delta(s, v). By Lemma 2, dist[v]>δ(s,v)\mathrm{dist}[v] > \delta(s, v), so δ(s,v)\delta(s, v) is finite. Also v≠sv \ne s, because dist[s]=0=δ(s,s)\mathrm{dist}[s] = 0 = \delta(s, s). Take a shortest path from ss to vv, and let uu be the vertex just before vv on it. Then (u,v)∈E(u, v) \in E and δ(s,u)=δ(s,v)−1\delta(s, u) = \delta(s, v) - 1. By the choice of vv, dist[u]=δ(s,u)\mathrm{dist}[u] = \delta(s, u), which is finite. So uu was discovered, and by the termination argument it was dequeued and scanned. Thus

dist[v]>δ(s,v)=δ(s,u)+1=dist[u]+1.(1)\mathrm{dist}[v] > \delta(s, v) = \delta(s, u) + 1 = \mathrm{dist}[u] + 1. \tag{1}

Look at the moment when the scan of uu reaches vv in line L7. There are three cases.

  • dist[v]=∞\mathrm{dist}[v] = \infty at this moment. Then line L8 sets dist[v]=dist[u]+1\mathrm{dist}[v] = \mathrm{dist}[u] + 1, and by Lemma 2 this value stays to the end. This contradicts (1).
  • vv was already dequeued. Then vv was dequeued before uu, and Corollary 4 gives dist[v]≤dist[u]\mathrm{dist}[v] \le \mathrm{dist}[u]. This contradicts (1).
  • vv is in the queue. Then vv is in the sequence of Lemma 3, whose first item is uu, so dist[v]≤dist[u]+1\mathrm{dist}[v] \le \mathrm{dist}[u] + 1. This contradicts (1).

A vertex with a finite distance was enqueued by Lemma 2, so it is in the queue or already dequeued, and the three cases cover all possibilities. So no such vv exists, and dist[v]=δ(s,v)\mathrm{dist}[v] = \delta(s, v) for each vv.

For the parents, let v≠sv \ne s be reachable. Then dist[v]=δ(s,v)\mathrm{dist}[v] = \delta(s, v) is finite, so line L8 ran for vv during the scan of some uu, and line L9 set π[v]=u\pi[v] = u at the same time, with (u,v)∈E(u, v) \in E and dist[v]=dist[u]+1\mathrm{dist}[v] = \mathrm{dist}[u] + 1. No later step changes π[v]\pi[v], because line L9 runs together with line L8. With the first part, δ(s,π[v])=δ(s,v)−1\delta(s, \pi[v]) = \delta(s, v) - 1. By induction on δ(s,v)\delta(s, v), the chain v,π[v],π[π[v]],…v, \pi[v], \pi[\pi[v]], \ldots reaches ss after exactly δ(s,v)\delta(s, v) steps, and read backwards it is a path from ss to vv with δ(s,v)\delta(s, v) edges, which is a shortest path. ■\blacksquare

Back to Breadth-first search