Leetcode 785 判断二分图 BFS 二分染色

地址 https://leetcode-cn.com/problems/is-graph-bipartite/

给定一个无向图graph,当这个图为二分图时返回true。

如果我们能将一个图的节点集合分割成两个独立的子集A和B,并使图中的每一条边的两个节点一个来自A集合,一个来自B集合,我们就将这个图称为二分图。

graph将会以邻接表方式给出,graph[i]表示图中与节点i相连的所有节点。每个节点都是一个在0到graph.length-1之间的整数。这图中没有自环和平行边: graph[i] 中不存在i,并且graph[i]中没有重复的值。


示例 1:
输入: [[1,3], [0,2], [1,3], [0,2]]
输出: true
解释: 
无向图如下:
0----1
|    |
|    |
3----2
我们可以将节点分成两组: {0, 2} 和 {1, 3}。

示例 2:
输入: [[1,2,3], [0,2], [0,1,3], [0,2]]
输出: false
解释: 
无向图如下:
0----1
|   |
|   |
3----2
我们不能将节点分割成两个独立的子集。
注意:

graph 的长度范围为 [1, 100]。
graph[i] 中的元素的范围为 [0, graph.length - 1]。
graph[i] 不会包含 i 或者有重复的值。
图是无向的: 如果j 在 graph[i]里边, 那么 i 也会在 graph[j]里边。

解法:

使用bfs遍历图,相邻的两个点染不同颜色 

由于使用BFS 保证一次BFS能遍历从某点 出发所有相邻的点

所以只要遍历一次 遇到没染色的点就以它为起点BFS染色就能保证覆盖全图

有染色冲突 则返回失败 否则就是二分图

代码

 1 class Solution {
 2 public:
 3    
 4    vector<int> color;
 5 
 6 bool bfs(vector<vector<int>>& graph, int idx)
 7 {
 8     queue<int> q;
 9 
10     int currentColor = 1;
11     color[idx] = currentColor;
12     q.push(idx);
13 
14     while (!q.empty()) {
15         int point = q.front(); q.pop();
16         currentColor = color[point];
17         //与其相连的点需要填充另一种颜色
18         for (int i = 0; i < graph[point].size(); i++) {
19             int nextPoint = graph[point][i];
20             if (color[nextPoint] != 0 && color[nextPoint] != 3-currentColor) {
21                 //颜色冲突 返回失败
22                 return false;
23             }
24             if(color[nextPoint] == 0){
25                 color[nextPoint] = 3-currentColor;
26                 q.push(nextPoint);
27             }
28         }
29         
30     }
31 
32     return true;
33 }
34 
35 bool isBipartite(vector<vector<int>>& graph) {
36     int count = graph.size();
37     color.resize(count, 0);
38 
39     for (int i = 0; i < count; i++) {
40         if (color[i] == 0) {
41             //bfs 染色
42             if (bfs(graph, i) == false) return false;
43         }
44     }
45 
46     return true;
47 }
48 
49 };
View Code
class Solution {
public:
   

int color[200];

bool bfs(vector<vector<int>>& graph,int p) {
    queue<int> q;

    q.push(p);
    color[p] = 1;
    
    while (q.size()) {
        int from = q.front(); q.pop();
        int otherColor = 3 - color[from];
        for (int i = 0; i < graph[from].size(); i++) {
            int to = graph[from][i];
            if (color[to] == 0) {
                color[to] = otherColor;
                q.push(to);
            }
            else if (color[to] == color[from]) return false;
        }
    }


    return true;
}

bool isBipartite(vector<vector<int>>& graph) {
    memset(color, 0, sizeof color);
    for (int i = 0; i < graph.size(); i++) {
        for (int j = 0; j < graph[i].size(); j++) {
            int p = graph[i][j];
            if (color[p] == 0) {
                if(false == bfs(graph,p)) return false;
            }
        }
    }

    return true;
}

    
};
View Code
作 者: itdef
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力
阿里打赏 微信打赏
原文地址:https://www.cnblogs.com/itdef/p/12539738.html