From b94c29ce016dedf785721cbac353c18fd1a486df Mon Sep 17 00:00:00 2001 From: AkVaya Date: Fri, 14 Aug 2020 00:23:51 +0530 Subject: [PATCH 1/7] Added is_graph_bipartite.cpp --- graph/is_graph_bipartite.cpp | 135 +++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 graph/is_graph_bipartite.cpp diff --git a/graph/is_graph_bipartite.cpp b/graph/is_graph_bipartite.cpp new file mode 100644 index 000000000..7d6df310d --- /dev/null +++ b/graph/is_graph_bipartite.cpp @@ -0,0 +1,135 @@ +/** + * @file is_graph_bipartite + * + * @brief Algorithm to check whether a graph is bipartite + * + * @details + * A graph is a collection of nodes also called vertices and these vertices + * are connected by edges.A bipartite graph is a graph whose vertices can be + * divided into two disjoint and independent sets U and V such that every edge + * connects a vertex in U to one in V. + * (https://en.wikipedia.org/wiki/Bipartite_graph) + * The given Algorithm will determine whether the given graph is bipartite or not + * + * + * Example - Here is a graph g1 with 5 vertices and is bipartite + * + * 1 4 + * / \ / \ + * 2 3 5 + * + * Example - Here is a graph G2 with 3 vertices and is not bipartite + * + * 1 --- 2 + * \ / + * 3 + * + * + * @author [Akshat Vaya](https://github.com/AkVaya) + * + */ +#include +#include +#include + +using std::vector; +using std::queue; + +const int nax = 5e5 + 1; +/** + * Class for representing graph as an adjacency list. + */ +class graph { + private: + int n; /// size of the graph + + vector > adj; /// adj stores the graph as an adjacency list + + vector side; ///stores the side of the vertex + + public: + /** + * @brief Constructor that initializes the graph on creation + */ + graph(int size = nax){ + n = size; + adj.resize(n); + side.resize(n,-1); + } + + void addEdge(int u, int v); /// function to add edges to our graph + + bool is_bipartite(); /// function to check whether the graph is bipartite or not + +}; +/** + * @brief Function that add an edge between two nodes or vertices of graph + * + * @param u is a node or vertex of graph + * @param v is a 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 checks whether the graph is bipartite or not + */ +bool graph::is_bipartite(){ + n = adj.size(); + side.resize(n,-1); + bool check = true; + queue q; + for (int current_edge = 0; current_edge < n; ++current_edge) + { + if(side[current_edge] == -1){ + q.push(current_edge); + side[current_edge] = 0; + while(q.size()){ + int current = q.front(); + q.pop(); + for(auto neighbour : adj[current]){ + if(side[neighbour] == -1){ + side[neighbour] = (1 ^ side[current]); + q.push(neighbour); + } + else{ + check &= (side[neighbour] != side[current]); + } + } + } + } + } + return check; +} +/** + * main funtion + */ +int main(){ + graph G1(5); /// creating graph G1 with 5 vertices + /// adding edges to the graphs as per the illustrated example + G1.addEdge(1,2); + G1.addEdge(1,3); + G1.addEdge(3,4); + G1.addEdge(4,5); + + graph G2(3); /// creating graph G2 with 3 vertices + /// adding edges to the graphs as per the illustrated example + G2.addEdge(1,2); + G2.addEdge(1,3); + G2.addEdge(2,3); + /// checking whether the graphs are bipartite or not + if(G1.is_bipartite()){ + std::cout<<"The given graph G1 is a bipartite graph\n"; + } + else{ + std::cout<<"The given graph G1 is not a bipartite graph\n"; + } + if(G2.is_bipartite()){ + std::cout<<"The given graph G2 is a bipartite graph\n"; + } + else{ + std::cout<<"The given graph G2 is not a bipartite graph\n"; + } + return 0; +} \ No newline at end of file From 97f3e1a0769cf4796a2507bc93c66b6adb600757 Mon Sep 17 00:00:00 2001 From: AkVaya Date: Fri, 14 Aug 2020 01:06:24 +0530 Subject: [PATCH 2/7] Perfomed the requested changes --- graph/is_graph_bipartite.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/graph/is_graph_bipartite.cpp b/graph/is_graph_bipartite.cpp index 7d6df310d..f0d0ef098 100644 --- a/graph/is_graph_bipartite.cpp +++ b/graph/is_graph_bipartite.cpp @@ -103,7 +103,7 @@ bool graph::is_bipartite(){ return check; } /** - * main funtion + * Main funtion */ int main(){ graph G1(5); /// creating graph G1 with 5 vertices @@ -132,4 +132,4 @@ int main(){ std::cout<<"The given graph G2 is not a bipartite graph\n"; } return 0; -} \ No newline at end of file +} From d87a6685f623ddc7cba3f1cbd1287d6d69630885 Mon Sep 17 00:00:00 2001 From: AkVaya Date: Fri, 14 Aug 2020 01:43:57 +0530 Subject: [PATCH 3/7] Performed the requested changes --- graph/is_graph_bipartite.cpp | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/graph/is_graph_bipartite.cpp b/graph/is_graph_bipartite.cpp index f0d0ef098..718ea9853 100644 --- a/graph/is_graph_bipartite.cpp +++ b/graph/is_graph_bipartite.cpp @@ -2,13 +2,14 @@ * @file is_graph_bipartite * * @brief Algorithm to check whether a graph is bipartite + * (https://en.wikipedia.org/wiki/Bipartite_graph) * * @details * A graph is a collection of nodes also called vertices and these vertices * are connected by edges.A bipartite graph is a graph whose vertices can be * divided into two disjoint and independent sets U and V such that every edge * connects a vertex in U to one in V. - * (https://en.wikipedia.org/wiki/Bipartite_graph) + * * The given Algorithm will determine whether the given graph is bipartite or not * * @@ -32,9 +33,6 @@ #include #include -using std::vector; -using std::queue; - const int nax = 5e5 + 1; /** * Class for representing graph as an adjacency list. @@ -43,9 +41,9 @@ class graph { private: int n; /// size of the graph - vector > adj; /// adj stores the graph as an adjacency list + std::vector > adj; /// adj stores the graph as an adjacency list - vector side; ///stores the side of the vertex + std::vector side; ///stores the side of the vertex public: /** @@ -74,12 +72,14 @@ void graph::addEdge(int u, int v) { } /** * @brief function that checks whether the graph is bipartite or not + * the function returns true if the graph is a bipartite graph + * the function returns false if the graph is not a bipartite graph */ bool graph::is_bipartite(){ n = adj.size(); side.resize(n,-1); bool check = true; - queue q; + std::queue q; for (int current_edge = 0; current_edge < n; ++current_edge) { if(side[current_edge] == -1){ @@ -103,9 +103,9 @@ bool graph::is_bipartite(){ return check; } /** - * Main funtion + * Function to test the above algorithm */ -int main(){ +void test(){ graph G1(5); /// creating graph G1 with 5 vertices /// adding edges to the graphs as per the illustrated example G1.addEdge(1,2); @@ -118,6 +118,7 @@ int main(){ G2.addEdge(1,2); G2.addEdge(1,3); G2.addEdge(2,3); + /// checking whether the graphs are bipartite or not if(G1.is_bipartite()){ std::cout<<"The given graph G1 is a bipartite graph\n"; @@ -131,5 +132,11 @@ int main(){ else{ std::cout<<"The given graph G2 is not a bipartite graph\n"; } +} +/** + * Main function + */ +int main(){ + test(); ///Testing return 0; } From ab26183183585147498b1a6b077c7aa34f69bb0d Mon Sep 17 00:00:00 2001 From: AkVaya Date: Fri, 14 Aug 2020 10:04:21 +0530 Subject: [PATCH 4/7] Performed the requested changes --- graph/is_graph_bipartite.cpp | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/graph/is_graph_bipartite.cpp b/graph/is_graph_bipartite.cpp index 718ea9853..86b82b781 100644 --- a/graph/is_graph_bipartite.cpp +++ b/graph/is_graph_bipartite.cpp @@ -1,9 +1,8 @@ /** * @file is_graph_bipartite * - * @brief Algorithm to check whether a graph is bipartite - * (https://en.wikipedia.org/wiki/Bipartite_graph) - * + * @brief Algorithm to check whether a graph is [bipartite](https://en.wikipedia.org/wiki/Bipartite_graph) + * * @details * A graph is a collection of nodes also called vertices and these vertices * are connected by edges.A bipartite graph is a graph whose vertices can be @@ -11,20 +10,19 @@ * connects a vertex in U to one in V. * * The given Algorithm will determine whether the given graph is bipartite or not - * - * + * + * * Example - Here is a graph g1 with 5 vertices and is bipartite - * + * * 1 4 * / \ / \ * 2 3 5 * * Example - Here is a graph G2 with 3 vertices and is not bipartite - * + * * 1 --- 2 * \ / * 3 - * * * @author [Akshat Vaya](https://github.com/AkVaya) * @@ -74,10 +72,16 @@ void graph::addEdge(int u, int v) { * @brief function that checks whether the graph is bipartite or not * the function returns true if the graph is a bipartite graph * the function returns false if the graph is not a bipartite graph + * + * @details + * Here, side refers to the two disjoint subsets of the bipartite graph. + * Initially, the values of side are set to -1 which is an unassigned state. A for loop is run for every vertex of the graph. + * If the current edge has no side assigned to it, then a Breadth First Search operation is performed. + * If two neighbours have the same side then the graph will not be bipartite and the value of check becomes false. + * If and only if each pair of neighbours have different sides, the value of check will be true and hence the graph bipartite. + * */ bool graph::is_bipartite(){ - n = adj.size(); - side.resize(n,-1); bool check = true; std::queue q; for (int current_edge = 0; current_edge < n; ++current_edge) From 3e355d0f7ad5bc8afdcfa1f8c53afc967ac16fd5 Mon Sep 17 00:00:00 2001 From: AkVaya Date: Sat, 15 Aug 2020 00:53:31 +0530 Subject: [PATCH 5/7] Performed the requested changes --- graph/is_graph_bipartite.cpp | 156 +++++++++++++++++++---------------- 1 file changed, 86 insertions(+), 70 deletions(-) diff --git a/graph/is_graph_bipartite.cpp b/graph/is_graph_bipartite.cpp index 86b82b781..426d25560 100644 --- a/graph/is_graph_bipartite.cpp +++ b/graph/is_graph_bipartite.cpp @@ -31,93 +31,109 @@ #include #include -const int nax = 5e5 + 1; /** * Class for representing graph as an adjacency list. */ -class graph { - private: - int n; /// size of the graph - - std::vector > adj; /// adj stores the graph as an adjacency list - - std::vector side; ///stores the side of the vertex - - public: - /** - * @brief Constructor that initializes the graph on creation - */ - graph(int size = nax){ - n = size; - adj.resize(n); - side.resize(n,-1); - } - - void addEdge(int u, int v); /// function to add edges to our graph - - bool is_bipartite(); /// function to check whether the graph is bipartite or not - -}; /** - * @brief Function that add an edge between two nodes or vertices of graph - * - * @param u is a node or vertex of graph - * @param v is a node or vertex of graph + * @namespace graph + * @brief Graph algorithms */ -void graph::addEdge(int u, int v) { - adj[u-1].push_back(v-1); - adj[v-1].push_back(u-1); -} -/** - * @brief function that checks whether the graph is bipartite or not - * the function returns true if the graph is a bipartite graph - * the function returns false if the graph is not a bipartite graph - * - * @details - * Here, side refers to the two disjoint subsets of the bipartite graph. - * Initially, the values of side are set to -1 which is an unassigned state. A for loop is run for every vertex of the graph. - * If the current edge has no side assigned to it, then a Breadth First Search operation is performed. - * If two neighbours have the same side then the graph will not be bipartite and the value of check becomes false. - * If and only if each pair of neighbours have different sides, the value of check will be true and hence the graph bipartite. - * - */ -bool graph::is_bipartite(){ - bool check = true; - std::queue q; - for (int current_edge = 0; current_edge < n; ++current_edge) - { - if(side[current_edge] == -1){ - q.push(current_edge); - side[current_edge] = 0; - while(q.size()){ - int current = q.front(); - q.pop(); - for(auto neighbour : adj[current]){ - if(side[neighbour] == -1){ - side[neighbour] = (1 ^ side[current]); - q.push(neighbour); - } - else{ - check &= (side[neighbour] != side[current]); +namespace graph{ + /** + * @namespace is_graph_bipartite + * @brief Functions for checking whether a graph is bipartite or not + */ + namespace is_graph_bipartite{ + + class Graph { + private: + int n; /// size of the graph + + std::vector > adj; /// adj stores the graph as an adjacency list + + std::vector side; ///stores the side of the vertex + + static const int nax = 5e5 + 1; + + + public: + /** + * @brief Constructor that initializes the graph on creation + */ + explicit Graph(int size = nax){ + n = size; + adj.resize(n); + side.resize(n,-1); + } + + void addEdge(int u, int v); /// function to add edges to our graph + + bool is_bipartite(); /// function to check whether the graph is bipartite or not + + }; + /** + * @brief Function that add an edge between two nodes or vertices of graph + * + * @param u is a node or vertex of graph + * @param v is a 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 checks whether the graph is bipartite or not + * the function returns true if the graph is a bipartite graph + * the function returns false if the graph is not a bipartite graph + * + * @details + * Here, side refers to the two disjoint subsets of the bipartite graph. + * Initially, the values of side are set to -1 which is an unassigned state. A for loop is run for every vertex of the graph. + * If the current edge has no side assigned to it, then a Breadth First Search operation is performed. + * If two neighbours have the same side then the graph will not be bipartite and the value of check becomes false. + * If and only if each pair of neighbours have different sides, the value of check will be true and hence the graph bipartite. + * + */ + bool Graph::is_bipartite(){ + bool check = true; + std::queue q; + for (int current_edge = 0; current_edge < n; ++current_edge) + { + if(side[current_edge] == -1){ + q.push(current_edge); + side[current_edge] = 0; + while(q.size()){ + int current = q.front(); + q.pop(); + for(auto neighbour : adj[current]){ + if(side[neighbour] == -1){ + side[neighbour] = (1 ^ side[current]); + q.push(neighbour); + } + else{ + check &= (side[neighbour] != side[current]); + } + } } } } + return check; } - } - return check; -} + } /// namespace is_graph_bipartite +} /// namespace graph /** - * Function to test the above algorithm + * Function to test the above algorithm + * @returns none */ -void test(){ - graph G1(5); /// creating graph G1 with 5 vertices +static void test(){ + graph::is_graph_bipartite::Graph G1(5); /// creating graph G1 with 5 vertices /// adding edges to the graphs as per the illustrated example G1.addEdge(1,2); G1.addEdge(1,3); G1.addEdge(3,4); G1.addEdge(4,5); - graph G2(3); /// creating graph G2 with 3 vertices + graph::is_graph_bipartite::Graph G2(3); /// creating graph G2 with 3 vertices /// adding edges to the graphs as per the illustrated example G2.addEdge(1,2); G2.addEdge(1,3); From e248a614e58cd377f8960db117df174ba13172ed Mon Sep 17 00:00:00 2001 From: AkVaya Date: Sat, 15 Aug 2020 01:58:23 +0530 Subject: [PATCH 6/7] Performed requested changes --- graph/is_graph_bipartite.cpp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/graph/is_graph_bipartite.cpp b/graph/is_graph_bipartite.cpp index 426d25560..db73a72af 100644 --- a/graph/is_graph_bipartite.cpp +++ b/graph/is_graph_bipartite.cpp @@ -32,11 +32,8 @@ #include /** - * Class for representing graph as an adjacency list. - */ -/** - * @namespace graph - * @brief Graph algorithms + * @namespace graph + * @brief Graph algorithms */ namespace graph{ /** @@ -44,7 +41,9 @@ namespace graph{ * @brief Functions for checking whether a graph is bipartite or not */ namespace is_graph_bipartite{ - + /** + * @brief Class for representing graph as an adjacency list. + */ class Graph { private: int n; /// size of the graph @@ -57,9 +56,9 @@ namespace graph{ public: - /** - * @brief Constructor that initializes the graph on creation - */ + /** + * @brief Constructor that initializes the graph on creation + */ explicit Graph(int size = nax){ n = size; adj.resize(n); From fd6b8100614e97b30056eb48744cb4fc8cbd32b9 Mon Sep 17 00:00:00 2001 From: AkVaya Date: Sat, 15 Aug 2020 10:58:51 +0530 Subject: [PATCH 7/7] Performed the requested changes --- graph/is_graph_bipartite.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/graph/is_graph_bipartite.cpp b/graph/is_graph_bipartite.cpp index db73a72af..baadf71fa 100644 --- a/graph/is_graph_bipartite.cpp +++ b/graph/is_graph_bipartite.cpp @@ -1,5 +1,5 @@ /** - * @file is_graph_bipartite + * @file * * @brief Algorithm to check whether a graph is [bipartite](https://en.wikipedia.org/wiki/Bipartite_graph) * @@ -11,7 +11,7 @@ * * The given Algorithm will determine whether the given graph is bipartite or not * - * + *
  * 	Example - Here is a graph g1 with 5 vertices and is bipartite
  *	
  *		1   4
@@ -24,6 +24,8 @@
  *		 \   /
  *		   3
  *	
+ *	
+ * * @author [Akshat Vaya](https://github.com/AkVaya) * */