Data Structures & AlgorithmsHardGATE CS · ICPC regional
A directed acyclic graph has the vertices A, B, K, S, X, Y and the edges B→K, B→S, B→Y, B→X, S→K, Y→S, Y→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. B Y A X S K
- B. B Y X S K A
- C. A B Y S K X
- D. B Y S X A K
Correct answer: C. A B Y S K X
Kahn's algorithm that always takes the smallest available vertex (a min-heap) gives A B Y S K X. A FIFO queue gives A B Y S X K, which can differ, and a reverse-postorder DFS gives B Y X S K A.
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
- Keys 50, 30, 70, 20, 40, 60, 80, 35, 45, 65 are inserted in order into an empty BST. Then 30 is deleted, then 50 is deleted; a node with…
- The array [3, 9, 2, 1, 4, 5, 8] is converted to a max-heap by Floyd's bottom-up method, sifting down from the last parent to the root…
- Keys 50, 30, 70, 20, 40, 60, 80, 35, 45, 65, 36 are inserted in this order into an empty binary search tree. The post-order traversal of…
- Keys 40, 20, 10, 25, 30, 22, 50, 35, 45, 60, 55, 65 are inserted one by one into an initially empty AVL tree. The pre-order traversal of…
- Floyd's bottom-up build-heap (sifting down from the last internal node to the root) is applied to the array [7, 2, 9, 4, 6, 1, 8, 3, 5] to…
- A DAG on vertices 1..6 has edges 1→3, 2→3, 3→4, 3→5, 2→6 and 5→6. How many distinct topological orderings does it have?