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. spark sql 的性能调优

    Caching Data in Memory 其他调优参数

  2. solr 通过【配置、多值字段、动态字段】来解决文本表达式查询精确到句子的问题

    一.Solr Multivalue field属性positionIncrementGap理解 分类:Lucene 2014-01-22 10:39阅读(3596)评论(0) 参考:http://ro ...

  3. css position小结

    relative:可使top,right,bottom,left等相对于自身位置来进行偏移:若无则这些偏移都不会起作用 absolute:寻找离自己最近position为relative或absolu ...

  4. java内存模型(一)正确使用 Volatile 变量

    文章转载自: 正确使用 Volatile 变量   Java 语言中的 volatile 变量可以被看作是一种 "程度较轻的 synchronized":与 synchronize ...

  5. 微信小程序教程系列

    微信小程序教程系列 来源:       https://blog.csdn.net/michael_ouyang/article/details/56846185 相关连接:http://blog.c ...

  6. 马尔可夫毯(Markov Blanket)

    马尔可夫毯(Markov Blanket) 最近接触到马尔可夫毯(MarkovBlanket)这个概念,发现网上资料不多,通俗易懂的解释甚少,查了一些资料后,决定写一个总结. 提到马尔可夫毯,就会有一 ...

  7. WPF 自定义属性

    做了一个自定义控件和一个自定义Grid,里面的元素可以随着绑定属性变化: 效果图(一定滑块): 关键代码: 1.自定义属性代码: public class MyGrid : Grid { public ...

  8. springboot sybase 数据库

    依赖:(驱动) <!-- https://mvnrepository.com/artifact/net.sourceforge.jtds/jtds --> <dependency&g ...

  9. ajax 实现跨域

    ajax本身是不可以跨域的,通过产生一个script标签来实现跨域.因为script标签的src属性是没有跨域的限制的. 其实设置了dataType: 'jsonp'后,$.ajax方法就和ajax ...

  10. datasnap 如何监控客户端的连接情况

    如果客户端是TCP/IP是短连接的情况就没有必要了. type pClientConns = ^TClientConns; // 客户连接 TClientConns = record clientid ...