BFS判断是否是二分图Bipartite算法

来源:互联网 发布:域名交易平台源码 编辑:程序博客网 时间:2024/06/04 18:33

二分图bipartite

Bipartite1

使用BFS广度优先判断一个图是否是二分图。基本图操作。

参考

http://www.geeksforgeeks.org/bipartite-graph/

#pragma once#include <stdio.h>#include <iostream>#include <queue>using namespace std;class CheckwhetheragivengraphisBipartiteornot{const static int V = 4;bool isBipartite(int G[][V], int src){int colors[V];fill(colors, colors+V, -1);colors[src] = 1;queue<int> qu;qu.push(src);while (qu.size()){int u = qu.front();qu.pop();for (int v= 0; v < V; v++){if (G[u][v] && colors[v] == -1){colors[v] = 1 - colors[u];qu.push(v);}else if (G[u][v] && colors[v] == colors[u]) return false;}}return true;}public:CheckwhetheragivengraphisBipartiteornot(){int G[][V] = {{0, 1, 0, 1},{1, 0, 1, 0},{0, 1, 0, 1},{1, 0, 1, 0}};isBipartite(G, 0) ? cout << "Yes" : cout << "No";}};



1 0
原创粉丝点击