Let G be a tree graph. Further, let A and B be the trees produced by performing BFS and DFS respectively on G. Can A and B be different trees? Why or why not?
- Dhruv Badaya

- May 25, 2024
- 1 min read
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