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)?
- A. 20
- B. 24
- C. 16
- 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).
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- A 3-ary max-heap with 100 elements is stored in a 0-indexed array (the children of i are 3i + 1, 3i + 2, 3i + 3). How many leaves does it…
- Build-heap (bottom-up, max-heap) is run on [3, 9, 2, 1, 4, 5, 8, 7, 6, 10, 12, 11]. In each sift-down step the larger of the element and…
- A dynamic array starts with capacity 4. When an append finds it full, it allocates capacity c + c / 2 (integer division), copies every…
- The words tea, ten, to, inn, in, i, ted, tent, tenant, tens, ton are inserted into an initially empty trie (one node per character, root…
- An array A[-2..5][1..6][0..3] of 4-byte elements has base address 1000 (address of A[-2][1][0]). What is the address of A[3][4][2] in…
- Only the non-zero band of a 20 x 20 tridiagonal matrix (entries with |i - j| <= 1, rows and columns numbered from 1) is stored in a 1-D…