When the graph is already a tree, both BFS and DFS produce the same tree. Hence, A and B cannot be distinct. Let us prove this by induction.
Assume that for a tree with k nodes, both BFS and DFS produce the same tree structure. Now consider a tree with k+1 nodes.
Start at the root node r.
Both BFS and DFS will first visit all children of r, establishing the same parent-child relationships.
By the unique path property, each subtree rooted at the children of r must also be a tree.
By the inductive hypothesis, for each subtree with k nodes, BFS and DFS produce the same tree.
Thus, for the tree with k+1 nodes, BFS and DFS must also produce the same overall tree structure.
By induction, BFS and DFS produce the same tree for any tree T.
Comments