Data Structures & AlgorithmsHardGATE CS · Codeforces Div 2 C
Directed acyclic graph on letters A to G with edges (X→Y means X must precede Y): B→D, B→G, C→A, C→F, E→D, E→F, E→G, F→D. Kahn's algorithm starts a FIFO queue with all in-degree-0 vertices in alphabetical order; it repeatedly dequeues a vertex, outputs it, and enqueues each out-neighbour (scanned alphabetically) whose remaining in-degree becomes 0. What is the output sequence?
- A. E C F A B G D
- B. B C A E F D G
- C. E C F B G D A
- D. B C E A F G D
Correct answer: D. B C E A F G D
The FIFO queue outputs B C E A F G D. Replacing the queue with a stack gives E C F A B G D, and always taking the alphabetically smallest available vertex gives B C A E F D G; only the queue discipline is asked.
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
- Directed acyclic graph on letters A to G with edges (X→Y means X must precede Y): C→A, D→E, E→C, F→C, G→A, G→B, G→C. Kahn's algorithm…
- Directed acyclic graph on letters A to G with edges (X→Y means X must precede Y): A→G, B→C, B→G, C→G, D→A, D→G, E→G, F→A, F→B, F→E, F→G…
- Directed acyclic graph on letters A to G with edges (X→Y means X must precede Y): A→D, C→A, C→G, E→B, E→D, F→G. Kahn's algorithm starts a…
- Directed acyclic graph on letters A to G with edges (X→Y means X must precede Y): B→A, C→A, C→G, D→C, E→A, E→B, E→C. Kahn's algorithm…
- Directed acyclic graph on letters A to F with edges (X→Y means X must precede Y): B→D, E→A, E→C, E→F, F→A. Kahn's algorithm starts a FIFO…
- Directed acyclic graph on letters A to G with edges (X→Y means X must precede Y): B→F, D→B, E→B, E→C, E→F, G→B, G→F. Kahn's algorithm…