How to Find Connected Components in Graph

In this article, we are going to find connected components in a graph by building a program in C++ language. We are going to use different approaches to build the program for finding the connected components in the graph. We are also going to calculate the time complexity and the space complexity for the program.

Approach 1: Using Depth First Search (DFS)

An undirected graph's connected component search is simpler. In order to obtain all strongly connected components, the concept is to perform DFS starting from each unexplored vertices.

To put the concept into practice using DFS, we should follow the below mentioned steps:

  • Set each vertex's initial state to unvisited.
  • Follow these steps for each vertex v.
  • Call the DFS and print the newline character to print each component in a new line if v has never been visited before.
  • Print out v and mark it as visited.
  • If a neighboring u of v is not visited, recursively call the DFS for that u.

Example 1:

// C++ program to print connected components in
// an undirected graph
#include
using namespace std;
// Graph class represents a undirected graph
// using adjacency list representation
class Graph {
	int V; // No. of vertices


	// Pointer to an array containing adjacency lists
	list* adj;


	// A function used by DFS
	void DFSUtil(int v, bool visited[]);


public:
	Graph(int V); // Constructor
	~Graph();
	void addEdge(int v, int w);
	void connectedComponents();
};


// Method to print connected components in an
// undirected graph
void Graph::connectedComponents()
{
	// Mark all the vertices as not visited
	bool* visited = new bool[V];
	for (int v = 0; v < V; v++)
		visited[v] = false;


	for (int v = 0; v < V; v++) {
		if (visited[v] == false) {
			// print all reachable vertices
			// from v
			DFSUtil(v, visited);


			cout << "\n";
		}
	}
	delete[] visited;
}


void Graph::DFSUtil(int v, bool visited[])
{
	// Mark the current node as visited and print it
	visited[v] = true;
	cout << v << " ";


	// Recur for all the vertices
	// adjacent to this vertex
	list::iterator i;
	for (i = adj[v].begin(); i != adj[v].end(); ++i)
		if (!visited[*i])
			DFSUtil(*i, visited);
}


Graph::Graph(int V)
{
	this->V = V;
	adj = new list[V];
}


Graph::~Graph() { delete[] adj; }


// method to add an undirected edge
void Graph::addEdge(int v, int w)
{
	adj[v].push_back(w);
	adj[w].push_back(v);
}


// Driver code
int main()
{
	// Create a graph given in the above diagram
	Graph g(5); // 5 vertices numbered from 0 to 4
	g.addEdge(1, 0);
	g.addEdge(2, 1);
	g.addEdge(3, 4);


	cout << "Following are connected components \n";
	g.connectedComponents();


	return 0;
}

Output:

Following are connected components
0 1 2
3 4

Time complexity for the following program is - O(V+E).

Space complexity for the following program is - O(V).

Approach 2: Using Disjoint Set Union

In this approach, we first declare each node as a separate subset before visiting each one in order to tackle the problem using DSU (Disjoint Set Union). Join the under with any newly discovered unexplored nodes. In this way, each traversal will only visit one component.

To put the concept into practice, simply follow the below mentioned steps:

  • Declare an array of size V called arr[], where V represents the total number of nodes.
  • The value indicates who the parent of the ith vertex is for each index I of the array arr[].
  • Every node should be created as its own parent, and as they are added together, their parents should be adjusted as necessary.
  • From node 0 through node V, go around them:
  • Start the DSU for each node that is its own parent.
  • Print each node in the disjoint set as though it were a separate component.

Example 2:

#include <bits/stdc++.h>
using namespace std;


int merge(int* parent, int x)
{
    if (parent[x] == x)
        return x;
    return merge(parent, parent[x]);
}


int connectedcomponents(int n, vector >& edges)
{
    int parent[n];
    for (int i = 0; i < n; i++) {
        parent[i] = i;
    }
    for (auto x : edges) {
        parent[merge(parent, x[0])] = merge(parent, x[1]);
    }
    int ans = 0;
    for (int i = 0; i < n; i++) {
        ans += (parent[i] == i);
    }
    for (int i = 0; i < n; i++) {
        parent[i] = merge(parent, parent[i]);
    }
    map > m;
    for (int i = 0; i < n; i++) {
        m[parent[i]].push_back(i);
    }
    for (auto it = m.begin(); it != m.end(); it++) {
        list l = it->second;
        for (auto x : l) {
            cout << x << " ";
        }
        cout << endl;
    }
    return ans;
}


int main()
{
    int n = 5;
    vector e1 = { 0, 1 };
    vector e2 = { 2, 1 };
    vector e3 = { 3, 4 };
    vector > e;
    e.push_back(e1);
    e.push_back(e2);
    e.push_back(e3);


    cout << "Following are connected components:\n";
    int a = connectedcomponents(n, e);
    return 0;
}

Output:

Following are connected components
0 1 2
3 4

Time complexity for the program is - O(V)

Space complexity for the program is - O(V)


Related Topics

How to unarchive in Gmail?

How to unarchive in Gmail Earlier, we have learned how to archive an email on a computer or a phone device via the Google app. But archiving is not sufficient; you...

3 minutes read.

How to convert epoch date to time?

How to convert epoch date to time? The concept of the epoch is vital for computation purposes. Epoch is nothing but the total milliseconds passed from 1st January 1970. This concept...

3 minutes read.

How to select all in Gmail

How to select all in Gmail Undoubtedly Gmail is one of the leading email service providers across the world. Teenagers, adults, old ages all rely upon Gmail because of its free...

3 minutes read.

How to delete a page in Microsoft Word?

What is Microsoft Word? Microsoft word is a word processing program, which is developed by Microsoft. It was initially launched on 25 October 1983 and has since been modified many times. Generally, Microsoft Word...

2 minutes read.

How to Cancel Netflix

How to Cancel Netflix Netflix Netflix is a service that lets its members watch a variety of television shows, movies, documentaries, and web series who purchase its subscription. You can enjoy unlimited ad-free...

3 minutes read.

What does RAM do on a computer?

What does RAM do on a computer? Many of us might have heard the word RAM in our daily life but do not know what it does except the fact that...

10 minutes read.

How to write a cheque?

How to write a cheque? In today's time, digital payments are consistently gaining more market share, but it's also important to know how to write a cheque. Many companies, employers, workers, and landlords still...

2 minutes read.

How to Find Divisors of a Number?

A number that equally divides a larger integer is referred to as a divisor or factor. By simply listing all the possible methods to multiply two numbers together to obtain...

3 minutes read.

How to take a screenshot on the iPhone?

How to take a screenshot on the iPhone? A screenshot is an excellent thing to quickly save your screen content, which is open on your screen. You can share your favorite videos, high scores...

4 minutes read.

How to defrag a computer?

How to defrag a computer Topics Covered What is Defragmentation?What is the need of defragmentation?Advantages of DefragmentationWhen to defrag your computerDefragmentation for your SSDDefrag your computer running on windows 10Defrag your computer...

8 minutes read.

How to take a Screenshot on computer?

How to take a Screenshot on computer The ScreenShot feature is widely used to procure the content of the screen as an image. This feature was introduced in the late ’90s,...

8 minutes read.

How to change the name in Gmail

How to change the name in Gmail Frequently asked questions: How I can change my Gmail nameSteps to change the name of Gmail accountStep to change to the name of Gmail on...

3 minutes read.

How to mark all emails as read in Gmail?

How to mark all emails as read in Gmail Gmail is extensively used to exchange official documents, expressions, proposals around the world. In the business industry leaving unread mails is considered...

3 minutes read.

How to cancel amazon prime?

How to cancel amazon prime? Amazon Prime is a subscription that offers variety of services with it. The advantage of amazon prime is that you can expect hassle-free shipping within two days, access to...

3 minutes read.

How to delete Snapchat Account?

What is Snapchat? Snapchat is a social networking app, which is developed by Snap Inc. It is created by Even Spiegel, Bobby Murphy, and Reggie Brown. Snapchat was launched in September 2011. It is...

3 minutes read.

How to find your Computer’s specs?

How to find your computer’s specs The computer specs include the details for every single component in your computer, including your CPU, hard drive space, and even the little things like...

7 minutes read.

How to change Gmail Password?

How to change Gmail Password? Today Gmail account is linked to all your application and software unlike, Instagram, Facebook, and Skype, etc. You can log in to different platforms directly using...

6 minutes read.

How to snooze an email in Gmail

How to snooze an email in Gmail With the popularity of Gmail, today every deal is likely to be discussed in emails. And with so many things rushing in the Inbox,...

3 minutes read.

How to take a screenshot on windows

How to take a screenshot on windows A screenshot is a picture taken on your computer. In this section, we will learn how to take screenshot on windows operating system. We can take screenshot...

5 minutes read.

How to Create Folders in Gmail

How to Create Folders in Gmail Gmail is a leading email service provider and is used by 1.5 billion-plus active users worldwide. All Gmail professional users receive at least dozens of...

6 minutes read.