BFS: Proof of Lemma 5, CPSC 331, Fall 2010

home page -  news -  syllabus -  schedule -  assignments -  tutorials -  tests -  java -  references -  Mike Jacobson


 Breadth-First Search: Proof of Lemma 5

Consider the BFS algorithm that was presented during the lecture on “Breadth First Search.&rdquo. As part of the proof of correctness of the algorithm the following lemma was included in these notes:

Lemma 5: If a vertex v is added to the queue at any point during the execution of the BFS algorithm then v is reachable from s. Furthermore the value d[v] that is set immediately before v is enqueued is equal to δ(s, v).

As the notes indicate, δ(s, v) is the length of a shortest path in the graph G from s to v. Since the objective of the algorithm is (in part) to compute these values for every vertex v that is reachable from the start vertex s, the lemma claims that the value for d[v] has been correctly set.

The objective of this document is to provide a proof of this lemma.

The first part of the lemma asserts that “if a vertex v is added to the queue at any point during the execution of the BFS algorithm then v is reachable from s.” This is a consequence of

The lemma also asserts that “the value of d[v] that is set immediately before v is added to the queue is equal to δ(s, v).

In order to prove this it is necessary and sufficient to prove, firstly, that d[v] ≥ δ(s, v), and to prove, secondly, that d[v] ≤ δ(s, v), as well.

Note that, since G s an undirected graph, δ(s, v) = δ(v, s). (Take a few minutes and make sure that you can prove this.) We may therefore consider paths from v to s (rather than the other way around) as we consider the value δ(s, v).

It can be seen by inspection of the algorithm that, since the colour of v is either grey or black after v is enqueued, the value of d[v] is never changed again. The inequality “d[v] ≥ δ()s, v)” is therefore a consequence of Lemma 2, which establishes the existence of a path in G from v to s with length d[v].

Proving the inequality “d[v] ≤ δ(s, v)” is, perhaps, the trickiest part of this argument. This claim can be proved by contradiction.

Suppose, therefore, that d[v] > δ(s, v) for at least one vertex v that is enqueued.

Since there are only finitely many vertices there must exist a vertex v such that d[v] > δ(s, v) and δ(s, v) is minimal — that is, such that d[w] = δ(s, w) for every vertex w such that δ(s, w) < δ(s, v). There may be more than one such vertex v, but there is at least one, and any of them may be used in this argument.

It is clear that v ≠ s, since d[s] is correctly set to be δ(s, s) = 0 by this algorithm.

It should also be clear that v is reachable from s — for, otherwise, δ(s, v) = +∞ and d[v] would not be too large, after all.

Therefore δ(s, v) must be equal to some positive integer k, and there must exist a path

(s, v1), (v1, v2), (v2, v3), … (vk−2, vk−1), (vk−1, v)

from s to v with length k in G.

Consider the vertex vk−1 just before v on this path. The existence of this path implies that δ(s, vk−1) ≤ k−1. On the other hand, it also clear that

δ(s, v)δ(s, vk−1) + 1,

so that δ(s, v) would be less than k if δ(s, vk−1) was less than k−1, and this is not the case. Therefore δ(s, vk−1) is (exactly) equal to k−1.

Since δ(s, vk−1) < δ(s, v), it follows by the choice of v that d[vk−1] is correctly set by the algorithm, that is, d[vk−1] = δ(s, vk−1) = k−1.

It now follows by inspection of the BFS algorithm that vk−1 is added to the queue at some point during the execution (since the value of d[vk−1] if only changed from +∞ to its final value when this happens).

Consider the colour of v at the point when vk−1 is added to the queue.

If v is either grey or black at this point then v must have been added to the queue before vk−1. However, it would then follow by Lemma 4 in the notes on this topic that d[v] ≤ d[vk−1] = k−1 at this point. Since the value of d[v] is not changed after this point, this contradicts the fact that d[v] > δ(s, v) = k.

Therefore the colour of v must be white when vk−1 is added to the queue.

Now, consider the colour of v at the beginning of the execution of the loop body that causes vk−1 to be removed from the queue. If this colour is not white at this point, then vk−1 and v are both on the queue at this point, with vk−1 at the front of the queue; it follows by Lemma 3 in the notes on this topic that

d[v]d[vk−1] + 1 = k

at this point (and thereafter), once again contradicting the fact that d[v] ≥ k+1 when the algorithm terminates. Consequently the colour of v must be white at the beginning of this execution of the body of the loop.

However, this is not possible either! vk−1 and v are neighbours, so that v must be considered and added to the queue, with d[v] set to be d[vk−1] + 1 = (k−1) + 1 = k during this execution of the loop body. The value of d[v] will never be changed after that, so that the final value of d[v] will turn out to be correct in this case too.

Since a contradiction has been obtained in every case we may now conclude that d[v] must be correctly set for every vertex v after all — completing the proof of the lemma.


Last updated:
http://www.cpsc.ucalgary.ca/~jacobs/cpsc331/F10/handouts/lecture31-supplement.html