0% found this document useful (0 votes)
74 views3 pages

Bfs Find Shortest Path On Unweighted Graph

BFS finds shortest paths between the root node and all other reachable nodes in a graph. This is proven by induction on the "position number" of each node in the BFS tree. The position number indicates the order in which nodes are inserted into the queue during the BFS search. It is proven that for any node v: (1) its distance from the root is equal to its level in the BFS tree, and (2) any other node at a lower distance must have a lower position number. This guarantees BFS finds shortest paths by always exploring closer nodes before farther ones.

Uploaded by

Ngo Quang Minh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views3 pages

Bfs Find Shortest Path On Unweighted Graph

BFS finds shortest paths between the root node and all other reachable nodes in a graph. This is proven by induction on the "position number" of each node in the BFS tree. The position number indicates the order in which nodes are inserted into the queue during the BFS search. It is proven that for any node v: (1) its distance from the root is equal to its level in the BFS tree, and (2) any other node at a lower distance must have a lower position number. This guarantees BFS finds shortest paths by always exploring closer nodes before farther ones.

Uploaded by

Ngo Quang Minh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

A proof that BFS finds shortest paths

In class, I presented a proof that BFS actually computes shortest paths,


a fact that is intuitively “obvious,” and yet, a careful proof takes a bit of
work. This handout gives the same proof, with some of the details expanded,
and is provided for your reference.

First, some notation. There are n vertices in the graph, numbered 1 . . . n.


The BFS starts at vertex r, which forms the root of the BFS tree, and a
total of ` vertices are reachable from r (and hence visited during BFS). For
a vertex v, we define
• level [v] to be the level of v in the BFS tree,

• dist[v] to be the actual distance from r to v in the graph, and

• pos[v] to be the “position number” i of v, where 1 ≤ i ≤ `, and v was


the ith vertex to be inserted into the queue during BFS.
We want to show that level [v] = dist[v] for all v. We do this by induction
on pos[v], and strengthen the induction hypothesis. Namely, we prove by
induction on i = 1 . . . ` that for v with pos[v] = i, we have
(I1) dist[v] = level [v], and

(I2) for any vertex w, if dist[w] < dist[v], then pos[w] < pos[v].
The case i = 1 is trivially true, as the reader may verify. We now prove
(I1) and (I2) for 1 < i ≤ `, assuming (I1) and (I2) hold for all i0 < i.
To prove (I1) for i, let pos[v] = i, let v 0 be the parent of v in the BFS
tree. Suppose, by way of contradiction, that there is a path

r;w→v

of length h < level [v]. First, we have

pos[v 0 ] < pos[v], (1)

since v is placed in the queue when v 0 is removed from the queue. Second,
we have
dist[w] < dist[v 0 ], (2)
since by (1) we may apply the induction hypothesis (I1) at pos[v 0 ], obtaining

dist[v 0 ] = level [v 0 ] = level [v] − 1 > h − 1 ≥ dist[w0 ].


Third, we have
pos[w] < pos[v 0 ], (3)
since by (1) we may apply the induction hypothesis (I2) at pos[v 0 ], together
with (2), to obtain (3).
But now consider the point in time during the execution of BFS when
w was removed from the queue. Since there is an edge w → v, the BFS
algorithm would visit v at this point in time, if it had not already at an
earlier time. Thus, the parent of v has position number at most pos[w],
which by (3) is strictly less than pos[v], and so v 0 cannot be the parent of v,
as assumed — a contradiction.

To prove (I2), suppose pos[v] = i and

dist[w] < dist[v]. (4)

By way of contradiction, assume that pos[w] ≥ pos[v]. Since by (4), w 6= v,


it follows that pos[w] 6= pos[v], and hence

pos[w] > pos[v]. (5)

Let v 0 the parent of v in the BFS tree, and let

r ; w0 → w

be a shortest path from r to w. This implies that r ; w0 is a shortest path


from r to w0 . From this, we may conclude that

dist[w0 ] = dist[w] − 1. (6)

First, just as in the proof of (I1), we have

pos[v 0 ] < pos[v]. (7)

Second, by hypothesis (I1) at pos[v], which we just proved above, the tree
path r ; v 0 → v is a shortest path, and hence the tree path r ; v 0 is also
a shortest path. From this, we may conclude that

dist[v 0 ] = dist[v] − 1. (8)

Now, (4), together with (6) and (8), imply that

dist[w0 ] < dist[v 0 ]. (9)


Applying the induction hypothesis (I2) at pos[v 0 ], which by (7) is less than
pos[v], along with (9), we obtain

pos[w0 ] < pos[v 0 ]. (10)

Now, v was placed in the queue when v 0 was removed from the queue,
and w was placed in the queue when w0 was removed or at some earlier
time. By (10), this means that w would be placed in the queue before v was
placed in the queue, which contradicts (5).

You might also like