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. P1449 后缀表达式

    题目描述 所谓后缀表达式是指这样的一个表达式:式中不再引用括号,运算符号放在两个运算对象之后,所有计算按运算符号出现的顺序,严格地由左而右新进行(不用考虑运算符的优先级). 如:3*(5–2)+7对应 ...

  2. 炸掉的fft,改天再调

    #include <iostream> #include <cstdio> #include <cmath> #include <algorithm> ...

  3. python3 数据类型测试

    type(要测试的数据):

  4. mysql的存储引擎与锁

    一.背景知识 1.锁是计算机协调多个进程或线程并发访问某一资源的机制. A.锁分类. | 共享锁(读锁):在锁定期间,多个用户可以读取同一个资源,读取过程中数据不会发生变化. | 排他锁(写锁):在锁 ...

  5. GO- 使用JSON

    1 json.Marshal  把对象转换为JSON的方法 原型如下 func Marshal(v interface{}) ([]byte, error)这个函数接收任意类型的数据 v,并转换为字节 ...

  6. mysql8安装与卸载

    参考: https://www.cnblogs.com/zxwen/p/9448797.html https://blog.csdn.net/weixin_30073553/article/detai ...

  7. maven插件上传本地jar包到maven中央仓库

    settings配置(如果设置后有问题,可以重启idea,保证重新加载settings文件): <!-- 上传jar包到maven中央仓库配置start --> <server> ...

  8. TXMLDocument 的使用

    TXMLDocument 的使用 TXMLDocument是DELPHI自带的操作XML的类. 需要它,需要引用单元: uses XMLDoc; var XMLDoc:TXMLDocument; XM ...

  9. 如何配置 VirtualBox 中的客户机与宿主机之间的网络连接

    如何配置 VirtualBox 中的客户机与宿主机之间的网络连接 作者: Aaron Kili 译者: LCTT rusking | 2017-03-01 13:40   评论: 3 收藏: 3 当你 ...

  10. appium-FAQ(持续更新...)

    Q1:未安装APP直接启用appium sever,初始化driver :driver = new AndroidDriver(new URL("http://127.0.0.1:4723/ ...