Data Structures & AlgorithmsHardProduct-company MCQ pattern
Kahn's algorithm is run on the DAG with edges 5→2, 5→0, 4→0, 4→1, 2→3, 3→1, always removing the SMALLEST-numbered vertex of in-degree 0 (a min-heap instead of a queue). What order is produced?
- A. 4 5 2 0 3 1
- B. 0 1 2 3 4 5
- C. 4 5 0 2 3 1
- D. 5 4 2 3 1 0
Correct answer: C. 4 5 0 2 3 1
Start: in-degree 0 are 4 and 5, take 4 (0 still needs 5, and 1 needs 3). Take 5, which frees 0 and 2; take 0, then 2, which frees 3; then 3, which frees 1. 5 4 2 3 1 0 is another valid topological order, but not the one this rule produces.
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
- How many topological orderings does the DAG with edges A→B, A→C, B→D, C→D, D→E, B→F have?
- Prim's algorithm starts at A on the undirected graph with edges A–B (3), A–C (1), B–C (7), B–D (5), C–D (2), C–E (7), D–E (3), D–F (8)…
- An undirected graph on A, B, C, D has edges A–B, B–C, C–D, D–A of weight 1 and a diagonal A–C of weight 2. How many distinct minimum…
- Union-find on elements 0–7 uses union by size (the smaller tree's root points to the larger tree's root; on a tie the second root points…
- Seven distinct keys are stored in a max-heap in a 0-indexed array. Which statement is correct?
- How many of these statements are correct? (i) Building a heap from n elements by bottom-up sift-down takes O(n) time. (ii) Inserting n…