Answer
Using mathematical induction to prove that breadth-first search visits vertices in order of their level in the resulting spanning tree.
Work Step by Step
-Because the edges not in the spanning tree are not followed
in the process, we can ignore them.
-Thus we can assume that the graph was a rooted tree to begin with. ---The basis step is trivial (there is only one vertex), so we assume the inductive hypothesis that breadth-first search applied to trees with n vertices have their vertices visited in order of their level in the
tree and consider a tree T with n+1 vertices.
-The last vertex to be visited during breadth-first search of this tree, say v, is the one that was added last to the list of vertices waiting to
be processed.
- It was added when its parent, say u, was being processed.
-We must show that v is at the lowest (bottom-most,
i.e., numerically greatest) level of the tree.
- Suppose not; say vertex x, whose parent is vertex w, is at a lower level.
-Then w is at a lower level than u.
-Clearly v must be a leaf, because any child of v could not have been seen before v is seen.
- Consider
the tree T obtained from T by deleting v. By the inductive hypothesis, the vertices in T must be processed in order of their level in T
(which is the same as their level in T , and the absence of v in T has no effect on the rest of the algorithm).
--Therefore u must have been processed before w, and therefore v would have joined the waiting list before x did, a contradiction.
--Therefore v is at the bottom-most level of the tree, and
the proof is complete.