dataStructure@ Check whether a given graph is Bipartite or not
2015-10-24 10:09
513 查看
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.#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 = 0; } }; void addEdge(vector<edge> &, vector<vector<int> > &, int, int);//add directed edge. void buildMap(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); } 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 = 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; else cout<<"NO"<<endl; return 0; } bool isCyclic(vector<edge> edgelist, vector<vector<int> > G,vector<bool> vis, vector<bool> RecStack, int v){ for(int i=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; } 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=0;i<vis.size();++i) vis[i]=false; for(int i=0;i<RecStack.size();++i) RecStack[i]=false; for(int i=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; } } } void addEdge(vector<edge> &edgelist, vector<vector<int> > &G, int from, int to){ edgelist.push_back(edge(to)); G[from].push_back(edgelist.size()-1); } 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=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; } void isReachable(vector<edge> edgelist, vector<vector<int> > G, int from, int to){ vector<bool> vis(G.size()); for(int i=0;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=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()){ int k = st.top(); st.pop(); for(int i=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
相关文章推荐
- nyoj 82 迷宫寻宝(一)(广搜)
- c++学习笔记(十):运算符重载综合——自定义数组类
- springmvc中controller返回html页面时页面乱码解法
- LinuxCNC+EtherCAT(2)~~~~~~
- hash
- MySQL数据库的同步配置+MySql读写分离
- request.getParameter() 和request.getAttribute() 区别
- 网站制作笔记一域名购买与主机备案
- Leetcode -- Median of Two Sorted Arrays
- Android Sqlite
- js正则
- ios开发之倒计时实现的两种方法
- 系统利益相关者描述案例
- pcb板中字母P代表什么
- NET基础课--Linq第一讲
- 基于Python的江苏大学校园网暴力破解
- 欢迎使用CSDN-markdown编辑器
- 推荐外链网盘 OpenDrive
- Android 异步消息处理机制 让你深入理解 Looper、Handler、Message三者关系
- 创建第一个android 应用程序时,SDK和ADT一定要保持版本一致。