From 117c8362bea8568ebd27bc52bc980461e06a560a Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Thu, 16 Jul 2020 15:35:37 +0000 Subject: [PATCH] formatting source-code for 7adb0d8d5e534324f9f713b3f0dd19823b6ed768 --- graph/connected_components.cpp | 156 ++++++++++++++++----------------- 1 file changed, 78 insertions(+), 78 deletions(-) diff --git a/graph/connected_components.cpp b/graph/connected_components.cpp index 8b2ce9944..bfb92c7c8 100644 --- a/graph/connected_components.cpp +++ b/graph/connected_components.cpp @@ -26,9 +26,9 @@ */ #include +#include #include #include -#include /** * @namespace graph @@ -36,107 +36,107 @@ */ namespace graph { - /** - * @brief Function that add edge between two nodes or vertices of graph - * - * @param u any node or vertex of graph - * @param v any node or vertex of graph - */ - void addEdge(std::vector> *adj, int u, int v) { +/** + * @brief Function that add edge between two nodes or vertices of graph + * + * @param u any node or vertex of graph + * @param v any node or vertex of graph + */ +void addEdge(std::vector> *adj, int u, int v) { (*adj)[u - 1].push_back(v - 1); (*adj)[v - 1].push_back(u - 1); - } +} - /** - * @brief Utility function for depth first seach algorithm - * this function explores the vertex which is passed into. - * - * @param u vertex or node to be explored - * @param visited already visited vertices - */ - void explore(const std::vector> *adj, int u, - std::vector &visited) { +/** + * @brief Utility function for depth first seach algorithm + * this function explores the vertex which is passed into. + * + * @param u vertex or node to be explored + * @param visited already visited vertices + */ +void explore(const std::vector> *adj, int u, + std::vector &visited) { visited[u] = true; for (auto v : (*adj)[u]) { - if (!visited[v]) { - explore(adj, v, visited); - } + if (!visited[v]) { + explore(adj, v, visited); + } } - } +} - /** - * @brief Function that perfoms depth first search algorithm on graph - * and calculated the number of connected components. - */ - int getConnectedComponents(const std::vector> *adj) { +/** + * @brief Function that perfoms depth first search algorithm on graph + * and calculated the number of connected components. + */ +int getConnectedComponents(const std::vector> *adj) { int n = adj->size(); int connected_components = 0; std::vector visited(n, false); for (int i = 0; i < n; i++) { - if (!visited[i]) { - explore(adj, i, visited); - connected_components++; - } + if (!visited[i]) { + explore(adj, i, visited); + connected_components++; + } } return connected_components; - } -} // namespace graph +} +} // namespace graph /** Function to test the algorithm */ void tests() { - std::cout << "Running predefined tests..." << std::endl; - std::cout << "Initiating Test 1..." << std::endl; - std::vector> adj1(9, std::vector()); - graph::addEdge(&adj1, 1, 2); - graph::addEdge(&adj1, 1, 3); - graph::addEdge(&adj1, 3, 4); - graph::addEdge(&adj1, 5, 7); - graph::addEdge(&adj1, 5, 6); - graph::addEdge(&adj1, 8, 9); - - assert(graph::getConnectedComponents(&adj1) == 3); - std::cout << "Test 1 Passed..." << std::endl; + std::cout << "Running predefined tests..." << std::endl; + std::cout << "Initiating Test 1..." << std::endl; + std::vector> adj1(9, std::vector()); + graph::addEdge(&adj1, 1, 2); + graph::addEdge(&adj1, 1, 3); + graph::addEdge(&adj1, 3, 4); + graph::addEdge(&adj1, 5, 7); + graph::addEdge(&adj1, 5, 6); + graph::addEdge(&adj1, 8, 9); - std::cout << "Innitiating Test 2..." << std::endl; - std::vector> adj2(10, std::vector()); - graph::addEdge(&adj2, 1, 2); - graph::addEdge(&adj2, 1, 3); - graph::addEdge(&adj2, 1, 4); - graph::addEdge(&adj2, 2, 3); - graph::addEdge(&adj2, 3, 4); - graph::addEdge(&adj2, 4, 8); - graph::addEdge(&adj2, 4, 10); - graph::addEdge(&adj2, 8, 10); - graph::addEdge(&adj2, 8, 9); - graph::addEdge(&adj2, 5, 7); - graph::addEdge(&adj2, 5, 6); - graph::addEdge(&adj2, 6, 7); + assert(graph::getConnectedComponents(&adj1) == 3); + std::cout << "Test 1 Passed..." << std::endl; - assert(graph::getConnectedComponents(&adj2) == 2); - std::cout << "Test 2 Passed..." << std::endl; + std::cout << "Innitiating Test 2..." << std::endl; + std::vector> adj2(10, std::vector()); + graph::addEdge(&adj2, 1, 2); + graph::addEdge(&adj2, 1, 3); + graph::addEdge(&adj2, 1, 4); + graph::addEdge(&adj2, 2, 3); + graph::addEdge(&adj2, 3, 4); + graph::addEdge(&adj2, 4, 8); + graph::addEdge(&adj2, 4, 10); + graph::addEdge(&adj2, 8, 10); + graph::addEdge(&adj2, 8, 9); + graph::addEdge(&adj2, 5, 7); + graph::addEdge(&adj2, 5, 6); + graph::addEdge(&adj2, 6, 7); + + assert(graph::getConnectedComponents(&adj2) == 2); + std::cout << "Test 2 Passed..." << std::endl; } /** Main function */ int main() { - /// running predefined tests - tests(); - - int vertices, edges; - std::cout << "Enter the number of vertices : "; - std::cin >> vertices; - std::cout << "Enter the number of edges : "; - std::cin >> edges; + /// running predefined tests + tests(); - std::vector> adj(vertices, std::vector()); + int vertices, edges; + std::cout << "Enter the number of vertices : "; + std::cin >> vertices; + std::cout << "Enter the number of edges : "; + std::cin >> edges; - while (edges--) { - int u, v; - std::cin >> u >> v; - graph::addEdge(&adj, u, v); - } + std::vector> adj(vertices, std::vector()); - int cc = graph::getConnectedComponents(&adj); - std::cout << cc << std::endl; - return 0; + while (edges--) { + int u, v; + std::cin >> u >> v; + graph::addEdge(&adj, u, v); + } + + int cc = graph::getConnectedComponents(&adj); + std::cout << cc << std::endl; + return 0; }