Given an undirected graph **g**, the task is to print the number of connected components in the graph.

**Examples:**

Input:

Output:3

There are three connected components:

1 – 5, 0 – 2 – 4 and 3

**Approach:** The idea is to use a variable **count** to store the number of connected components and do the following steps:

- Initialize all vertices as unvisited.
- For all the vertices check if a vertex has not been visited, then perform DFS on that vertex and increment the variable
**count**by 1.

Below is the implementation of the above approach:

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Graph class represents a undirected graph ` `// using adjacency list representation ` `class` `Graph { ` ` ` `// No. of vertices ` ` ` `int` `V; ` ` ` ` ` `// Pointer to an array containing adjacency lists ` ` ` `list<` `int` `>* adj; ` ` ` ` ` `// A function used by DFS ` ` ` `void` `DFSUtil(` `int` `v, ` `bool` `visited[]); ` ` ` `public` `: ` ` ` `// Constructor ` ` ` `Graph(` `int` `V); ` ` ` ` ` `void` `addEdge(` `int` `v, ` `int` `w); ` ` ` `int` `NumberOfconnectedComponents(); ` `}; ` ` ` `// Function to return the number of ` `// connected components in an undirected graph ` `int` `Graph::NumberOfconnectedComponents() ` `{ ` ` ` ` ` `// Mark all the vertices as not visited ` ` ` `bool` `* visited = ` `new` `bool` `[V]; ` ` ` ` ` `// To store the number of connected components ` ` ` `int` `count = 0; ` ` ` `for` `(` `int` `v = 0; v < V; v++) ` ` ` `visited[v] = ` `false` `; ` ` ` ` ` `for` `(` `int` `v = 0; v < V; v++) { ` ` ` `if` `(visited[v] == ` `false` `) { ` ` ` `DFSUtil(v, visited); ` ` ` `count += 1; ` ` ` `} ` ` ` `} ` ` ` ` ` `return` `count; ` `} ` ` ` `void` `Graph::DFSUtil(` `int` `v, ` `bool` `visited[]) ` `{ ` ` ` ` ` `// Mark the current node as visited ` ` ` `visited[v] = ` `true` `; ` ` ` ` ` `// Recur for all the vertices ` ` ` `// adjacent to this vertex ` ` ` `list<` `int` `>::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<` `int` `>[V]; ` `} ` ` ` `// 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() ` `{ ` ` ` `Graph g(5); ` ` ` `g.addEdge(1, 0); ` ` ` `g.addEdge(2, 3); ` ` ` `g.addEdge(3, 4); ` ` ` ` ` `cout << g.NumberOfconnectedComponents(); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

2

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.

## Recommended Posts:

- Convert undirected connected graph to strongly connected directed graph
- Count of unique lengths of connected components for an undirected graph using STL
- Maximum number of edges among all connected components of an undirected graph
- Connected Components in an undirected graph
- Sum of the minimum elements in all connected components of an undirected graph
- Clone an undirected graph with multiple connected components
- Largest subarray sum of all connected components in undirected graph
- Maximum sum of values of nodes among all connected components of an undirected graph
- Number of single cycle components in an undirected graph
- Maximum number of edges to be removed to contain exactly K connected components in the Graph
- Maximum decimal equivalent possible among all connected components of a Binary Valued Graph
- Octal equivalents of connected components in Binary valued graph
- Cycles of length n in an undirected and connected graph
- Kth largest node among all directly connected nodes to the given node in an undirected graph
- Check if longest connected component forms a palindrome in undirected graph
- Queries to check if vertices X and Y are in the same Connected Component of an Undirected Graph
- Queries to count connected components after removal of a vertex from a Tree
- Number of connected components in a 2-D matrix of strings
- Check if the length of all connected components is a Fibonacci number
- Convert the undirected graph into directed graph such that there is no path of length greater than 1

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.