0
0
DSA Cprogramming~10 mins

Union Find Disjoint Set Data Structure in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Union Find Disjoint Set Data Structure
  [Initialize each element as its own set]
            |
            v
  [Find root of element A] ←─────┐
            |                   |
            v                   |
  [Find root of element B]      |
            |                   |
            v                   |
  [Are roots different?] --No--> [Stop]
            |
           Yes
            |
            v
  [Union sets by linking roots]
            |
            v
  [Update parent pointer]
            |
            v
          [Done]
Start with each element alone. To union, find roots of both sets. If different, link one root to the other. This merges sets.
Execution Sample
DSA C
int find(int x) {
  if(parent[x] != x) parent[x] = find(parent[x]);
  return parent[x];
}

void union_set(int a, int b) {
  a = find(a);
  b = find(b);
  if(a != b) parent[b] = a;
}
This code finds the root of a set with path compression and unions two sets by linking roots.
Execution Table
StepOperationNodes in SetsPointer ChangesVisual State
1Initialize sets for elements 1,2,3,41,2,3,4 each aloneparent[1]=1, parent[2]=2, parent[3]=3, parent[4]=4┌──────┐ ┌──────┐ ┌──────┐ ┌──────┐ │ 1 │ │ 2 │ │ 3 │ │ 4 │ │ next:│ │ next:│ │ next:│ │ next:│ │ 1 │ │ 2 │ │ 3 │ │ 4 │ └──────┘ └──────┘ └──────┘ └──────┘
2Union(1, 2): find roots1,2,3,4 each alonefind(1)=1, find(2)=2Same as step 1
3Union(1, 2): link root 2 to root 1Sets: {1,2}, {3}, {4}parent[2] = 1┌──────┐ ┌──────┐ ┌──────┐ ┌──────┐ │ 1 │ ──→│ 2 │ │ 3 │ │ 4 │ │ next:│ │ next:│ │ next:│ │ next:│ │ 1 │ │ 1 │ │ 3 │ │ 4 │ └──────┘ └──────┘ └──────┘ └──────┘
4Union(3, 4): find rootsSets: {1,2}, {3}, {4}find(3)=3, find(4)=4Same as step 3
5Union(3, 4): link root 4 to root 3Sets: {1,2}, {3,4}parent[4] = 3┌──────┐ ┌──────┐ ┌──────┐ ┌──────┐ │ 1 │ ──→│ 2 │ │ 3 │ ──→│ 4 │ │ next:│ │ 1 │ │ next:│ │ 3 │ │ 1 │ └──────┘ └──────┘ └──────┘ └──────┘
6Union(2, 3): find roots with path compressionSets: {1,2}, {3,4}find(2): parent[2]=1, find(1)=1 find(3)=3Same as step 5
7Union(2, 3): link root 3 to root 1Sets: {1,2,3,4}parent[3] = 1┌──────┐ ┌──────┐ ┌──────┐ ┌──────┐ │ 1 │ ──→│ 2 │ │ 3 │ │ 4 │ │ next:│ │ 1 │ │ next:│ │ next:│ │ 1 │ └──────┘ │ 1 │ │ 3 │ └──────┘ └──────┘ └──────┘
8Find(4): path compression updates parent[4]Sets: {1,2,3,4}find(4): parent[4]=3, find(3)=1, update parent[4]=1┌──────┐ ┌──────┐ ┌──────┐ ┌──────┐ │ 1 │ ──→│ 2 │ │ 3 │ │ 4 │ │ next:│ │ 1 │ │ next:│ │ next:│ │ 1 │ └──────┘ │ 1 │ │ 1 │ └──────┘ └──────┘ └──────┘
9Find(4) again: direct parent is 1Sets: {1,2,3,4}find(4)=1Same as step 8
10Stop: all elements connected in one setSets: {1,2,3,4}No pointer changesSame as step 9
💡 All elements connected in one set; no further unions needed.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6After 7After 8Final
parent[1]1111111111
parent[2]2211111111
parent[3]3333333111
parent[4]4444333311
Key Moments - 3 Insights
Why do we update parent pointers during find (path compression)?
Updating parent pointers during find (see step 8 in execution_table) flattens the tree, making future finds faster by pointing nodes directly to the root.
What happens if we try to union two elements already in the same set?
If roots are the same (step 6 shows roots 1 and 3 differ, but if same, union stops), no pointer changes occur because they are already connected.
Why do we link one root to another instead of merging all nodes directly?
Linking roots (step 3, 5, 7) keeps the structure simple and efficient, avoiding complex pointer updates for all nodes.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, which parent pointer changes?
Aparent[3] = 1
Bparent[1] = 2
Cparent[2] = 1
Dparent[4] = 3
💡 Hint
Check the 'Pointer Changes' column in step 3 of execution_table.
At which step does element 4's parent pointer first change?
AStep 7
BStep 5
CStep 8
DStep 3
💡 Hint
Look at 'Pointer Changes' and 'Visual State' columns for parent[4] updates.
If we skip path compression in find, how would the variable_tracker change after step 8?
Aparent[4] would remain 3
Bparent[4] would be 1
Cparent[3] would change to 4
Dparent[2] would change to 3
💡 Hint
Check how path compression updates parent[4] in step 8 in variable_tracker.
Concept Snapshot
Union Find Disjoint Set:
- Each element starts as its own set (parent points to itself).
- find(x) returns root parent with path compression.
- union(a,b) links roots if different.
- Path compression flattens tree for fast future finds.
- Efficient for connectivity queries and merges.
Full Transcript
Union Find Disjoint Set Data Structure starts by making each element its own set with parent pointers to itself. To union two elements, we find their root parents using the find function, which also compresses paths by updating parent pointers directly to the root. If roots differ, union links one root to the other, merging sets. This process is efficient for checking connectivity and merging sets. The execution trace shows initialization, unions of pairs, and path compression updating parent pointers to flatten the structure for faster future queries.