top of page

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?

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.

  1. Start at the root node r.

  2. Both BFS and DFS will first visit all children of r, establishing the same parent-child relationships.

  3. By the unique path property, each subtree rooted at the children of r must also be a tree.

  4. By the inductive hypothesis, for each subtree with k nodes, BFS and DFS produce the same tree.

  5. 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.

31 views0 comments
logo

Crookshanks Academy is an online learning platform with free netflix styled courses.

Crookshanks Academy 2023 ©️ All Rights Reserved

This content is protected from right clicks.
bottom of page