Data Structures & AlgorithmsVery hardGATE CS · Codeforces Div 2 C
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 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. F E B C G A D
- B. C A E B D F G
- C. C E F A B G D
- D. F E C G B A D
Correct answer: C. C E F A B G D
The FIFO queue outputs C E F A B G D. Replacing the queue with a stack gives F E B C G A D, and always taking the alphabetically smallest available vertex gives C A E B D F 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): 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…
- Directed acyclic graph on letters A to G with edges (X→Y means X must precede Y): A→G, B→F, E→B, E→C, E→F, E→G. Kahn's algorithm starts a…
- Directed acyclic graph on letters A to F with edges (X→Y means X must precede Y): B→C, D→A, D→C, E→C, E→D, F→A, F→C. Kahn's algorithm…
- Directed acyclic graph on letters A to F with edges (X→Y means X must precede Y): A→B, A→E, D→B, F→A, F→D. Kahn's algorithm starts a FIFO…