Union-Find or DFS which one is better to find connected component?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
In the realm of graph theory, understanding the problem of finding connected components is essential. Two prominent methods used to solve this problem are Union-Find (also known as Disjoint Set Union, DSU) and Depth-First Search (DFS). Both have their advantages and specific use cases. This article delves into the workings of each method, comparing their strengths and weaknesses to determine which might be better suited for finding connected components.
Understanding Connected Components
Before diving into algorithms, it's pivotal to understand what a connected component is. In an undirected graph, a connected component is a subgraph in which any two vertices are connected to each other by paths. These subgraphs are maximally connected, meaning adding any other vertices or edges will result in them no longer being a component of the original graph.
Union-Find Approach
Basics of Union-Find
Union-Find is a data structure that keeps track of elements partitioned into a number of non-overlapping (disjoint) subsets. Its primary operations are:
- Find: Determine which subset a particular element is in. This can be used for determining if two elements are in the same subset.
- Union: Join two subsets into a single subset.
Implementation Details
Union-Find is typically implemented using two techniques to optimize its operations:
- Path Compression: This technique helps flatten the structure of the tree whenever
Findis called. In doing so, it makes future operations faster by reducing the distance of nodes to the root. - Union by Rank/Size: This keeps the tree flatter by attaching the smaller tree under the root of the larger tree during
Unionoperations. - Efficient in scenarios where frequent union and find operations are needed.
- Particularly beneficial in dynamic connectivity problems where the graph structure changes over time.
- Offers near-constant time complexity for both operations with optimizations (i.e., , where is the inverse Ackermann function).
- Utilized in static graphs where the graph structure is known and fixed.
- Simplicity and straightforward implementation make DFS a go-to for quick solutions.
- Time complexity is where is the number of vertices and is the number of edges.
Related reading
- Union of multiple K-minimum values sets of different sizes in the KMV algorithm
- Union of two network diagrams
- Union/find algorithm without union by rank for disjoint-set forests data structure
- Unique non-repeating random numbers in O1?
- Union of dict objects in Python
- unique for arrays in JavaScript
- Unique random number generation in an integer array
- Uniqueness of Inorder, Preorder, and Postorder traversal with null elements

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.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.