GATE Computer ScienceVery hardGATE CS (PYQ pattern)
Kahn's topological sort uses a FIFO queue seeded with all vertices of in-degree 0 in increasing order; vertices are dequeued, and out-neighbours (in increasing order) are enqueued when their in-degree reaches 0. The DAG has edges 1->4, 2->4, 2->6, 3->1, 3->5, 4->7, 5->7, 5->8, 6->8, 7->9, 8->9. What is the output order and the maximum queue length?
- A. 1 2 3 4 5 6 7 8 9; max 3
- B. 2 3 6 1 5 4 8 7 9; max 2
- C. 9 7 8 4 5 1 6 3 2; max 4
- D. 2 3 6 1 5 4 8 7 9; max 3
Correct answer: D. 2 3 6 1 5 4 8 7 9; max 3
Initially the queue is 2 3 (in-degree 0); vertices become free as their predecessors are processed. FIFO processing gives the order shown, and the queue peaks at 3.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- An undirected graph has edges 1-2, 2-3, 3-4, 4-5, 5-6, 6-1, 2-7, 7-8, 8-9, 9-7, 4-10, 10-11. Vertices are coloured by the parity of their…
- int range(Node n, int lo, int hi) { if (!n) return 0; int c = (lo <= n->k && n->k <= hi); if (lo < n->k) c += range(n->l, lo, hi); if…
- Keys 50, 30, 70, 20, 40, 60, 80, 10, 25, 35, 45, 55, 65, 75, 85, 5, 22, 33, 38 are inserted in this order into an empty BST, and every…
- bool f(Node n) { if (!n) return true; if (n->l && n->l->k > n->k) return false; if (n->r && n->r->k < n->k) return false; return f(n->l)…
- In the BST obtained by inserting 40, 20, 60, 10, 30, 50, 5, 15, 3, 1, 25, 55, 57, 56 in order, how many nodes have a balance factor…
- A binary tree is stored in level order with null children written as : 1 2 3 4 5 6 7 8 (a null child still consumes a slot only if its…