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