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

Recall that a graph is bipartite if we can split it's set of nodes into two independent subsets A and B such that every edge in the graph has one node in A and another node in B.

The graph is given in the following form: graph[i] is a list of indexes j for which the edge between nodes i and j exists.  Each node is an integer between 0 and graph.length - 1.  There are no self edges or parallel edges: graph[i] does not contain i, and it doesn't contain any element twice.

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.
分析:

Our goal is trying to use two colors to color the graph and see if there are any adjacent nodes having the same color.
Initialize a color[] array for each node. Here are three states for colors[] array:
0: Haven't been colored yet.
1: Blue.
-1: Red.
For each node,

  1. If it hasn't been colored, use a color to color it. Then use the other color to color all its adjacent nodes (DFS).
  2. If it has been colored, check if the current color is the same as the color that is going to be used to color it.

DFS

 class Solution {
public boolean isBipartite(int[][] graph) {
int n = graph.length;
// 0 unvisited 1 red -1 blue
int[] colors = new int[n];
//This graph might be a disconnected graph. So check each unvisited node.
for (int i = ; i < n; i++) {
if (colors[i] == ) {
if (!validColor(graph, colors, , i)) {
return false;
}
}
}
return true;
} public boolean validColor(int[][] graph, int[] colors, int color, int node) {
colors[node] = color;
for (int next : graph[node]) {
if (colors[next] == && !validColor(graph, colors, -color, next)) {
return false;
} else if (colors[next] == colors[node]) {
return false;
}
}
return true;
}
}

BFS

 class Solution {
public boolean isBipartite(int[][] graph) {
// 0: unvisited; 1: black; 2: white
int[] status = new int[graph.length];
// for loop is used to handle unconnected components in the graph
for (int i = ; i < graph.length; i++) {
if (status[i] == ) {
status[i] = ;
if (!bfs(i, status, graph)) {
return false;
};
}
}
return true;
} private boolean bfs(int i, int[] status, int[][] graph) {
Queue<Integer> queue = new LinkedList<>();
queue.offer(i);
while (!queue.isEmpty()) {
int current = queue.poll();
for (int neighbor : graph[current]) {
if (status[neighbor] == ) {
status[neighbor] = (status[current] == ) ? : ;
queue.offer(neighbor);
} else {
if (status[neighbor] == status[current]) return false;
}
}
}
return true;
}
}

Is Graph Bipartite?的更多相关文章

  1. [leetcode]785. Is Graph Bipartite? [bai'pɑrtait] 判断二分图

    Given an undirected graph, return true if and only if it is bipartite. Example 1: Input: [[1,3], [0, ...

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

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

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

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

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

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

  5. LeetCode - Is Graph Bipartite?

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

  6. [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 ...

  7. 785. Is Graph Bipartite?

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

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

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

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

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

  10. LeetCode 785. Is Graph Bipartite?

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

随机推荐

  1. 斐波那契数列 矩阵乘法优化DP

    斐波那契数列 矩阵乘法优化DP 求\(f(n) \%1000000007​\),\(n\le 10^{18}​\) 矩阵乘法:\(i\times k\)的矩阵\(A\)乘\(k\times j\)的矩 ...

  2. Linux--系统调优

    建议看看:https://www.cnblogs.com/yinzhengjie/p/9994207.html 一.关闭swap交换分区 Linux swapoff命令用于关闭系统交换区(swap a ...

  3. ICEM-缺角正方体

    原视频下载地址:https://yunpan.cn/cqKwGiMRiqN7I  访问密码 3377

  4. rsyslog使用简介

    1 把日志打到新的日志文件里面 #### RULES ####ruleset(name="remote"){ action(type="omfile" file ...

  5. 微信小程序侧边栏滑动特效(左右滑动)

    侧边栏滑动是很常见的功能,但是小程序出来不久,很多特效还没有成熟案例,只能原生重写,所以今天为大家带来4个漂亮的侧边栏特效~~ 侧边栏特效一 先看效果: wxml: <!--page/one/i ...

  6. DELPHI解析JSON格式化的日期

    DELPHI解析JSON格式化的日期 json返回的日期是 /Date(1560355200000)/ 这样的格式. 这个1560355200000,是指1970年以后的秒数. DELPHI如何解析这 ...

  7. Maven多镜像配置

    Maven阿里云镜像相信国内用得是很爽的,但有时候,一些版本的包明明可以在http://mvnrepository.com上搜索到.但你确实下载不来... 废话不多,settings.xml多镜像配置 ...

  8. decimal赋值

    decimal dRebate1 = new decimal(1);decimal dRebate2 = Convert.ToDecimal(1);decimal dRebate3 = 1M; htt ...

  9. Docs-.NET-C#-指南-语言参考-预处理器指令:#elif(C# 参考)

    ylbtech-Docs-.NET-C#-指南-语言参考-预处理器指令:#elif(C# 参考) 1.返回顶部 1. #elif(C# 参考) 2015/07/20 #elif 可以创建复合条件指令. ...

  10. Qt编写数据导出到Excel及Pdf和打印数据

    一.前言 用Qt开发已经九年了,期间用Qt做过不少的项目,在各种项目中有个功能很常用,尤其是涉及到数据记录存储的项目,那就是需要对查询的数据进行导出到Excel,或者导出到Pdf文件,或者直接打印查询 ...