Bfs Find Shortest Path On Unweighted Graph
Bfs Find Shortest Path On Unweighted Graph
(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
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
r ; w0 → w
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
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).