二分图bipartite
使用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";
}
};