Geeks - Check whether a given graph is Bipartite or not 二分图检查

检查一个图是否是二分图的算法

使用的是宽度搜索:

1 初始化一个颜色记录数组

2 利用queue宽度遍历图

3 从任意源点出发,染色0, 或1

4 遍历这点的邻接点,如果没有染色就染色与这个源点相反的颜色,如果已经染色并且和源点的值相反,那么就是合法点,如果是相同的颜色,那么就不能是二分图

 

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

#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";
	}
};

Geeks - Check whether a given graph is Bipartite or not 二分图检查

时间: 2024-10-12 00:36:57

Geeks - Check whether a given graph is Bipartite or not 二分图检查的相关文章

[email&#160;protected] 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, fo

[LeetCode] Is Graph Bipartite? 是二分图么?

Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipartite if we can split it's set of nodes into two independent subsets A and B such that every edge in the graph has one node in A and another node in B.

785.Is Graph Bipartite?

Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipartite if we can split it's set of nodes into two independent subsets A and B such that every edge in the graph has one node in A and another node in B.

785. Is Graph Bipartite?( 判断是否为二分图)

Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipartite if we can split it's set of nodes into two independent subsets A and B such that every edge in the graph has one node in A and another node in B.

LF.56.Bipartite

1 /* 2 Determine if an undirected graph is bipartite. A bipartite graph is one in which the nodes can be divided into two groups such that no nodes have direct edges to other nodes in the same group. 3 4 Examples 5 6 1 -- 2 7 8 / 9 10 3 -- 4 11 12 is

关于Failed to check the status of the service com.taotao.service.ItemService. No provider available for the service【已解决】

项目中用dubbo发生: Failed to check the status of the service com.taotao.service.ItemService. No provider available for the service 原因: Dubbo缺省会在启动时检查依赖的服务是否可用,不可用时会抛出异常,阻止Spring初始化完成,以便上线时,能及早发现问题,默认check=true. 如果你的Spring容器是懒加载的,或者通过API编程延迟引用服务,请关闭check,否则

with check(转)

假如我要为一个表中添加一个外键约束.语法如下 alter table  dbo.employee with check add constraint [FK_employeeno]  foreign key ([colorcode]) references  dbo.color([colorcode]) 其中的with check /nocheck 的作用: 指定表中的数据是否用新添加的或者重新启用的foregin key 或者check 进行验证. check :对添加验证之前的数据也进行验证

UVA - 10004 - Bicoloring (简单图论-着色判断)

UVA - 10004 Bicoloring Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description  Bicoloring  In 1976 the ``Four Color Map Theorem" was proven with the assistance of a computer. This theorem states that e

UVA - 12532 Interval Product

http://acm.bnu.edu.cn/v3/problem_show.php?pid=27853 Interval Product Time Limit: 2000ms Memory Limit: 131072KB This problem will be judged on UVA. Original ID: 1253264-bit integer IO format: %lld Java class name: Main Prev Submit Status Statistics Di