GATE Computer ScienceVery hardGATE CS (PYQ pattern)

Union-find on elements 1..12 (initially separate) uses union by size (the root of the larger set becomes the parent; on a tie the root of the first argument stays root) and find with full path compression. After union(1,2), (3,4), (1,3), (5,6), (7,8), (5,7), (1,5), (9,10), (11,12), (9,11), (9,1), (4,12) are performed, how many parent-pointer steps are followed in total while locating roots (counting each hop from a node to its parent, once per find, before compression)?

  1. A. 20
  2. B. 24
  3. C. 16
  4. D. 29

Correct answer: A. 20

Simulating each union's two finds with the stated rules gives 20 hops; at the end all 12 elements are in one set (components = 1).

More Programming and Data Structures (GATE) questions

Exams that ask this topic