Data Structures & AlgorithmsVery hardGATE CS · TCS NQT · Product Company SDE
A DAG has edges A→B, A→C, A→D, A→E, D→E. Using Kahn's algorithm (always picking the smallest-labelled vertex among those currently available with in-degree 0), what is the topological order?
- A. ABCDE
- B. DBAEC
- C. EDCBA
- D. ACBDE
Correct answer: A. ABCDE
Repeatedly remove the smallest-labelled vertex with in-degree 0 and decrement its neighbours' in-degree, giving ABCDE.
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 75, 52, 51, 68, 44, 20, 77, 85 are inserted in this order into an empty BST. How many nodes lie in the left subtree of the root (75)?
- A max-heap is built by inserting 50, 84, 44, 25, 79, 86 one by one (sift-up on each insert). After one extract-max (move the last element…
- A min-heap is built by inserting 60, 47, 76, 55, 61, 80, 34 one by one (sift-up on each insert). After one extract-min (move the last…
- Inserting into a binary heap of n elements (array-based, sift-up) takes worst-case time:
- Keys 12, 54, 92, 89, 71, 88, 69 are inserted in this order into an empty BST. What is the postorder traversal?
- A max-heap is built by inserting 87, 88, 19, 50, 39, 89 one by one (sift-up on each insert). After one extract-max (move the last element…