Consider a Union Find data structure initialized with 5 elements (0 to 4). After performing unions Union(0, 1), Union(1, 2), and Union(3, 4), what is the parent array?
int[] parent = {0, 1, 2, 3, 4}; int[] rank = {0, 0, 0, 0, 0}; void Union(int[] parent, int[] rank, int x, int y) { int rootX = Find(parent, x); int rootY = Find(parent, y); if (rootX != rootY) { if (rank[rootX] < rank[rootY]) { parent[rootX] = rootY; } else if (rank[rootX] > rank[rootY]) { parent[rootY] = rootX; } else { parent[rootY] = rootX; rank[rootX]++; } } } int Find(int[] parent, int x) { if (parent[x] != x) { parent[x] = Find(parent, parent[x]); } return parent[x]; } Union(parent, rank, 0, 1); Union(parent, rank, 1, 2); Union(parent, rank, 3, 4); Console.WriteLine(string.Join(",", parent));
Remember that union by rank attaches the smaller tree under the root of the larger tree.
After unions, elements 0,1,2 share root 0, and elements 3,4 share root 3. So parent array shows roots: [0,0,0,3,3].
Given the parent array [0, 0, 0, 3, 3] and rank array [2, 0, 0, 1, 0], what is the result of Find(2)?
int[] parent = {0, 0, 0, 3, 3}; int[] rank = {2, 0, 0, 1, 0}; int Find(int[] parent, int x) { if (parent[x] != x) { parent[x] = Find(parent, parent[x]); } return parent[x]; } int result = Find(parent, 2); Console.WriteLine(result);
Follow the parent links until the root is found.
Element 2's parent is 0, which is its own parent, so Find(2) returns 0.
What error does the following code produce when calling Union(0, 1)?
int[] parent = {0, 1, 2}; int[] rank = {0, 0, 0}; void Union(int[] parent, int[] rank, int x, int y) { int rootX = Find(parent, x); int rootY = Find(parent, y); if (rootX != rootY) { if (rank[rootX] < rank[rootY]) { parent[rootX] = rootY; } else if (rank[rootX] > rank[rootY]) { parent[rootY] = rootX; } else { parent[rootY] = rootX; rank[rootX]++; } } } int Find(int[] parent, int x) { if (parent[x] != x) { parent[x] = Find(parent, parent[x]); } return parent[x]; } Union(parent, rank, 0, 1);
Check which parent is assigned to which root in the union logic.
The union logic mistakenly assigns parent[rootX] = rootY when rank[rootX] < rank[rootY], which is reversed. It should assign parent[rootX] = rootY in that case.
Starting with 6 elements (0 to 5), after performing unions Union(0, 1), Union(2, 3), Union(4, 5), and Union(1, 2), how many disjoint sets remain?
int[] parent = {0, 1, 2, 3, 4, 5}; int[] rank = {0, 0, 0, 0, 0, 0}; void Union(int[] parent, int[] rank, int x, int y) { int rootX = Find(parent, x); int rootY = Find(parent, y); if (rootX != rootY) { if (rank[rootX] < rank[rootY]) { parent[rootX] = rootY; } else if (rank[rootX] > rank[rootY]) { parent[rootY] = rootX; } else { parent[rootY] = rootX; rank[rootX]++; } } } int Find(int[] parent, int x) { if (parent[x] != x) { parent[x] = Find(parent, parent[x]); } return parent[x]; } Union(parent, rank, 0, 1); Union(parent, rank, 2, 3); Union(parent, rank, 4, 5); Union(parent, rank, 1, 2); int count = 0; for (int i = 0; i < parent.Length; i++) { if (Find(parent, i) == i) count++; } Console.WriteLine(count);
Count how many elements are their own parent after all unions.
Unions connect sets {0,1,2,3} and {4,5}, so two disjoint sets remain.
Which option best explains the main benefit of path compression in the Union Find data structure?
Think about how Find operation speed improves after path compression.
Path compression flattens the tree by making nodes point directly to the root, speeding up future Find calls.
