IntermediateAbout 20 min
Union-Find (Disjoint Set)
Read the full article on Substack
6 min read.
Master the Union-Find data structure. Essential for Kruskal's Algorithm, connected components, and cycle detection in undirected graphs.
Think of it this way: Think of social networks. When person A befriends person B, their entire groups of friends merge into one giant community. You can quickly check if two random people are in the same extended network.
8 separate sets: every node is its own root.
1/36
Check your understanding
Answer from memory before you scroll back up. Pulling an idea out of your head is what makes it stay there.
union(x, y)
Sequence
Order the steps of union by rank with path compression.
Tap a piece, then tap where it goes — or drag it. Tap the tray to take one back.
- 1Drop a step here
- 2Drop a step here
- 3Drop a step here
- 4Drop a step here
4 pieces left to place
Quiz
20 questionsQuestion 1 of 20
Score: 0
What is the primary optimization that makes Union-Find nearly constant time?
Done with this lesson?
Sign in free to keep your progress, streak and quiz scores.