Given an undirected graph, count the number of connected components.
C++
// 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;
}
Also in C++:
- Title
- how to make loop in c++
- Category
- C++
- Title
- how to type cast quotient of two integers to double with c++
- Category
- C++
- Title
- c++ main function
- Category
- C++
- Title
- C++ If
- Category
- C++
- Title
- opperanf >> c++
- Category
- C++
- Title
- primeros numeros primos
- Category
- C++
- Title
- c++ base 10 to base 2
- Category
- C++
- Title
- type id c++
- Category
- C++
- Title
- double ended queue in c++ stl
- Category
- C++
- Title
- error: invalid conversion from 'Node*' to 'int'
- Category
- C++
- Title
- c++ initialize a vector
- Category
- C++
- Title
- self in c++
- Category
- C++
- Title
- find height of a tree
- Category
- C++
- Title
- c++ remove text file
- Category
- C++
- Title
- c++ pointers
- Category
- C++
- Title
- subarray sum in c++
- Category
- C++
- Title
- heap in cpp stl
- Category
- C++
- Title
- pop_back
- Category
- C++
- Title
- preorder traversal c++
- Category
- C++
- Title
- c++ switch case statement
- Category
- C++
- Title
- opencv compile c++
- Category
- C++
- Title
- how to swap string characters in c++
- Category
- C++
- Title
- c++ function return array
- Category
- C++
- Title
- c++ for loop syntax
- Category
- C++
- Title
- convert int to string c++
- Category
- C++
- Title
- file objects in c++
- Category
- C++
- Title
- c++ function return pointer to itself
- Category
- C++
- Title
- two elements with difference K in c++
- Category
- C++
- Title
- initialising 2d vector
- Category
- C++
- Title
- append string to another string c++
- Category
- C++
- Title
- namespace c++
- Category
- C++
- Title
- how to iterate over unordered_map c++
- Category
- C++
- Title
- first prime numbers
- Category
- C++
- Title
- program to know if a number is prime
- Category
- C++
- Title
- c++ reset stream
- Category
- C++
- Title
- : error: ‘cont’ cannot be used as a function return (cont(cont-1))/2;
- Category
- C++
- Title
- c++ server service ros
- Category
- C++
- Title
- comparing strings c++
- Category
- C++
- Title
- pause the console c++
- Category
- C++
- Title
- pair in c++
- Category
- C++
- Title
- C++ string format ctime
- Category
- C++
- Title
- c++ do while loop
- Category
- C++
- Title
- how to iterate trough a vector in c++
- Category
- C++
- Title
- how to turn int into string c++
- Category
- C++
- Title
- trovare il valore massimo in un array c++ w3
- Category
- C++
- Title
- how to round to nearest whole number unity
- Category
- C++
- Title
- *min_element in c++
- Category
- C++
- Title
- calculate factorial
- Category
- C++
- Title
- how to create a vector in c++
- Category
- C++
- Title
- how to declare a vector in c++
- Category
- C++
- Title
- function declerations in C++
- Category
- C++
- Title
- how to inject a dll into a game c++
- Category
- C++
- Title
- min heap declaration in c++ stl
- Category
- C++
- Title
- never gonna give you up
- Category
- C++
- Title
- first fit algorithm
- Category
- C++
- Title
- c++ vector pop_back
- Category
- C++
- Title
- quick sort predefined function in c++
- Category
- C++
- Title
- c++ code to print hello world
- Category
- C++
- Title
- c++ ternary operator
- Category
- C++
- Title
- how to get os name in c++
- Category
- C++
- Title
- assegnare valori in c++
- Category
- C++
- Title
- c++ loop through array
- Category
- C++
- Title
- binary tree deletion
- Category
- C++
- Title
- iterate through unordered_map c++ in reverse order
- Category
- C++
- Title
- Read multiple files(.txt) c++
- Category
- C++
- Title
- error: redefinition of ‘class Customer’
- Category
- C++
- Title
- how to find the mode of a vector c++
- Category
- C++
- Title
- sort function in cpp
- Category
- C++
- Title
- check if key exists in map c++
- Category
- C++
- Title
- system("pause") note working c++
- Category
- C++
- Title
- lopping over an array c++
- Category
- C++
- Title
- COnvert string to char * C++
- Category
- C++
- Title
- c++ formatting
- Category
- C++
- Title
- how to get input from the console in c++
- Category
- C++
- Title
- c++ vector add element
- Category
- C++
- Title
- power c++
- Category
- C++
- Title
- c++ append to list
- Category
- C++
- Title
- how to compare two strings lexicographically in c++
- Category
- C++
- Title
- what does map.count() return in c++
- Category
- C++
- Title
- stringstream in c++ with delimiter
- Category
- C++
- Title
- arrays in C++
- Category
- C++
- Title
- factorion
- Category
- C++
- Title
- pbds in c++
- Category
- C++
- Title
- SFML window
- Category
- C++
- Title
- msdn parse command line
- Category
- C++
- Title
- vector initialization c++
- Category
- C++
- Title
- how initilaize deffult value to c++ class
- Category
- C++
- Title
- c++ method name
- Category
- C++
- Title
- differentialble programming
- Category
- C++
- Title
- input a string in c++
- Category
- C++
- Title
- chess perft 5
- Category
- C++
- Title
- simple timer arduino blynk library error
- Category
- C++
- Title
- findung the mode in c++
- Category
- C++
- Title
- c++ vector iterator
- Category
- C++
- Title
- Runtime Error: Runtime ErrorAbort signal from abort(3) (SIGABRT)
- Category
- C++
- Title
- substr in c++
- Category
- C++
- Title
- c++ get ascii value of char
- Category
- C++
- Title
- binary search stl in c++
- Category
- C++
- Title
- what is difffrence between s.length() and s.size()
- Category
- C++
- Title
- compile c++ program
- Category
- C++