Data Structures & AlgorithmsModerateTCS NQT · Infosys pattern
Kahn's algorithm uses a MIN-heap (smallest available vertex first) on the DAG with edges 3→1, 3→4, 2→4, 4→0, 1→0, 5→2. What topological order does it produce?
- A. 3, 5, 1, 2, 4, 0
- B. 5, 3, 2, 1, 4, 0
- C. 3, 1, 4, 5, 2, 0
- D. 3, 1, 5, 2, 4, 0
Correct answer: D. 3, 1, 5, 2, 4, 0
Initially in-degree 0: {3, 5} → take 3; this frees 1 (4 still waits for 2) → heap {1, 5} → take 1 (0 still waits for 4) → take 5 → frees 2 → take 2 → frees 4 → take 4 → frees 0. Order 3, 1, 5, 2, 4, 0. A FIFO queue would take 5 before 1 (3, 5, 1, 2, 4, 0); taking 4 right after 1 ignores its pending edge from 2.
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
- The words bat, bath, bad, ban, band are inserted into an empty trie (one node per character, plus the root). How many nodes does the trie…
- Keys 50, 30, 70, 20, 40, 60, 80, 35, 65, 10 are inserted into an empty BST. How many nodes have EXACTLY one child?
- A climber can take 1 or 3 steps at a time. In how many different ordered ways can a staircase of 7 steps be climbed? (ways(n) = ways(n −…
- Keys 50, 40, 30, 45, 47 are inserted in that order into an empty AVL tree. What is the preorder traversal of the final tree?
- Kruskal's algorithm runs on edges (weight): A–B 4, A–C 3, B–C 2, B–D 5, C–D 7, C–E 8, D–E 6, D–F 9, E–F 4. What is the total weight of the…
- Bellman–Ford from S relaxes the directed edges in this fixed order in every pass: C→D (1), B→C (2), A→B (3), S→A (4). How many passes are…