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): D→A, D→G, E→B, F→C, G→A, G→B. 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 G B A
- B. D E F G C A B
- C. D E F C G A B
- D. F E D G C B A
Correct answer: B. D E F G C A B
The FIFO queue outputs D E F G C A B. Replacing the queue with a stack gives F C E D G B A, and always taking the alphabetically smallest available vertex gives D E F C G A B; 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
- Undirected graph on vertices 0 to 5 with edges listed as: 1–4, 0–1, 3–4, 1–2, 0–5, 2–3, 2–5, 0–4, 0–3. The adjacency list of each vertex…
- Undirected graph on vertices 0 to 5 with edges listed as: 2–3, 1–3, 0–1, 0–4, 1–5, 3–5, 2–5, 2–4, 0–3, 1–4. The adjacency list of each…
- Undirected graph on vertices 0 to 5 with edges listed as: 2–3, 0–3, 0–2, 0–5, 3–5, 0–4, 2–5, 4–5, 0–1, 1–4, 1–3. The adjacency list of…
- Undirected graph on vertices 0 to 5 with edges listed as: 2–3, 1–4, 0–1, 3–5, 3–4, 0–5, 0–4, 2–5, 0–2, 0–3. The adjacency list of each…
- Undirected graph on vertices 0 to 7 with edges listed as: 0–3, 3–7, 0–1, 1–2, 1–4, 0–4, 3–6, 0–5, 2–5. The adjacency list of each vertex…
- Undirected graph on vertices 0 to 5 with edges listed as: 3–4, 4–5, 0–4, 1–3, 3–5, 1–4, 1–2, 0–1, 2–4. The adjacency list of each vertex…