Data Structures & AlgorithmsHardProduct-company placement
An undirected graph has edges A–B, A–C, A–D, B–F, B–G, C–E, D–F, E–F, F–G. An ITERATIVE DFS starts with stack [A]; it pops a vertex, skips it if already visited, otherwise marks and outputs it and pushes all its unvisited neighbours in alphabetical order. What is the output order?
- A. A B F D E C G
- B. A B C D F G E
- C. A D F G E C B
- D. A D F G B E C
Correct answer: D. A D F G B E C
Pushing neighbours in alphabetical order means the alphabetically LAST one is popped first, so this stack DFS is not the same as recursive DFS with alphabetical scanning. Simulating the stack gives A D F G B E C.
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
- Coins of denominations 1, 4, 7, 8 are available in unlimited supply. What is the minimum number of coins needed to make 38, and how many…
- Heap sort (ascending) is run on [24, 11, 17, 25, 37, 7, 32, 23]: build a max-heap bottom-up, then repeatedly swap the root with the last…
- A DAG on vertices A–G has edges B→A, B→D, B→E, B→F, E→C, F→A, F→C, F→D (G isolated). What order does Kahn's algorithm with a FIFO queue…
- The words stem, stall, stamp, star, step are inserted into an empty trie (one node per character, shared prefixes share nodes, plus a…
- A binary tree with distinct labels has INORDER G H B F C E A D I and LEVEL-ORDER I E G D F A H C B. What is its postorder traversal?
- Floyd–Warshall runs on vertices 1–4 with directed edges 1→2 (10), 1→4 (1), 2→1 (12), 2→3 (10), 3→1 (12), 4→1 (1), 4→3 (4). After the…