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. 第15章 高并发服务器编程(1)_非阻塞I/O模型

    1. 高性能I/O (1)通常,recv函数没有数据可用时会阻塞等待.同样,当socket发送缓冲区没有足够多空间来发送消息时,函数send会阻塞. (2)当socket在非阻塞模式下,这些函数不会阻 ...

  2. css sprite实例

    css sprite直译过来就是CSS精灵.通常被解释为“CSS图像拼合”或“CSS贴图定位”.本文章向码农们介绍css sprite使用方法和基本使用实例,需要的码农可以参考一下. 一.什么是css ...

  3. 关于oracle数据库压力测试

    今天接到需求,需要对oracle数据库进行压力测试,就这几个字的需求. 然后查看了以下软件: 1.Benchmark Factory是一款专业的服务器性能测试工具,专为数据库测试和可扩展性测量而设计, ...

  4. django 更新 模板语言

    Django模板系统 官方文档 常用语法 只需要记两种特殊符号: {{  }}和 {% %} 变量相关的用{{}},逻辑相关的用{%%}. 变量 在Django的模板语言中按此语法使用:{{ 变量名 ...

  5. node实现缓存

    内容: 1.缓存基本原理 2.node实现缓存 1.缓存基本原理 第一重要.缓存策略: cache-control:用于控制缓存,常见的取值有private.no-cache.max-age.must ...

  6. 0_Simple__MultiGPU

    使用多台 GPU 进行计算 ▶ 源代码.使用不同的流来控制不同 GPU 上的运算任务. #include <stdio.h> #include <timer.h> #inclu ...

  7. 比较完整的URL验证

    转自:http://wuchaorang.2008.blog.163.com/blog/static/4889185220135279223253/ function IsURL(str_url){v ...

  8. HTML5 Canvas ( 线性渐变, 升级版的星空 ) fillStyle, createLinearGradient, addColorStop

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. leetcode530

    /** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNo ...

  10. Simple2D-16(音乐播放器)ImGui 库介绍

    什么是 ImGui IMGUI (Immediate Mode Graphical User interface),下载地址. ImGui 是一种比较新颖的 GUI 实现模式,适用于显示区域实时刷新的 ...