Check whether a given graph is Bipartite or not

Bipartite Graph is a graph whose vertices can be divided into two independent sets, U and V such that every edge (u, v) either connects a vertex from U to 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 is no edge that connects vertices of same set.

A bipartite graph is possible if the graph coloring is possible using two colors such that vertices in a set are colored with the same color. Note that it is possible to color a cycle graph with even cycle using two colors. For example, see the following graph.

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

Algorithm to check if a graph is Bipartite:
One approach is to check whether the graph is 2-colorable or not using backtracking algorithm m coloring problem.
Following is a simple algorithm to find out whether a given graph is 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 into set V).
3. Color all neighbor’s neighbor with RED color (putting into set U).
4. This way, assign color to all vertices such that it satisfies all the constraints of m way coloring problem where m = 2.
5. While assigning colors, if we find a neighbor which is colored with same color as current vertex, then the graph cannot be colored with 2 vertices (or graph is not Bipartite)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<limits>
#include<vector>
#include<stack>
using namespace std;
struct edge{
int to, cost;
edge(int t){
this->to = t; this->cost = ;
}
};
void addEdge(vector<edge> &, vector<vector<int> > &, int, int);//add directed edge.
void buildMap(vector<edge> &edgelist, vector<vector<int> > &G){
addEdge(edgelist,G,,);
addEdge(edgelist,G,,);
addEdge(edgelist,G,,);
addEdge(edgelist,G,,);
addEdge(edgelist,G,,);
//addEdge(edgelist,G,5,0);
}
void addDoubleEdge(vector<edge> &, vector<vector<int> > &, int, int);// add undirected edge.
bool isCyclic(vector<edge>, vector<vector<int> >,vector<bool>, vector<bool>, int);// find cycles starting from v.
void isCyclicUtil(vector<edge>, vector<vector<int> >);// find all cycles.
bool dfs(vector<edge>, vector<vector<int> >, vector<bool>, int, int);//check if ''to'' is reachable from ''from''.
void isReachable(vector<edge>, vector<vector<int> >, int, int);
bool isBipartitie(vector<edge> , vector<vector<int> >,int v);//check if a graph is a bipartite graph.
int main(){
int maxn = ;
vector<edge> edgelist;
vector<vector<int> > G(maxn); buildMap(edgelist,G); //isCyclicUtil(edgelist, G); //isReachable(edgelist, G, 1, 1); if(isBipartitie(edgelist, G, )) cout<<"YES"<<endl;
else cout<<"NO"<<endl; return ;
}
bool isCyclic(vector<edge> edgelist, vector<vector<int> > G,vector<bool> vis, vector<bool> RecStack, int v){
for(int i=;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;
}
void isCyclicUtil(vector<edge> edgelist, vector<vector<int> > G){// find all cycles.
vector<bool> vis(G.size());
vector<bool> RecStack(G.size());
for(int i=;i<vis.size();++i) vis[i]=false;
for(int i=;i<RecStack.size();++i) RecStack[i]=false; for(int i=;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;
}
}
}
void addEdge(vector<edge> &edgelist, vector<vector<int> > &G, int from, int to){
edgelist.push_back(edge(to));
G[from].push_back(edgelist.size()-);
}
void addDoubleEdge(vector<edge> &edgelist, vector<vector<int> > &G, int from, int to){
addEdge(edgelist,G,from,to);
addEdge(edgelist,G,to,from);
}
bool dfs(vector<edge> edgelist, vector<vector<int> > G, vector<bool> vis, int from, int to){
if(from == to) return true;
for(int i=;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;
}
void isReachable(vector<edge> edgelist, vector<vector<int> > G, int from, int to){
vector<bool> vis(G.size());
for(int i=;i<vis.size();++i) vis[i] = false;
vis[from] = true;
if(dfs(edgelist, G, vis, from, to)) cout<<from<<" and "<<to<<" are reachable to each other"<<endl;
else cout<<from<<" and "<<to<<" are not reachable to each other"<<endl;
}
bool isBipartitie(vector<edge> edgelist, vector<vector<int> > G,int v){
vector<int> color(G.size());
for(int i=;i<color.size();++i) color[i] = -;
stack<int> st;
while(!st.empty()) st.pop(); st.push(v); color[v]=;// 1 stands for RED, and 0 stands for BLUE, -1 stands for non-colored. while(!st.empty()){
int k = st.top(); st.pop(); for(int i=;i<G[k].size();++i){
edge e = edgelist[G[k][i]];
if(color[e.to] == -){
color[e.to] = - color[k];
st.push(e.to);
}
else if(color[e.to] == color[k]) return false;
}
}
return true;
}

dataStructure@ Check whether a given graph is Bipartite or not的更多相关文章

  1. dataStructure@ Check if a directed graph has cycles

    #include<iostream> #include<cstdio> #include<cstring> #include<limits> #incl ...

  2. Geeks - Check whether a given graph is Bipartite or not 二分图检查

    检查一个图是否是二分图的算法 使用的是宽度搜索: 1 初始化一个颜色记录数组 2 利用queue宽度遍历图 3 从随意源点出发.染色0. 或1 4 遍历这点的邻接点.假设没有染色就染色与这个源点相反的 ...

  3. LeetCode 785. Is Graph Bipartite?

    原题链接在这里:https://leetcode.com/problems/is-graph-bipartite/ 题目: Given an undirected graph, return true ...

  4. Is Graph Bipartite?

    Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...

  5. [LeetCode] Is Graph Bipartite? 是二分图么?

    Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...

  6. [Swift]LeetCode785. 判断二分图 | Is Graph Bipartite?

    Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...

  7. LeetCode - Is Graph Bipartite?

    Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...

  8. [LeetCode] 785. Is Graph Bipartite?_Medium tag: DFS, BFS

    Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...

  9. 785. Is Graph Bipartite?

    Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...

随机推荐

  1. CSU1326+背包+并查集

    先预处理出有多少个任务即可 #include<stdio.h> #include<stdlib.h> #include<string.h> #include< ...

  2. Google Play市场考察报告

    考察了Google Play日本市场的10款应用,考察的重点在于每个App有什么亮点,盈利模式在哪里.本文并不是App的功能介绍. (1)恋爱文集[文库类应用] 该应用收录了一些恋爱文章,其主要受众是 ...

  3. hibernate 数据行数统计 count(*)

    Hibernate关于sql中的count(*)数据统计: ①如果使用的是HQL: 直接在HQL中使用count(*)即可获取行数 Long count = (Long)HibernateUtil.g ...

  4. 非常好的Demo网站

    http://www.xdemo.org/

  5. SQLite入门与分析(二)---设计与概念(续)

    SQLite入门与分析(二)---设计与概念(续)   写在前面:本节讨论事务,事务是DBMS最核心的技术之一.在计算机科学史上,有三位科学家因在数据库领域的成就而获ACM图灵奖,而其中之一Jim G ...

  6. 深入理解ob_flush和flush的区别

    ob_flush/flush在手册中的描述, 都是刷新输出缓冲区, 并且还需要配套使用, 所以会导致很多人迷惑… 其实, 他们俩的操作对象不同, 有些情况下, flush根本不做什么事情.. ob_* ...

  7. PHP 如何阻止用户上传成人照片或者裸照

    在这份教程中,我们将会学习到如何阻止用户通过PHP上传成人照片或者裸照. 示例   下载 我在phpclasses.org上面偶然发现一个很有用的,由Bakr Alsharif开发的可以帮助开发者基于 ...

  8. 到底怎么样才叫看书?——Tony Zhao's

    到底怎么样才叫看书?——上篇 目录: 一.引入 二.经历了就能理解 三.读书要分级 四.只读经典 五.别吝惜你动笔的那点时间 一.引入 看到这个题目的时候你可能会感到有点好笑:“这还用问,看书就是把书 ...

  9. C++静态成员变量和静态成员函数小结

    静态类成员包括静态数据成员和静态函数成员两部分. 一 静态数据成员: 类体中的数据成员的声明前加上static关键字,该数据成员就成为了该类的静态数据成员.和其他数据成员一样,静态数据成员也遵守pub ...

  10. java的Serialization 机制

    基本使用方法               Serialization是指把类或者基本的数据类型持久化(persistence)到数据流(Stream)中,包括文件.字节流.网络数据流.         ...