dataStructure@ Check whether a given graph is Bipartite or not
Check whether a given graph is Bipartite or not
A 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的更多相关文章
- dataStructure@ Check if a directed graph has cycles
		
#include<iostream> #include<cstdio> #include<cstring> #include<limits> #incl ...
 - Geeks - Check whether a given graph is Bipartite or not 二分图检查
		
检查一个图是否是二分图的算法 使用的是宽度搜索: 1 初始化一个颜色记录数组 2 利用queue宽度遍历图 3 从随意源点出发.染色0. 或1 4 遍历这点的邻接点.假设没有染色就染色与这个源点相反的 ...
 - LeetCode 785. Is Graph Bipartite?
		
原题链接在这里:https://leetcode.com/problems/is-graph-bipartite/ 题目: Given an undirected graph, return true ...
 - Is Graph Bipartite?
		
Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...
 - [LeetCode] Is Graph Bipartite? 是二分图么?
		
Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...
 - [Swift]LeetCode785. 判断二分图 | Is Graph Bipartite?
		
Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...
 - LeetCode - Is Graph Bipartite?
		
Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...
 - [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 ...
 - 785. Is Graph Bipartite?
		
Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...
 
随机推荐
- js 计时器
			
<html><head><script type="text/javascript">var count=0;var t ;function t ...
 - 【网络】IP地址分配、端口号、分层
			
3.网络分层 OSI的七层网络结构图和TCP/IP的五层结构图 OSI七层模型OSI中的层 功能 ...
 - c#做动态(gif)中文验证码
			
无意中在国外论坛发现一个gif动画类,我使用它来制作了一个动态验证码 : 一:首先新建一个类库 1:新建AnimatedGifEncoder类 using System; using System.C ...
 - linux 文件比对总结
			
1. 过滤a.log的重复数据 #统计 cat datatest.log|sort|uniq -d |wc -l #放入b.log cat datatest.log|sort|uniq -d > ...
 - P44、面试题4:替换空格
			
题目:请实现一个函数,把字符串中的每个空格替换成“%20”.例如输入“We are happy.”,则输出“We%20are%20happy.”. 如果用java string类中提供的replace ...
 - (从终端看linux-1)linux tty pty pts 概念 区别
			
基本概念: 1> tty(终端设备的统称):tty一词源于Teletypes,或者teletypewriters,原来指的是电传打字机,是通过串行线用打印机键盘通过阅读和发送信息的东西,后来这东 ...
 - EL表达式 JSTL中的常用EL函数 动态数据的国际化
			
ELppt: EL 全名为Expression Language.EL主要作用: 获取数据: •EL表达式主要用于替换JSP页面中的脚本表达式,以从各种类型的web域 中检索java对象.获取数据.( ...
 - NFC(5)编写NFC程序的基本步骤
			
1,设置权限 <uses-permission android:name="android.permission.NFC" /> 2,限制Android版本 <u ...
 - 【HDOJ】2385 Stock
			
水题,逆向做+优先级队列. /* 2385 */ #include <iostream> #include <sstream> #include <string> ...
 - poj2186Popular Cows(强连通分量)
			
http://poj.org/problem?id=2186 用tarjan算出强连通分量的个数 将其缩点 连成一棵树 则题目所求即变成求出度为0 的那个节点 在树中是唯一的 即树根 #includ ...