Data Structures & AlgorithmsModerateCodeforces Div 2 D · ICPC regional
A directed acyclic graph has the vertices A, E, G, V, X, Z and the edges G→E, G→V, X→G, X→A, A→Z, Z→E. A topological order lists every vertex once so that each edge goes from an earlier to a later vertex. Which topological order is lexicographically smallest when read as a sequence of letters?
- A. X A G Z V E
- B. X A G V Z E
- C. X G A V Z E
- D. X G V A Z E
Correct answer: B. X A G V Z E
Kahn's algorithm that always takes the smallest available vertex (a min-heap) gives X A G V Z E. A FIFO queue gives X A G Z V E, which can differ, and a reverse-postorder DFS gives X G V A Z E.
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
- A tree has the vertices A, F, I, O, P, U, Y and the edges I–A, I–F, F–U, U–O, F–P, P–Y. What is the minimum number of vertices needed so…
- A tree has vertices with values in brackets: U[-2], B[7], H[5], A[7], N[-2], P[5], C[4], S[-1]. The edges are U–B, U–H, H–A, A–N, B–P…
- An undirected graph has the vertices A, J, Q, T, Y and the edges T–Q, T–A, Y–Q, Y–A, Q–A. What is its chromatic number, the least number…
- A tree has the vertices B, C, D, E, M, P, S, U, V, W and the edges U–B, U–C, C–W, B–V, U–M, M–D, D–P, B–S, W–E. A set D dominates the tree…
- A tree has vertices D, H, J, M, Q, R, U, W and weighted edges (weights in brackets): D–H (9), H–Q (5), H–W (5), Q–M (1), D–U (8), U–J (2)…
- An undirected connected graph on the vertices F, G, Q, S, U has the edges U–G, S–Q, F–Q, U–F, G–S, U–S, S–F, G–Q, U–Q. How many spanning…