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
Current lesson

Union Find Lab

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
Open deeper structure lesson
1.0x