Given an undirected graph, return true if and only if it is bipartite.

Example 1:
Input: [[1,3], [0,2], [1,3], [0,2]]
Output: true
Explanation:
The graph looks like this:
0----1
| |
| |
3----2
We can divide the vertices into two groups: {0, 2} and {1, 3}.
Example 2:
Input: [[1,2,3], [0,2], [0,1,3], [0,2]]
Output: false
Explanation:
The graph looks like this:
0----1
| \ |
| \ |
3----2
We cannot find a way to divide the set of nodes into two independent subsets.

设G=(V,E)是一个无向图。如顶点集V可分割为两个互不相交的子集V1,V2之并,并且图中每条边依附的两个顶点都分别属于这两个不同的子集

思路

1. based on Graph Bipartite attribute, we can fill two different color for each subset.
2. if not Graph Bipartite, at lease one node such that its color happens to be the same as its neighbor
3. coz we need to traversal each node, we can both use dfs and bfs

代码

 class Solution {
public boolean isBipartite(int[][] graph) {
int[] visited = new int[graph.length];
//default 0: not visited;
//lable 1: green
//lable 2: red
for(int i = 0; i < graph.length; i++) {
// such node has been visited
if(visited[i] != 0) {continue;}
//such node has not been visited
Queue<Integer> queue = new LinkedList();
queue.add(i);
// mark as green
visited[i] = 1;
while(!queue.isEmpty()) {
int cur = queue.poll();
int curLable = visited[cur];
// if curLable is green, fill neighborLable to red
int neighborLable = curLable == 1? 2:1;
for(int neighbor:graph[cur]) {
//such node has not been visited
if(visited[neighbor] == 0) {
visited[neighbor] = neighborLable;
queue.add(neighbor);
}
// node visited, and visited[neighbor] != neighborLable, conflict happens
else if(visited[neighbor] != neighborLable) {
return false;
}
}
}
}
return true;
}
}

[leetcode]785. Is Graph Bipartite? [bai'pɑrtait] 判断二分图的更多相关文章

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

    Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...

  2. LeetCode 785. Is Graph Bipartite?

    原题链接在这里:https://leetcode.com/problems/is-graph-bipartite/ 题目: Given an undirected graph, return true ...

  3. [LeetCode] 785. Is Graph Bipartite?_Medium tag: DFS, BFS

    Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...

  4. 【LeetCode】785. Is Graph Bipartite? 解题报告(Python)

    [LeetCode]785. Is Graph Bipartite? 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu. ...

  5. 785. Is Graph Bipartite?

    Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...

  6. 785. Is Graph Bipartite?从两个集合中取点构图

    [抄题]: Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is  ...

  7. [Swift]LeetCode785. 判断二分图 | Is Graph Bipartite?

    Given an undirected graph, return true if and only if it is bipartite. Recall that a graph is bipart ...

  8. 【LeetCode】图论 graph(共20题)

    [133]Clone Graph (2019年3月9日,复习) 给定一个图,返回它的深拷贝. 题解:dfs 或者 bfs 都可以 /* // Definition for a Node. class ...

  9. Java实现 LeetCode 785 判断二分图(分析题)

    785. 判断二分图 给定一个无向图graph,当这个图为二分图时返回true. 如果我们能将一个图的节点集合分割成两个独立的子集A和B,并使图中的每一条边的两个节点一个来自A集合,一个来自B集合,我 ...

随机推荐

  1. linux中nmcli命令使用及网络配置

    nmcli命令与配置文件对应关系  主机名: 如果说你没有设置主机名的话,默认是localhost.localdomain 修改配置文件的主机名 # hostnamectl  set-hostname ...

  2. [UE4]C++创建对象的三种方式

    #include <iostream> using namespace std; class A { private: int n; public: A(int m):n(m) { } ~ ...

  3. Oracle中分页查询和联表查询

    1.使用ROWNUM伪列查询 1.1.查询十条数据(rownum<=n) SELECT ROWNUM,A.* FROM v_sjjx_unit_info A WHERE ROWNUM<=1 ...

  4. 统计numpy数组中每个值的个数

    import numpy as np from collections import Counter data = np.array([1.1,2,3,4,4,5]) Counter(data) #简 ...

  5. javascript面向对象的程序设计之Object.getOwnPropertyDescriptor()

    Object.getOwnPropertyDescriptor()用于获取给定属性的描述信息,这个描述信息是一个对象. 如果是访问器属性,则这个对象的属性有configurable,enumerabl ...

  6. Log4net详细说明(全)

    转自:http://www.cnblogs.com/zhangchenliang/p/4546352.htmlhttp://www.cnblogs.com/zhangchenliang/p/45463 ...

  7. [ffmpeg_3.3.2]demuxing_decoding.c

    分析ffmpeg3.3.2的example: 由于ffmpeg文档比较少,而且API变化表较大,所以个人首先从ffmpeg自带的demo开始分析,分析(demuxing_decoding.c) 1:首 ...

  8. 为solr增加用户验证

    添加此功能主要是为了增加solr服务器的安全性,不能随便让人访问. 1.      在tomcat的F:\Tomcat 6.0.26_solr\conf\tomcat-users.xml添加用户角色并 ...

  9. 了解ES6

    内容: 1.ES6介绍及基础 2.模块.类和继承 3.ES6高级特性 4.Generator和Iterator 5.异步编程 6.函数相关 内容参考:<ES6 标准入门> ES6标准阅读链 ...

  10. linux上安装vsftpd

    介绍:在前几篇博客中博主介绍了,怎么用java语言搭建一个简单的网站.如果有些小伙伴想把自己做的网站发布到服务器上让别人访问的话,不妨可以关注博主的博客,博客会在接下来的几篇博客中介绍怎么把一个网站发 ...