Data Structures & AlgorithmsHardGATE CS
An undirected graph has edges A–D, A–F, A–G, B–C, B–F, C–D, C–E, E–G, 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 D C B F G E
- B. A D F G C B E
- C. A G F B C E D
- D. A G E C B F D
Correct answer: C. A G F B C E D
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 G F B C 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
- What is the length of the longest common subsequence of ONTOLOGY and TOPOLOGY, and how many DISTINCT longest common subsequences (as…
- Edit distance between MARTIAN and COMPILER where insert and delete cost 1 each but a substitution costs 2: what is the minimum total cost?
- Dijkstra's algorithm runs from A on the directed graph A→C (4), B→A (1), B→F (6), C→B (7), C→E (12), C→F (6), D→C (3), E→A (15), E→B (3)…
- The weighted undirected graph A–B (27), A–D (3), A–E (1), B–C (25), B–D (8), B–E (18), B–F (5), C–E (9) has a unique MST. What is the…
- A simple undirected graph has 13 vertices and exactly 4 connected components. What is the maximum possible number of edges?
- What is the length of the shortest string that has BOTH COMPILER and SHORTEST as subsequences (shortest common supersequence)?