datastructure@ Check Whether a given graph is bipartite or not

Source: Internet
Author: User

Check whether a given graph is bipartite or not

A bipartite graph is a graph whose vertices can being divided into a independent sets, u and V such that every edge (U, V) Either connects a vertex from U-V or a vertex from V to U. In other words, for every edge (U, V), either U belongs To-U and V to V, or U-belongs to V. and V to U. We can also say that there are no edge that connects vertices of same set.

A bipartite graph is possible if the graph coloring is possible using a and colors such that vertices in a set is colored W ith the same color. Note that it's possible to color a cycle graph with the even cycle using the-colors. For example, see the following graph.

It is not possible to color a cycle graph with odd cycle using the-colors.

Algorithm to check if a graph is bipartite:
One approach is to check whether the graph was 2-colorable or not using backtracking algorithm m coloring problem.
Following is a simple algorithm to find out whether a given graph are birpartite or not using breadth First Search (BFS).
1.Assign RED color to the source vertex (putting into set U).
2.Color all the neighbors with BLUE Color (putting to set V).
3.Color all neighbor ' s neighbor with RED Color (putting to set U).
4.This, assign color to all vertices such that it satisfies all the constraints of M-to coloring problem where M = 2.
5. While assigning colors, if we find a neighbor which are colored with same color as current vertex, then the graph cannot Be colored with 2 vertices (or graph was not bipartite)

#include <iostream>#include<cstdio>#include<cstring>#include<limits>#include<vector>#include<stack>using namespacestd;structedge{intto, cost; Edge (intt) {         This->to = t; This->cost =0; }};voidAddedge (vector<edge> &, vector<vector<int> > &int,int);//add directed edge.voidBuildmap (vector<edge> &edgelist, vector<vector<int> > &G) {Addedge (edgelist,g,0,1); Addedge (Edgelist,g,1,2); Addedge (Edgelist,g,2,3); Addedge (Edgelist,g,3,4); Addedge (Edgelist,g,4,0); //Addedge (edgelist,g,5,0);}voidAdddoubleedge (vector<edge> &, vector<vector<int> > &int,int);//add undirected edge.BOOLIscyclic (VECTOR&LT;EDGE&GT;, vector<vector<int> >,vector<BOOL, vector<BOOL,int);//find cycles starting from V.voidIscyclicutil (VECTOR&LT;EDGE&GT;, vector<vector<int> >);//find all cycles.BOOLDFS (VECTOR&LT;EDGE&GT;, vector<vector<int>, vector<BOOL,int,int);//check if "to" is reachable from "from".voidIsreachable (VECTOR&LT;EDGE&GT;, vector<vector<int>,int,int);BOOLIsbipartitie (vector<edge>, vector<vector<int>,intV);//Check if a graph is a bipartite graph.intMain () {intMAXN =5; Vector<edge>edgelist; Vector<vector<int> >G (MAXN);        Buildmap (EDGELIST,G); //Iscyclicutil (edgelist, G); //isreachable (Edgelist, G, 1, 1);        if(Isbipartitie (Edgelist, G,0)) cout<<"YES"<<Endl; Elsecout<<"NO"<<Endl; return 0;}BOOLIscyclic (vector<edge> edgelist, vector<vector<int> > g,vector<BOOL> Vis, vector<BOOL> Recstack,intv) {     for(intI=0; I<g[v].size (); + +i) {Edge e=Edgelist[g[v][i]]; if(Recstack[e.to])return true; if(!Vis[e.to]) {Vis[e.to]=true; Recstack[e.to] =true; if(Iscyclic (edgelist,g,vis,recstack,e.to))return true; Recstack[e.to]=false; }    }    return false;}voidIscyclicutil (vector<edge> edgelist, vector<vector<int> > G) {//find all cycles.vector<BOOL>Vis (G.size ()); Vector<BOOL>Recstack (G.size ());  for(intI=0; I<vis.size (); ++i) vis[i]=false;  for(intI=0; I<recstack.size (); ++i) recstack[i]=false;  for(intI=0; I<g.size (); + +i) {        if(!Vis[i]) {Vis[i]=true; Recstack[i] =true; if(Iscyclic (edgelist,g,vis,recstack,i)) {cout<<i<<"starts a cycle"<<Endl; } Recstack[i]=false; }    }}voidAddedge (vector<edge> &edgelist, vector<vector<int> > &g,int  from,intTo )    {Edgelist.push_back (Edge (to)); g[ from].push_back (Edgelist.size ()-1);}voidAdddoubleedge (vector<edge> &edgelist, vector<vector<int> > &g,int  from,intTo ) {Addedge (edgelist,g, from, to); Addedge (Edgelist,g,to, from);}BOOLDFS (vector<edge> edgelist, vector<vector<int> > G, vector<BOOL> Vis,int  from,intTo ) {    if( from= = to)return true;  for(intI=0; i<g[ from].size (); + +i) {Edge e= edgelist[g[ from][i]]; if(e.to = = to)return true; if(!Vis[e.to]) {Vis[e.to]=true; if(Dfs (edgelist, G, Vis, e.to, to))return true; }    }    return false;}voidIsreachable (vector<edge> edgelist, vector<vector<int> > G,int  from,intTo ) {Vector<BOOL>Vis (G.size ());  for(intI=0; I<vis.size (); ++i) Vis[i] =false; vis[ from] =true; if(Dfs (edgelist, G, Vis, from, to)) cout<< from<<" and"<<to<<"is reachable to all other"<<Endl; Elsecout<< from<<" and"<<to<<"is not reachable"<<Endl;}BOOLIsbipartitie (vector<edge> edgelist, vector<vector<int> > G,intv) {Vector<int>Color (g.size ());  for(intI=0; I<color.size (); ++i) Color[i] =-1; Stack<int>St;  while(!st.empty ()) St.pop (); St.push (v); COLOR[V]=1;//1 stands for RED, and 0 stands for BLUE,-1 stands for non-colored.         while(!St.empty ()) {        intK =st.top (); St.pop ();  for(intI=0; I<g[k].size (); + +i) {Edge e=Edgelist[g[k][i]]; if(Color[e.to] = =-1) {color[e.to]=1-Color[k];            St.push (e.to); }             Else if(color[e.to] = = Color[k])return false; }    }    return true;}
View Code

[email protected] Check whether a given graph is bipartite or not

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.