Data Structures & AlgorithmsHardGATE CS · Product-company placement
A DAG on vertices A–G has edges A→D, A→F, C→D, E→C, F→B, F→D, G→A, G→F. What order does Kahn's algorithm with a FIFO queue (initial zero in-degree vertices enqueued alphabetically; newly freed vertices enqueued in the order they become free, scanning each adjacency list alphabetically) produce?
- A. E G C A F B D
- B. E C G A F B D
- C. G E C A F D B
- D. G E C A F B D
Correct answer: A. E G C A F B D
All four candidate orders are valid topological orders, which is exactly the trap: only the stated rule fixes one. Simulating it gives E G C A F B 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 edit distance (insert, delete, substitute; each cost 1) between DIAMETER and INTERVAL?
- Prim's algorithm starts at F on the weighted undirected graph A–B (18), A–F (15), B–E (26), B–F (23), B–G (21), C–D (9), C–E (16), D–E…
- A rod of length 8 can be cut into integer pieces; a piece of length i sells for p(i) with p(1..8) = 1, 2, 5, 14, 16, 22, 24, 25. What is…
- 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…
- 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…