BFS检查c中的图是否为二分图
作者:互联网
我正在实现一种算法来确定无向图是否为二分图.基于this pseudo-code制作了我的实现,它适用于连接的图形,但当它断开连接时,只是程序指示错误的答案.我认为如果它没有连接,那么每个不相交的子图需要一个循环.但我坚持这一点.我如何能够为我打印正确答案的代码?
#include <cstdio>
#include <vector>
#include <queue>
#include <iostream>
using namespace std;
#define MAX 1000
int numberVertex, numberEdges;
int particion[MAX], visited[MAX];
vector< int > adjacencyMatrix[MAX];
bool bfs()
{
int i, origin, destination, begin;
queue< int > queueVertex;
begin = 0;
queueVertex.push(begin);
particion[begin] = 1; // 1 left,
visited[begin] = 1; // set adjacencyMatrixray
while(!queueVertex.empty())
{
origin = queueVertex.front(); queueVertex.pop();
for(i=0; i < adjacencyMatrix[origin].size(); i++)
{
destination = adjacencyMatrix[origin][i];
if(particion[origin] == particion[destination])
{
return false;
}
if(visited[destination] == 0)
{
visited[destination] = 1;
particion[destination] = 3 - particion[origin]; // alter 1 and 2 subsets
queueVertex.push(destination);
}
}
}
return true;
}
int main()
{
freopen("tarea2.in", "r", stdin);
int i,j, nodeOrigin, nodeDestination;
scanf("%d %d", &numberVertex, &numberEdges);
for(i=0; i<numberEdges; i++)
{
scanf("%d %d", &nodeOrigin, &nodeDestination);
adjacencyMatrix[nodeOrigin].push_back(nodeDestination);
adjacencyMatrix[nodeDestination].push_back(nodeOrigin);
}
if(bfs()) {
printf("Is bipartite\n");
for (j=0; j<numberVertex; j++){
cout<<j<<" "<<particion[j]<<endl;
}
}
else {printf("Is not bipartite\n");}
return 0;
}
例如,对于此输入
6 4
3 0
1 0
2 5
5 4
输出应该是:
Is bipartite
0 1
1 2
2 1
3 2
4 1
5 2
而是抛出输出:
0 1
1 2
2 0
3 2
4 0
5 0
发生这种情况是因为图形不是连接的图形,即有两个连接的组件.我希望你能帮助我,因为我已经坚持了几天.
解决方法:
您应该在每个连接的组件上运行bfs.最简单的方法是迭代所有顶点,如果没有访问它们,那么只需在它们上调用bfs即可.
bool is_bipartite()
{
for(int i = 0; i < numberVertex; i++)
{
if (visited[i] == 0 && !bfs(i)) {
return false;
}
}
return true;
}
它仍然是线性的,因为您在每个连接的组件上运行一次bfs.
#include <cstdio>
#include <vector>
#include <queue>
#include <iostream>
using namespace std;
#define MAX 1000
int numberVertex, numberEdges;
int particion[MAX], visited[MAX];
vector< int > adjacencyMatrix[MAX];
bool bfs(int begin)
{
int i, origin, destination;
queue< int > queueVertex;
queueVertex.push(begin);
particion[begin] = 1; // 1 left,
visited[begin] = 1; // set adjacencyMatrixray
while(!queueVertex.empty())
{
origin = queueVertex.front(); queueVertex.pop();
for(i=0; i < adjacencyMatrix[origin].size(); i++)
{
destination = adjacencyMatrix[origin][i];
if(particion[origin] == particion[destination])
{
return false;
}
if(visited[destination] == 0)
{
visited[destination] = 1;
particion[destination] = 3 - particion[origin]; // alter 1 and 2 subsets
queueVertex.push(destination);
}
}
}
return true;
}
bool is_bipartite()
{
for(int i=0; i< numberVertex; i++)
{
if (visited[i] == 0 && !bfs(i)) {
return false;
}
}
return true;
}
int main()
{
//freopen("tarea2.in", "r", stdin);
int i,j, nodeOrigin, nodeDestination;
scanf("%d %d", &numberVertex, &numberEdges);
for(i=0; i<numberEdges; i++)
{
scanf("%d %d", &nodeOrigin, &nodeDestination);
adjacencyMatrix[nodeOrigin].push_back(nodeDestination);
adjacencyMatrix[nodeDestination].push_back(nodeOrigin);
}
if(is_bipartite()) {
printf("Is bipartite\n");
for (j=0; j<numberVertex; j++){
cout<<j<<" "<<particion[j]<<endl;
}
}
else {printf("Is not bipartite\n");}
return 0;
}
标签:breadth-first-search,c,graph-theory,bipartite 来源: https://codeday.me/bug/20190723/1513405.html