Data Structures & AlgorithmsHardCodeforces Div 2 C · GATE CS
A directed acyclic graph has the vertices D, E, L, M, N, X, Z and the edges L→X, L→N, L→M, D→L, M→N, M→E, 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. Z D L X M E N
- B. D Z L M X E N
- C. D L M N X Z E
- D. Z D L X M N E
Correct answer: C. D L M N X Z E
Kahn's algorithm that always takes the smallest available vertex (a min-heap) gives D L M N X Z E. A FIFO queue gives D Z L M X E N, which can differ, and a reverse-postorder DFS gives Z D L X M N 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 vertices C, I, M, R, S, U, X and weighted edges (weights in brackets): M–C (9), C–U (2), C–I (8), C–X (1), M–R (8), U–S (7)…
- A directed weighted graph has the arcs u→v (weight): J→Z (11), J→G (6), T→H (1), Z→J (8), Z→S (3), Z→G (3), S→J (2), S→T (12), S→G (2)…
- A counter starts at 1. Each step, you may add 1, add 2, or double the counter. Each step must stay at or below 15. Two sequences of steps…
- A pile holds p stones. In one move a player removes exactly s stones, where s is one of {1, 2, 4} and s ≤ the pile size. The player who…
- An undirected graph has the vertices D, N, P, W, Z and the edges W–D, W–P, W–Z, D–N, N–Z. In how many ways can each vertex be given one of…
- Consider this function: python def g(n): if n < 3: return n if n % 2 == 0: return g(n - 3) + g(n - 3) return g(n - 3) - 1 What value does…