Data Structures & AlgorithmsModerateGATE CS · Codeforces Div 2 C
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 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 C E D A B
- B. B C E D A F
- C. B C F E D A
- D. F C E D B A
Correct answer: C. B C F E D A
The FIFO queue outputs B C F E D A. Replacing the queue with a stack gives F C E D A B, and always taking the alphabetically smallest available vertex gives B C E D A 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): 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…
- 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…