Data Structures & AlgorithmsModerateCodeforces Div 2 C · GATE CS
A directed acyclic graph has the vertices A, H, N, Q, X, Z and the edges N→Z, H→Z, A→H, Q→H, Q→Z, Q→X. 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. N Q A H Z X
- B. A N Q H X Z
- C. N A Q H Z X
- D. Q X N A H Z
Correct answer: B. A N Q H X Z
Kahn's algorithm that always takes the smallest available vertex (a min-heap) gives A N Q H X Z. A FIFO queue gives A N Q H X Z, which can differ, and a reverse-postorder DFS gives Q X N A H Z.
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
- An undirected graph has the vertices A, B, D, P, R, Y and the edges P–D, P–B, P–R, D–A, D–Y, B–Y, A–R, R–Y. In how many ways can each…
- Each cell of a grid with 3 rows and 5 columns has an arrow: R = right, L = left, U = up, D = down. RURUR LRDLU RDDRD From a cell you may…
- On an 8×8 chessboard, rows and columns are numbered 0 to 7, and squares are written (row, column). A knight moves in the usual L-shape…
- Bellman–Ford is run from source X on the directed graph whose arcs (weights in brackets) are listed in the order they are relaxed: F→I…
- A tree has the vertices C, E, G, K, N, S, T, V, Z and the edges T–K, K–E, K–V, K–S, S–N, N–C, C–G, G–Z. A centroid is a vertex whose…
- A grid has 3 rows and 3 columns with these values (top to bottom): 787 591 837 A path moves between side-adjacent cells (up, down, left or…