How to use Disjoint Sets in Connected Component labeling?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Connected component labeling assigns a unique label to each connected foreground region in a binary image. Disjoint sets, also called union-find, are useful because the first scan of the image often discovers that two temporary labels actually refer to the same component, and union-find lets you merge those equivalences efficiently.
Why Union-Find Fits Connected Components
When you scan a binary image row by row, a foreground pixel can encounter neighboring pixels that already have labels. Sometimes there is only one neighboring label, but sometimes two different labels both touch the current pixel and must later be recognized as the same component.
That is the exact job of a disjoint-set structure:
- '
findreturns the representative label of a set' - '
unionmerges two labels that refer to one component'
Two-Pass Labeling Strategy
The classic union-find CCL algorithm has two passes.
First Pass
For each foreground pixel:
- inspect already-visited neighbors
- assign a new label if no foreground neighbor exists
- reuse an existing label if one neighbor label exists
- assign one label and union the rest if multiple neighbor labels exist
Second Pass
Replace every temporary label with its root representative from the disjoint-set structure.
That second pass collapses equivalent provisional labels into one final component label.
A Small Python Example
This example uses 4-connectivity and labels 1 pixels in a binary image.
This example keeps the idea visible: assign temporary labels first, then resolve label equivalences through union-find.
Four-Connected Versus Eight-Connected
The neighbor rule changes the result.
- 4-connected usually checks north and west during the first pass
- 8-connected also considers north-west and north-east
The union-find structure does not change much. What changes is which previously visited neighbors you treat as connected.
Why Path Compression Helps
Without optimization, repeated find operations can become expensive as the equivalence structure grows. Path compression makes every later find flatter and faster. Combined with union heuristics, the algorithm becomes almost linear for practical image sizes.
That is why union-find is the standard textbook companion to two-pass component labeling.
Common Pitfalls
The most common mistake is forgetting to union all distinct neighboring labels when a pixel touches multiple provisional components. If you skip that, the second pass cannot collapse them correctly.
Another mistake is mixing 4-connectivity logic with 8-connectivity expectations. The neighborhood rule changes which components merge.
A third issue is trying to finalize labels during the first pass without recording equivalences. That usually produces duplicate labels for one component.
Summary
- Connected component labeling often needs temporary labels and later equivalence merging.
- Disjoint sets provide efficient
findandunionoperations for that equivalence management. - The standard approach is a first pass for provisional labels and a second pass for canonical labels.
- Path compression makes the union-find structure efficient in practice.
- Choose 4-connectivity or 8-connectivity deliberately because it changes the labeling result.
Related reading
- How to use Haar wavelet to detect LINES on an image?
- How to use UIVisualEffectView to Blur Image?
- How would I increase my accuracy in the cifar-100 dataset? I have a 10 accuracy at the moment
- How would I tint an image programmatically on iOS?
- How to use lower_boundupper_bound to find position of any number in array?
- How to use recursion in creating a binary search algorithm
- How to use fn_map to map each row in an array C to its coresponding one in the array B
- How to use freeze_graph.py tool in TensorFlow v1

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.