If I have an undirected graph (implemented as a list of vertices), how can I find its connected components? How can I use quick-union?
Asked
Active
Viewed 5.9k times
46
-
The vertices are represented as a list, but how are the edges represented? – Ted Hopp Jan 12 '14 at 18:17
-
Graph G is a list of lists of integers. There is edge form i to j iff j is on the list G[i] and i on G[j]. – abalcerek Jan 12 '14 at 18:20
-
1This question appears to be off-topic because it is about computer science, not programming, and belongs on http://cs.stackexchange.com/ – Ted Hopp Jan 12 '14 at 18:20
-
@user52045 i have answered this question because you seem new to SO , but nowonwards you should also post about what you tried . – Aseem Goyal Jan 12 '14 at 18:46
-
8How is this question too broad? – Fermat's Little Student Feb 28 '15 at 15:06
2 Answers
46
Use depth-first search (DFS) to mark all individual connected components as visited:
dfs(node u)
for each node v connected to u :
if v is not visited :
visited[v] = true
dfs(v)
for each node u:
if u is not visited :
visited[u] = true
connected_component += 1
dfs(u)
The best way is to use this straightforward method which is linear time O(n).
Since you asked about the union-find algorithm:
for each node parent[node] = node
for each node u :
for each node v connected to u :
if findset(u)!=findset(v) :
union(u,v)
**I assume you know about how findset and union works **
for each node if (parent[node] == node)
connected_component += 1

Quentin Pradet
- 4,691
- 2
- 29
- 41

Aseem Goyal
- 2,683
- 3
- 31
- 48
-
9
-
2@Will : yes, since BFS covers the graph in "layers" starting from the node `X` you picked at first, means that if some node `Y` is not reachable from `X`, then BFS won't visit it. While DFS in such scenario will do, why? because when you cover all the reachable nodes from `X` and there still some unvisited nodes, then DFS will start over from one of those nodes, thus DFS gives you a forest while BFS a single tree. – ThunderWiring May 31 '16 at 15:11
-
4@ThunderWiring I'm not sure I understand. What's stopping us from running BFS from one of those unvisited/undiscovered nodes? There seems to be nothing in the definition of DFS that necessitates running it for every undiscovered node in the graph. If you run either BFS or DFS on each undiscovered node you'll get a forest of connected components. – Hunan Rostomyan Nov 10 '16 at 06:50
-
@HunanRostomyan nothing stops you from running BFS from those nodes, what i meant is that BFS simply cannot do this on his own in one run of the algorithm while DFS can, please take a look here to see the *right* DFS implementation https://en.wikipedia.org/wiki/Depth-first_search#Pseudocode notice that the stake contains all the vertices `v` in the graph. – ThunderWiring Nov 10 '16 at 15:00
-
@Aseem Goyal, notice that there is no halt condition in your recursive `dfs` which makes your implementation incorrect. – ThunderWiring Nov 10 '16 at 15:01
-
@ThunderWiring What makes you think the stack contains *all* vertices in the graph? The code simply deals with the nodes (w) adjacent to the source node (v), not *all* nodes in G. Take a closer look at line 3 in the recursive and line 8 in the interative implementation in the link you posted. – Hunan Rostomyan Nov 10 '16 at 16:23
-
2@ThunderWiring As regards your claim that Aseem's algorithm is incorrect: you don't need an explicit base case (what you call a "halt condition") to get out of recursion, because the for loop (line 2 above) will eventually terminate (since a given node has finitely many nodes connected to it). Once all of the unvisited neighbors are `dfs`'d, the top level `dfs` calls will all bottom out. – Hunan Rostomyan Nov 11 '16 at 04:11
-
I just implemented your algorithm in Python: http://stackoverflow.com/q/42152900/4014959 – PM 2Ring Feb 10 '17 at 10:17
-
3@ThunderWiring I do not agree with your statement "that if some node Y is not reachable from X, then BFS won't visit it. While DFS in such scenario will do.." If a node is not reachable from X none of BFS or DFS can give you that node, if you are starting from X. Can you give an example of what you are saying? – Barshan Das Jan 06 '19 at 13:46
1
For every edge(u,v)
find union(u,v)
using quick union-find datastructure and find set of each vertex using find(v)
. Each new set is a connected component in the graph

Vikram Bhat
- 6,106
- 3
- 20
- 19