Data Structures & AlgorithmsHardGATE CS · ICPC regional · Product Company SDE
An undirected graph has adjacency lists (neighbours visited in alphabetical order) A: [B]; B: [A, C, F]; C: [B, E, F]; D: [F]; E: [C]; F: [B, C, D]. What is the (recursive) DFS traversal order starting from A?
- A. A, B, C, F, E, D
- B. A, B, C, D, E, F
- C. A, B, C, E, F, D
- D. D, F, E, C, B, A
Correct answer: C. A, B, C, E, F, D
Recursive DFS from A immediately recurses into the first unvisited neighbour before trying the next one, diving as deep as possible before backtracking, giving A, B, C, E, F, D. BFS from the same start would instead explore level by level, giving A, B, C, F, E, D.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- A binary tree has preorder traversal [90, 24, 42, 38, 77, 67, 57, 44] and inorder traversal [24, 38, 42, 44, 57, 67, 77, 90]…
- A binary tree has preorder traversal [9, 97, 20, 59, 28, 78] and inorder traversal [9, 20, 28, 59, 78, 97]. Reconstructing the tree (root…
- Keys 55, 72, 25, 37, 27, 49, 91 are inserted via separate chaining (h(k) = k mod 7, new keys appended to the end of the chain) into a…
- An undirected graph has edges (u–v: weight) A–B:4, B–D:7, A–D:15, A–C:29, B–C:28, C–D:29. What is the weight of its minimum spanning tree?
- Keys 27, 96, 68, 47, 51, 92, 12 are inserted in this order into an empty BST (no rebalancing). What is the height of the tree (edges on…
- An undirected graph has adjacency lists (neighbours visited in alphabetical order) A: [G]; B: [C]; C: [B, D]; D: [C, F, G]; E: [F]; F: [D…