Data Structures & AlgorithmsVery hardGATE CS · Advanced
A disjoint-set forest on elements 1..10 starts with every element as its own root. Union(a, b) finds both roots (find uses full path compression) and, if they differ, attaches the root of the smaller set below the root of the larger one; on a tie the root of b's set goes below the root of a's set. The operations, in order, are: U(1,2), U(3,4), U(2,4), U(5,6), U(7,8), U(6,8), Find(4), U(5,1), U(9,10), Find(8), U(9,3). What is the final parent array for elements 1 to 10?
- A. [5, 1, 1, 3, 5, 5, 5, 7, 5, 9]
- B. [5, 1, 5, 1, 5, 5, 5, 5, 5, 9]
- C. [5, 1, 5, 1, 9, 5, 5, 5, 9, 9]
- D. [4, 4, 4, 4, 8, 8, 8, 4, 10, 4]
Correct answer: B. [5, 1, 5, 1, 5, 5, 5, 5, 5, 9]
Track roots and sizes: ties always hang b's root under a's root, and each Find compresses the whole path to the root. Without compression, with the opposite tie-break or with naive linking, one or more entries differ.
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
- A tree on the vertices A–J has the edges A–B, B–C, C–D, A–E, D–F, B–G, F–H, A–I, C–J. What is the sum of the distances over all unordered…
- A directed acyclic graph has the vertices A–G and the arcs D→F, D→C, D→B, G→C, G→A, C→A, C→E, C→B. How many different topological…
- In a bipartite graph the left vertices are L1–L6 and the right vertices are R1–R6, with the edges L1–R3, L1–R5, L1–R6, L2–R3, L3–R2…
- Dijkstra's algorithm (a vertex is settled when removed from the priority queue and its distance is never changed afterwards) is run from A…
- A directed graph on the vertices A–E has the arcs C→B, C→A, B→C, B→E, B→A, D→E, E→B, E→A, A→C, A→B, A→D. Every vertex has equal in-degree…
- Every node of a binary tree has either 0 or 2 children. Its preorder traversal is W S M Q R P K Z T and its postorder traversal is Q R M K…