Data Structures & AlgorithmsHardProduct-company placement
An undirected graph has edges A–B, A–G, B–E, C–E, C–F, C–G, D–E, D–F, D–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 E C F D G
- B. A B G E C D F
- C. A G D F E C B
- D. A G D F C E B
Correct answer: D. A G D F C E B
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 D F C E B.
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 shortest string that has BOTH COMPUTER and SUBSEQUENT as subsequences (shortest common supersequence)?
- A rooted tree has 6 nodes with 1 child, 6 nodes with 2 children, 3 nodes with 3 children, and every other node is a leaf. How many leaves…
- Edit distance between HEAPSORT and DYNAMIC where insert and delete cost 1 each but a substitution costs 2: what is the minimum total cost?
- How many topological orderings does the DAG on vertices A–F with edges C→A, D→A, E→A, E→D, F→C, F→D, F→E have?
- An undirected connected graph has edges A–D, A–H, B–F, B–G, C–G, C–H, D–E, D–G, F–H, G–I. How many articulation points (cut vertices) and…
- Lectures occupy half-open intervals [start, end): [5, 13), [8, 13), [8, 16), [13, 16), [13, 17), [13, 18), [13, 20), [17, 22). A lecture…