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?

  1. A. [5, 1, 1, 3, 5, 5, 5, 7, 5, 9]
  2. B. [5, 1, 5, 1, 5, 5, 5, 5, 5, 9]
  3. C. [5, 1, 5, 1, 9, 5, 5, 5, 9, 9]
  4. 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.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic