Data Structures & AlgorithmsHardGATE CS · ICPC regional
Directed acyclic graph on vertices A, B, C, D, E, F, G with edges F→D, F→C, A→C, A→E, A→G, D→E, E→B. Among all topological orderings (every edge goes from earlier to later in the order), which one is lexicographically smallest when compared letter by letter? Give it as letters separated by spaces.
- A. A F G C D E B
- B. F D A G E C B
- C. F D A G E B C
- D. A F C D E B G
Correct answer: D. A F C D E B G
Kahn's algorithm with a min-heap always takes the smallest available vertex and yields A F C D E B G. A FIFO queue or reverse DFS postorder gives valid orders that are not lexicographically smallest.
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
- Strings a = "ccca" and b = "cbca". The edit distance allows insert, delete and substitute, each costing 1 (the standard Levenshtein…
- Tree with vertices 0 to 8, root 0, edges 0–1, 1–2, 1–3, 1–4, 2–5, 2–6, 6–7, 5–8 (parent–child). Vertex weights (vertex:weight) 0:11, 1:10…
- Array [27, 21, 1, 3, 16, 28, 17] is turned into a min-heap in place with bottom-up heapify: for i from ⌊n/2⌋−1 down to 0, sift the element…
- On an 8 × 8 chessboard with rows and columns numbered 0 to 7, a knight starts at (5, 7) and must reach (0, 5) (as (row, column)). A knight…
- Undirected weighted graph on vertices A, B, C, D, E, F with edges A–B (6), A–E (6), A–F (7), B–C (3), B–D (6), B–F (9), C–E (8), D–F (5)…
- Words ['dd', 'da', 'bb', 'bd', 'db', 'cc', 'aba'] (each may be used at most once). A chain is a sequence of these words in which the first…