mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-02-13 07:25:42 +08:00
Merge branch 'master' of https://github.com/ayaankhan98/C-Plus-Plus
This commit is contained in:
@@ -2,7 +2,8 @@
|
||||
*
|
||||
* \file
|
||||
* \brief [Connected Components
|
||||
* (Connected Components)](https://en.wikipedia.org/wiki/Component_(graph_theory))
|
||||
* (Connected
|
||||
* Components)](https://en.wikipedia.org/wiki/Component_(graph_theory))
|
||||
*
|
||||
* \author [Ayaan Khan](http://github.com/ayaankhan98)
|
||||
*
|
||||
@@ -35,7 +36,7 @@ using std::vector;
|
||||
*/
|
||||
class graph {
|
||||
private:
|
||||
/** \brief adj stores adjacency list representation of graph */
|
||||
/** \brief adj stores adjacency list representation of graph */
|
||||
vector<vector<int>> adj;
|
||||
|
||||
/** \brief keep track of connected components */
|
||||
@@ -48,16 +49,16 @@ class graph {
|
||||
void explore(int, vector<bool> &);
|
||||
|
||||
public:
|
||||
/**
|
||||
/**
|
||||
* \brief Constructor that intiliazes the graph on creation and set
|
||||
* the connected components to 0
|
||||
*/
|
||||
explicit graph(int n) : adj(n, vector<int>()) { connected_components = 0; }
|
||||
|
||||
|
||||
/** \brief Function to add a edge between two nodes or vertices of graph */
|
||||
void addEdge(int, int);
|
||||
|
||||
/**
|
||||
/**
|
||||
* \brief Function the calculates the connected compoents in the graph
|
||||
* by performing the depth first search on graph
|
||||
*
|
||||
@@ -112,14 +113,14 @@ void graph::explore(int u, vector<bool> &visited) {
|
||||
|
||||
/** Main function */
|
||||
int main() {
|
||||
/// creating a graph with 4 vertex
|
||||
/// creating a graph with 4 vertex
|
||||
graph g(4);
|
||||
|
||||
/// Adding edges between vertices
|
||||
/// Adding edges between vertices
|
||||
g.addEdge(1, 2);
|
||||
g.addEdge(3, 2);
|
||||
|
||||
/// printing the connected components
|
||||
/// printing the connected components
|
||||
std::cout << g.getConnectedComponents();
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user