TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
connected_components.cpp
Go to the documentation of this file.
1
28#include <algorithm>
29#include <cassert>
30#include <iostream>
31#include <vector>
32
38namespace graph {
46void addEdge(std::vector<std::vector<int>> *adj, int u, int v) {
47 (*adj)[u - 1].push_back(v - 1);
48 (*adj)[v - 1].push_back(u - 1);
49}
50
59void explore(const std::vector<std::vector<int>> *adj, int u,
60 std::vector<bool> *visited) {
61 (*visited)[u] = true;
62 for (auto v : (*adj)[u]) {
63 if (!(*visited)[v]) {
64 explore(adj, v, visited);
65 }
66 }
67}
68
77int getConnectedComponents(const std::vector<std::vector<int>> *adj) {
78 int n = adj->size();
79 int connected_components = 0;
80 std::vector<bool> visited(n, false);
81
82 for (int i = 0; i < n; i++) {
83 if (!visited[i]) {
84 explore(adj, i, &visited);
85 connected_components++;
86 }
87 }
88 return connected_components;
89}
90} // namespace graph
91
93void tests() {
94 std::cout << "Running predefined tests..." << std::endl;
95 std::cout << "Initiating Test 1..." << std::endl;
96 std::vector<std::vector<int>> adj1(9, std::vector<int>());
97 graph::addEdge(&adj1, 1, 2);
98 graph::addEdge(&adj1, 1, 3);
99 graph::addEdge(&adj1, 3, 4);
100 graph::addEdge(&adj1, 5, 7);
101 graph::addEdge(&adj1, 5, 6);
102 graph::addEdge(&adj1, 8, 9);
103
104 assert(graph::getConnectedComponents(&adj1) == 3);
105 std::cout << "Test 1 Passed..." << std::endl;
106
107 std::cout << "Innitiating Test 2..." << std::endl;
108 std::vector<std::vector<int>> adj2(10, std::vector<int>());
109 graph::addEdge(&adj2, 1, 2);
110 graph::addEdge(&adj2, 1, 3);
111 graph::addEdge(&adj2, 1, 4);
112 graph::addEdge(&adj2, 2, 3);
113 graph::addEdge(&adj2, 3, 4);
114 graph::addEdge(&adj2, 4, 8);
115 graph::addEdge(&adj2, 4, 10);
116 graph::addEdge(&adj2, 8, 10);
117 graph::addEdge(&adj2, 8, 9);
118 graph::addEdge(&adj2, 5, 7);
119 graph::addEdge(&adj2, 5, 6);
120 graph::addEdge(&adj2, 6, 7);
121
122 assert(graph::getConnectedComponents(&adj2) == 2);
123 std::cout << "Test 2 Passed..." << std::endl;
124}
125
127int main() {
129 tests();
130
131 int vertices = int(), edges = int();
132 std::cout << "Enter the number of vertices : ";
133 std::cin >> vertices;
134 std::cout << "Enter the number of edges : ";
135 std::cin >> edges;
136
137 std::vector<std::vector<int>> adj(vertices, std::vector<int>());
138
139 int u = int(), v = int();
140 while (edges--) {
141 std::cin >> u >> v;
142 graph::addEdge(&adj, u, v);
143 }
144
145 int cc = graph::getConnectedComponents(&adj);
146 std::cout << cc << std::endl;
147 return 0;
148}
void tests()
Graph Algorithms.
void explore(const std::vector< std::vector< int > > *adj, int u, std::vector< bool > *visited)
Utility function for depth first seach algorithm this function explores the vertex which is passed in...
int getConnectedComponents(const std::vector< std::vector< int > > *adj)
Function that perfoms depth first search algorithm on graph and calculated the number of connected co...
void addEdge(std::vector< std::vector< int > > *adj, int u, int v)
Function that add edge between two nodes or vertices of graph.