Data Structures & AlgorithmsHardGATE CS · TCS NQT · Product Company SDE
An undirected graph has adjacency lists A:BCF B:ADF C:AE D:B E:CF F:ABE. Neighbours are always explored in alphabetical order. Which is the DFS (recursive, pre-order) visiting order starting at A?
- A. ABDFCE
- B. ABCFDE
- C. ABDFEC
- D. CEFDBA
Correct answer: C. ABDFEC
DFS goes as deep as possible before backtracking, taking neighbours in alphabetical order, giving ABDFEC.
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
- In the undirected graph with edges A–B:12, B–C:8, B–D:12, A–E:9, D–F:10, B–F:14, A–C:12, what is the shortest-path distance from A to E?
- Keys 81, 18, 92, 48, 71, 55, 44, 41 are inserted in this order into an empty BST. What is the postorder traversal?
- A max-heap is built by inserting 55, 85, 57, 63, 32, 19, 91, 12 one by one (sift-up on each insert). After one extract-max (move the last…
- A max-heap is built by inserting 73, 97, 96, 27, 78, 89, 18 one by one (sift-up on each insert). After one extract-max (move the last…
- A DAG has edges A→B, A→C, B→D, C→E, A→D, B→C. Using Kahn's algorithm (always picking the smallest-labelled vertex among those currently…
- What is the time complexity of searching for a word of length m in a trie that stores n words, independent of n?