The Wayback Machine - https://web.archive.org/web/20210106170122/https://www.geeksforgeeks.org/number-of-connected-components-of-a-graph-using-disjoint-set-union/
Related Articles

Related Articles

Number of connected components of a graph ( using Disjoint Set Union )
  • Last Updated : 06 Jan, 2021

Given an undirected graph G with vertices numbered in the range [0, N] and an array Edges[][] consisting of M edges, the task is to find the total number of connected components in the graph using Disjoint Set Union algorithm.

Examples:

Input: N = 4, Edges[][] = {{1, 0}, {2, 3}, {3, 4}}
Output: 2
Explanation: There are only 2 connected components as shown below:

Input: N = 4, Edges[][] = {{1, 0}, {0, 2}, {3, 5}, {3, 4}, {6, 7}}
Output: 2
Explanation: There are only 3 connected components as shown below:



Approach: The problem can be solved using Disjoint Set Union algorithm. Follow the steps below to solve the problem:

  • In DSU algorithm, there are two main functions, i.e. connect() and root() function.
  • connect(): Connects an edge.
  • root(): Recursively determine the topmost parent of a given edge.
  • For each edge {a, b}, check if a is connected to b or not. If found to be false, connect them by appending their top parents.
  • After completing the above step for every edge, print the total number of the distinct top-most parents for each vertex.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
  
#include <bits/stdc++.h>
using namespace std;
  
// Stores the parent of each vertex
int parent[1000000];
  
// Function to find the topmost
// parent of vertex a
int root(int a)
{
    // If current vertex is
    // the topmost vertex
    if (a == parent[a]) {
        return a;
    }
  
    // Otherwsie, set topmost vertex of
    // its parent as its topmost vertex
    return parent[a] = root(parent[a]);
}
  
// Function to connect the component
// having vertex a with the component
// having vertex b
void connect(int a, int b)
{
    // Connect edges
    a = root(a);
    b = root(b);
  
    if (a != b) {
        parent[b] = a;
    }
}
  
// Function to find unique top most parents
void connectedComponents(int n)
{
    set<int> s;
  
    // Traverse all vertices
    for (int i = 0; i < n; i++) {
  
        // Insert all topmost
        // vertices obtained
        s.insert(parent[i]);
    }
  
    // Print count of connected components
    cout << s.size() << '\n';
}
  
// Function to print answer
void printAnswer(int N,
                 vector<vector<int> > edges)
{
  
    // Setting parent to itself
    for (int i = 0; i <= N; i++) {
        parent[i] = i;
    }
  
    // Traverse all edges
    for (int i = 0; i < edges.size(); i++) {
        connect(edges[i][0], edges[i][1]);
    }
  
    // Print answer
    connectedComponents(N);
}
  
// Driver Code
int main()
{
    // Given N
    int N = 8;
  
    // Given edges
    vector<vector<int> > edges = {
        { 1, 0 }, { 0, 2 }, { 5, 3 }, { 3, 4 }, { 6, 7 }
    };
  
    // Function call
    printAnswer(N, edges);
  
    return 0;
}

chevron_right


Output:

3

Time Complexity: O(N+M)
Auxiliary Space: O(N+M)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :