Data Structures & AlgorithmsHardGATE CS · Product-company placement
An undirected graph has edges A–B, A–E, A–F, B–D, B–E, B–F, C–D, C–G, E–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 F B E G C D
- B. A B D C G E F
- C. A B E F D G C
- D. A F E G C D B
Correct answer: A. A F B E G C 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 F B E G C 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
- 0/1 knapsack with capacity 16: items (weight, value) = (9, 40), (3, 34), (2, 19), (7, 38), (7, 32). What is the maximum total value?
- Using parts 1, 5, 6 (each any number of times), in how many ways can 9 be written as an ORDERED sum (so 1 + 2 and 2 + 1 count separately)?
- In the DAG with edges A→B, A→G, B→D, D→G, E→A, E→B, E→C, E→D, E→F, E→G, G→C, how many distinct directed paths are there from E to C, and…
- A 5 × 5 grid is given row by row as .##.. / ..... / ....# / ...#. / .#... ('#' = wall). Moving one step up, down, left or right through…
- What is the minimum number of cuts needed to split 'madamimad' into pieces that are all palindromes?
- A binary min-heap is stored in a 0-indexed array [13, 33, 15, 35, 42, 23, 32]. Perform, in order: insert 6, delete-min, insert 60…