Geeks - Check whether a given graph is Bipartite or not 二分图检查
2017-04-22 15:35
453 查看
检查一个图是否是二分图的算法
使用的是宽度搜索:
1 初始化一个颜色记录数组
2 利用queue宽度遍历图
3 从随意源点出发。染色0。 或1
4 遍历这点的邻接点。假设没有染色就染色与这个源点相反的颜色,假设已经染色而且和源点的值相反。那么就是合法点,假设是同样的颜色。那么就不能是二分图
參考:http://www.geeksforgeeks.org/bipartite-graph/
使用的是宽度搜索:
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 二分图检查
- dataStructure@ Check whether a given graph is Bipartite or not
- *Check whether a given graph is Bipartite or not
- Check whether a given Binary Tree is Complete or not 解答
- check a string is numeric or not? 检查字符串是否是数值型的
- 判断是否为完全二叉树 Check whether a binary tree is a complete tree or not
- 第15题 To Check the given linked list is palindrome or not
- Check if the given binary tree is BST or not.
- How To Check Whether Physical Standby is in Sync with the Primary or Not?
- CareerCup Find whether a given number is a perfect square or not.(Addition and Subtraction only)
- FW - check whether tree is balanced or not - find the max and min of one tree- 2013年12月18日13:12:04
- VB.net check whether a file is opened or not
- check whether the crystal report runtime is exists 检查crystalreport运行时是否存在
- leetCode Given an integer (signed 32 bits), write a function to check whether it is a power of 4
- VSTO Check Cell is In Edit Mode or Not
- Node.js is taking over the Enterprise – whether you like it or not
- Adobe--Without using /,% ,+and * operators. write a function to check whether a number is divisible by 3 or nor?
- Q4.2 Given a directed graph, design an algorithm to find out whether there is a route.
- 【Leetcode】Given a binary tree, check whether it is a mirror of itself
- How to Check Whether API Server is up or Down