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): C→A, D→E, E→C, F→C, G→A, G→B, G→C. 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. D F G E B C A
- B. G B F D E C A
- C. D E F G B C A
- D. G F D E C B A
Correct answer: A. D F G E B C A
The FIFO queue outputs D F G E B C A. Replacing the queue with a stack gives G B F D E C A, and always taking the alphabetically smallest available vertex gives D E F G B C A; 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): 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…
- 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…