Data Structures & AlgorithmsHardGATE CS · TCS NQT · Product Company SDE
An undirected graph has adjacency lists A:BCDG B:AEF C:A D:AE E:BD F:B G:A. Neighbours are always explored in alphabetical order. Which is the DFS (recursive, pre-order) visiting order starting at A?
- A. ABEDFCG
- B. ABEDFGC
- C. GCFDEBA
- D. ABCDGEF
Correct answer: A. ABEDFCG
DFS goes as deep as possible before backtracking, taking neighbours in alphabetical order, giving ABEDFCG.
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
- Edges A-E, D-E, B-E, A-C, A-B are added one by one to an initially empty graph on vertices {A, B, C, D, E}, using Union-Find (with path…
- In the undirected graph with edges A–B:14, B–C:2, A–D:11, D–E:8, A–F:14, B–G:11, A–G:3, what is the shortest-path distance from A to B?
- Keys 37, 28, 13, 54, 24, 77, 95, 49, 68 are inserted in this order into an empty BST. What is the preorder traversal?
- A min-heap is built by inserting 69, 20, 19, 35, 32, 87, 52, 58 one by one (sift-up on each insert). After one extract-min (move the last…
- A directed weighted graph has edges A→B:3, A→C:5, B→D:5, D→E:9, A→D:5, C→E:-2 (some weights are negative, but there is no negative-weight…
- In the undirected graph with edges A–B:12, A–C:9, C–D:12, D–E:10, C–F:15, E–G:12, E–F:6, A–D:15, what is the shortest-path distance from A…