From f9ac0b4bd9da054b58da641d7eac6783f0311167 Mon Sep 17 00:00:00 2001 From: quantum08 Date: Thu, 28 Feb 2019 22:01:24 +0530 Subject: [PATCH 1/6] program to check undirected graph is bipartite or not in c++ --- .../src/Bipartite_check/bipartite.cpp | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 code/graph_algorithms/src/Bipartite_check/bipartite.cpp diff --git a/code/graph_algorithms/src/Bipartite_check/bipartite.cpp b/code/graph_algorithms/src/Bipartite_check/bipartite.cpp new file mode 100644 index 0000000000..1a039e5a13 --- /dev/null +++ b/code/graph_algorithms/src/Bipartite_check/bipartite.cpp @@ -0,0 +1,49 @@ +#include +using namespace std; +int main() +{ + long int n,m,u,v,c,flag=0; + + // taking n ->number of vertex and m -> number of edges + cin>>n>>m; + + // adj is 2-D vector to store edges between vertex + vector >adj(n,vector()); + + // dis is use to store color of vertex + vector dis(n,-1); + queueq; + + //for number of edges times + for(int i=0;i>u>>v; + adj[u-1].push_back(v-1); + adj[v-1].push_back(u-1); + } + u=0; + dis[u]=0; + q.push(u); + while(!q.empty()) + { + c=q.front(); + q.pop(); + for(auto i=adj[c].begin();i!=adj[c].end();i++) + { + // updating color of vertex + if(dis[*i]==-1 ) + { + q.push(*i); + dis[*i]=1-dis[c]; + } + else if(dis[*i]==dis[c]) // checking for bipartite graph + { + flag=1; + } + } + } + if(flag==0) + cout<<"Graph is bipartite"; + else + cout<<"Graph is not bipartite"; +} From 371079a9faec3f74f3399aa3a56d8ca3e6544704 Mon Sep 17 00:00:00 2001 From: vaibhav Pathak <46650456+quantum08@users.noreply.github.com> Date: Mon, 4 Mar 2019 23:52:15 +0530 Subject: [PATCH 2/6] Update bipartite.cpp 1.) updating header File . 2.) removing extra spacing 3.) Better declaration of variable --- .../src/Bipartite_check/bipartite.cpp | 28 +++++++++---------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/code/graph_algorithms/src/Bipartite_check/bipartite.cpp b/code/graph_algorithms/src/Bipartite_check/bipartite.cpp index 1a039e5a13..2b27102b60 100644 --- a/code/graph_algorithms/src/Bipartite_check/bipartite.cpp +++ b/code/graph_algorithms/src/Bipartite_check/bipartite.cpp @@ -1,28 +1,26 @@ -#include +#include +#include +#include using namespace std; int main() { - long int n,m,u,v,c,flag=0; - + long int vertex,edge,u,v,c,flag=0; // taking n ->number of vertex and m -> number of edges - cin>>n>>m; - + cin>>vertex>>edge; // adj is 2-D vector to store edges between vertex - vector >adj(n,vector()); - - // dis is use to store color of vertex - vector dis(n,-1); + vector >adj(vertex,vector()); + // color is use to store color of vertex + vector color(vertex,-1); queueq; - //for number of edges times - for(int i=0;i>u>>v; adj[u-1].push_back(v-1); adj[v-1].push_back(u-1); } u=0; - dis[u]=0; + color[u]=0; q.push(u); while(!q.empty()) { @@ -31,12 +29,12 @@ int main() for(auto i=adj[c].begin();i!=adj[c].end();i++) { // updating color of vertex - if(dis[*i]==-1 ) + if(color[*i]==-1 ) { q.push(*i); - dis[*i]=1-dis[c]; + color[*i]=1-color[c]; } - else if(dis[*i]==dis[c]) // checking for bipartite graph + else if(color[*i]==color[c]) // checking for bipartite graph { flag=1; } From f524a1689fe2df5c0fc1f09359c072581d9587bc Mon Sep 17 00:00:00 2001 From: vaibhav Pathak <46650456+quantum08@users.noreply.github.com> Date: Fri, 8 Mar 2019 23:40:51 +0530 Subject: [PATCH 3/6] Update bipartite.cpp --- .../src/Bipartite_check/bipartite.cpp | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/code/graph_algorithms/src/Bipartite_check/bipartite.cpp b/code/graph_algorithms/src/Bipartite_check/bipartite.cpp index 2b27102b60..5f6d7cc98f 100644 --- a/code/graph_algorithms/src/Bipartite_check/bipartite.cpp +++ b/code/graph_algorithms/src/Bipartite_check/bipartite.cpp @@ -1,21 +1,20 @@ #include #include #include -using namespace std; int main() { long int vertex,edge,u,v,c,flag=0; // taking n ->number of vertex and m -> number of edges - cin>>vertex>>edge; + std::cin>>vertex>>edge; // adj is 2-D vector to store edges between vertex - vector >adj(vertex,vector()); + std::vector >adj(vertex,std::vector()); // color is use to store color of vertex - vector color(vertex,-1); - queueq; + std::vector color(vertex,-1); + std::queueq; //for number of edges times for(int i=0;i>u>>v; + std::cin>>u>>v; adj[u-1].push_back(v-1); adj[v-1].push_back(u-1); } @@ -41,7 +40,7 @@ int main() } } if(flag==0) - cout<<"Graph is bipartite"; + std::cout<<"Graph is bipartite"; else - cout<<"Graph is not bipartite"; + std::cout<<"Graph is not bipartite"; } From 68b3e7264ba48064c96b7c5d556185a073cde535 Mon Sep 17 00:00:00 2001 From: vaibhav Pathak <46650456+quantum08@users.noreply.github.com> Date: Thu, 14 Mar 2019 00:47:02 +0530 Subject: [PATCH 4/6] Update bipartite.cpp Change spacing around angle bracket and type of variable --- .../src/Bipartite_check/bipartite.cpp | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/code/graph_algorithms/src/Bipartite_check/bipartite.cpp b/code/graph_algorithms/src/Bipartite_check/bipartite.cpp index 5f6d7cc98f..38ba855d6e 100644 --- a/code/graph_algorithms/src/Bipartite_check/bipartite.cpp +++ b/code/graph_algorithms/src/Bipartite_check/bipartite.cpp @@ -1,20 +1,21 @@ -#include -#include -#include +#include +#include +#include int main() { - long int vertex,edge,u,v,c,flag=0; + long int vertex , edge , u , v , c ; + bool flag=false; // taking n ->number of vertex and m -> number of edges - std::cin>>vertex>>edge; + std::cin >> vertex >> edge; // adj is 2-D vector to store edges between vertex - std::vector >adj(vertex,std::vector()); + std::vector < std::vector < long int > > adj(vertex , std::vector ()); // color is use to store color of vertex - std::vector color(vertex,-1); - std::queueq; + std::vector color(vertex , -1); + std::queue q; //for number of edges times - for(int i=0;i>u>>v; + std::cin >> u >> v; adj[u-1].push_back(v-1); adj[v-1].push_back(u-1); } @@ -25,7 +26,7 @@ int main() { c=q.front(); q.pop(); - for(auto i=adj[c].begin();i!=adj[c].end();i++) + for(auto i=adj[c].begin();i!=adj[c].end(); ++i) { // updating color of vertex if(color[*i]==-1 ) @@ -35,12 +36,12 @@ int main() } else if(color[*i]==color[c]) // checking for bipartite graph { - flag=1; + flag=true; } } } - if(flag==0) - std::cout<<"Graph is bipartite"; + if( !flag ) + std::cout << "Graph is bipartite"; else - std::cout<<"Graph is not bipartite"; + std::cout << "Graph is not bipartite"; } From 44e4ba37a622897e236d18053a9e18fd346b9422 Mon Sep 17 00:00:00 2001 From: vaibhav Pathak <46650456+quantum08@users.noreply.github.com> Date: Mon, 25 Mar 2019 22:50:32 +0530 Subject: [PATCH 5/6] Update bipartite.cpp update to print output in newline --- code/graph_algorithms/src/Bipartite_check/bipartite.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/code/graph_algorithms/src/Bipartite_check/bipartite.cpp b/code/graph_algorithms/src/Bipartite_check/bipartite.cpp index 38ba855d6e..1afbed9891 100644 --- a/code/graph_algorithms/src/Bipartite_check/bipartite.cpp +++ b/code/graph_algorithms/src/Bipartite_check/bipartite.cpp @@ -41,7 +41,7 @@ int main() } } if( !flag ) - std::cout << "Graph is bipartite"; + std::cout << "Graph is bipartite" << "\n" ; else - std::cout << "Graph is not bipartite"; + std::cout << "Graph is not bipartite" << "\n" ; } From 32f70fc9a4417a3790728243af758f2c9be00091 Mon Sep 17 00:00:00 2001 From: vaibhav Pathak <46650456+quantum08@users.noreply.github.com> Date: Sun, 7 Apr 2019 14:13:30 +0530 Subject: [PATCH 6/6] Update bipartite.cpp Spacing after conditional statement and naming variable --- .../src/Bipartite_check/bipartite.cpp | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/code/graph_algorithms/src/Bipartite_check/bipartite.cpp b/code/graph_algorithms/src/Bipartite_check/bipartite.cpp index 1afbed9891..bb8baa499b 100644 --- a/code/graph_algorithms/src/Bipartite_check/bipartite.cpp +++ b/code/graph_algorithms/src/Bipartite_check/bipartite.cpp @@ -3,17 +3,17 @@ #include int main() { - long int vertex , edge , u , v , c ; - bool flag=false; + long int vertex , edge , u , v , counter ; + bool flag = false; // taking n ->number of vertex and m -> number of edges std::cin >> vertex >> edge; // adj is 2-D vector to store edges between vertex std::vector < std::vector < long int > > adj(vertex , std::vector ()); // color is use to store color of vertex - std::vector color(vertex , -1); - std::queue q; - //for number of edges times - for(int i=0;i < edge; ++i) + std::vector < long int > color(vertex , -1); + std::queue < long int > q; + // for number of edges times + for (int i=0;i < edge; ++i) { std::cin >> u >> v; adj[u-1].push_back(v-1); @@ -22,25 +22,25 @@ int main() u=0; color[u]=0; q.push(u); - while(!q.empty()) + while (!q.empty()) { - c=q.front(); + counter=q.front(); q.pop(); - for(auto i=adj[c].begin();i!=adj[c].end(); ++i) + for (auto i=adj[counter].begin();i!=adj[counter].end(); ++i) { // updating color of vertex - if(color[*i]==-1 ) + if (color[*i]==-1) { q.push(*i); - color[*i]=1-color[c]; + color[*i]=1-color[counter]; } - else if(color[*i]==color[c]) // checking for bipartite graph + else if (color[*i]==color[counter]) { flag=true; } } } - if( !flag ) + if ( !flag ) std::cout << "Graph is bipartite" << "\n" ; else std::cout << "Graph is not bipartite" << "\n" ;