From e6ab38c2fdd803ebc241cce2aeb7a28161ac259a Mon Sep 17 00:00:00 2001 From: Ayaan Khan Date: Thu, 25 Jun 2020 04:13:49 +0530 Subject: [PATCH 1/4] Added docs --- graph/connected_components.cpp | 71 +++++++++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) diff --git a/graph/connected_components.cpp b/graph/connected_components.cpp index 0bfb8bbdb..ab35f7587 100644 --- a/graph/connected_components.cpp +++ b/graph/connected_components.cpp @@ -1,29 +1,86 @@ +/** + * + * \file + * \brief [Connected Components + * (Connected Components)](https://en.wikipedia.org/wiki/Component_(graph_theory)) + * + * \author [Ayaan Khan](http://github.com/ayaankhan98) + * + * \details + * A graph is a collection of nodes also called vertices and these vertices + * are connected by edges. A connected component in a graph refers to a set of + * vertices which are reachable form one another. + * + * Example - Here is graph with 3 connected components + * + * 3 9 6 8 + * / \ / / \ / \ + * 2---4 2 7 3 7 + * + * first second third + * component component component + * + */ + +#include #include #include using std::vector; +/** + * Class for representing graph as a adjacency list. + */ class graph { private: + /** \brief adj stores adjacency list representation of graph */ vector> adj; + + /** \brief keep track of connected components */ int connected_components; + + /** \brief Utility function to perform depth first search on graph */ void depth_first_search(); + + /** \brief Utility function that explores given vertex in graph */ void explore(int, vector &); public: + /** + * \brief Constructor that intiliazes the graph on creation and set + * the connected components to 0 + */ explicit graph(int n) : adj(n, vector()) { 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 + * + * @return connected_components total connected components in graph + */ int getConnectedComponents() { depth_first_search(); return connected_components; } }; +/** + * \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 graph::addEdge(int u, int v) { adj[u - 1].push_back(v - 1); adj[v - 1].push_back(u - 1); } +/** + * \brief Function that perfoms depth first search algorithm on graph + */ void graph::depth_first_search() { int n = adj.size(); vector visited(n, false); @@ -35,7 +92,13 @@ void graph::depth_first_search() { } } } - +/** + * \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 vertex + */ void graph::explore(int u, vector &visited) { visited[u] = true; for (auto v : adj[u]) { @@ -45,10 +108,16 @@ void graph::explore(int u, vector &visited) { } } +/** Main function */ int main() { + /// creating a graph with 4 vertex graph g(4); + + /// Adding edges between vertices g.addEdge(1, 2); g.addEdge(3, 2); + + /// printing the connected components std::cout << g.getConnectedComponents(); return 0; } From 6980792eff787eb58023042f4103be4af3f5ab14 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Wed, 24 Jun 2020 22:44:37 +0000 Subject: [PATCH 2/4] formatting source-code for e6ab38c2fdd803ebc241cce2aeb7a28161ac259a --- graph/connected_components.cpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/graph/connected_components.cpp b/graph/connected_components.cpp index ab35f7587..efff4e4c7 100644 --- a/graph/connected_components.cpp +++ b/graph/connected_components.cpp @@ -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) * @@ -33,7 +34,7 @@ using std::vector; */ class graph { private: - /** \brief adj stores adjacency list representation of graph */ + /** \brief adj stores adjacency list representation of graph */ vector> adj; /** \brief keep track of connected components */ @@ -46,16 +47,16 @@ class graph { void explore(int, vector &); public: - /** + /** * \brief Constructor that intiliazes the graph on creation and set * the connected components to 0 */ explicit graph(int n) : adj(n, vector()) { 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 * @@ -110,14 +111,14 @@ void graph::explore(int u, vector &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; } From 64bb640391f0d60bed8f87356ac86a487a94aac9 Mon Sep 17 00:00:00 2001 From: Ayaan Khan Date: Thu, 25 Jun 2020 13:02:12 +0530 Subject: [PATCH 3/4] Wrapped example in

---
 graph/connected_components.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/graph/connected_components.cpp b/graph/connected_components.cpp
index ab35f7587..18c99cf45 100644
--- a/graph/connected_components.cpp
+++ b/graph/connected_components.cpp
@@ -11,6 +11,7 @@
  * are connected by edges. A connected component in a graph refers to a set of
  * vertices which are reachable form one another.
  *
+ * 
  * Example - Here is graph with 3 connected components
  *
  *      3   9           6               8
@@ -19,6 +20,7 @@
  *
  *    first          second           third
  *    component      component        component
+ * 
* */ From 05789603ab82ac5e00e2e5d415509719066687c8 Mon Sep 17 00:00:00 2001 From: Ayaan Khan Date: Thu, 25 Jun 2020 15:49:30 +0530 Subject: [PATCH 4/4] fix: repeated sentences --- graph/connected_components.cpp | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/graph/connected_components.cpp b/graph/connected_components.cpp index ec7ef19e6..e53dbf424 100644 --- a/graph/connected_components.cpp +++ b/graph/connected_components.cpp @@ -1,9 +1,9 @@ /** * * \file - * \brief [Connected Components - * (Connected - * Components)](https://en.wikipedia.org/wiki/Component_(graph_theory)) + * \brief [Graph Connected Components + * (Connected Components)] + * (https://en.wikipedia.org/wiki/Component_(graph_theory)) * * \author [Ayaan Khan](http://github.com/ayaankhan98) * @@ -42,10 +42,7 @@ class graph { /** \brief keep track of connected components */ int connected_components; - /** \brief Utility function to perform depth first search on graph */ void depth_first_search(); - - /** \brief Utility function that explores given vertex in graph */ void explore(int, vector &); public: @@ -55,7 +52,6 @@ class graph { */ explicit graph(int n) : adj(n, vector()) { connected_components = 0; } - /** \brief Function to add a edge between two nodes or vertices of graph */ void addEdge(int, int); /**