Foundations
Linear Structures
Hashing
Array and String Patterns
Recursion, Search, and Sorting
Problem Solving Patterns
Algorithm Searching
Sorting Algorithms
Array Algorithms
Grid Algorithms
Linear Structure Algorithms
Hashing Algorithms
Recursion and Backtracking
Greedy Algorithms
Tree and Heap Algorithms
Graph Algorithms
Dynamic Programming Algorithms
String Algorithms
Math Algorithms
Keyboard ready
Space plays. Arrow keys step. R restarts.
LAB Union Find Lab
Graphs
Problem shape
Intermediate
A
B
C
D
E
Core idea
Track disjoint sets with parent pointers, path compression, and union by rank.
Step 1 / 5
Problem shape
Graphs Intermediate
Union Find
Welcome to the Union Find algorithm lab. Track disjoint sets with parent pointers, path compression, and union by rank. Use it when: Use for connectivity, cycle detection, and minimum spanning tree algorithms. The time complexity is Almost O(1), and the space complexity is O(n). The pseudocode is: find(x) if parent[x] != x parent[x] = find(parent[x]) return parent[x] union(a, b) attach one root to the other As you practice, track the state that changes and the condition that makes the algorithm stop.
Time
Almost O(1)
Space
O(n)
Pseudocode
find(x)
if parent[x] != x
parent[x] = find(parent[x])
return parent[x]
union(a, b)
attach one root to the other 1.0x