以后用英文写注释了,练练一手。
链接:https://leetcode-cn.com/problems/is-graph-bipartite/
中文题目:
给定一个无向图graph,当这个图为二分图时返回true。
如果我们能将一个图的节点集合分割成两个独立的子集A和B,并使图中的每一条边的两个节点一个来自A集合,一个来自B集合,我们就将这个图称为二分图。
graph将会以邻接表方式给出,graph[i]表示图中与节点i相连的所有节点。每个节点都是一个在0到graph.length-1之间的整数。这图中没有自环和平行边: graph[i] 中不存在i,并且graph[i]中没有重复的值。
英文题目:
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.
示例 1:
输入: [[1,3], [0,2], [1,3], [0,2]]
输出: true
解释:
无向图如下:
0----1
| |
| |
3----2
我们可以将节点分成两组: {0, 2} 和 {1, 3}。
示例 2:
输入: [[1,2,3], [0,2], [0,1,3], [0,2]]
输出: false
解释:
无向图如下:
0----1
| \ |
| \ |
3----2
我们不能将节点分割成两个独立的子集。
注意:
graph 的长度范围为 [1, 100]。
graph[i] 中的元素的范围为 [0, graph.length - 1]。
graph[i] 不会包含 i 或者有重复的值。
图是无向的: 如果j 在 graph[i]里边, 那么 i 也会在 graph[j]里边。
思路:
This a typically coloring question,we use different color to painting the contiguous node, every initial node are been setted color 0. so we are paiting from the first node, every time wo painting one node and painting neighbors with opposite color, and we can judge the color is not a Graph Bipartite when meet the same color with neighbor.
题解:
class Solution {
public boolean isBipartite(int[][] graph) {
int[] colors = new int[graph.length];
for (int i = 0; i < graph.length; i++) {
// 碰到做过标记的直接略过
if (colors[i] != 0) {
continue;
}
// 使用队列记录遇到的颜色
Queue<Integer> queue = new LinkedList<>();
queue.offer(i);
// blue: 1 , red : -1
colors[i] = 1;
// BFS
while(!queue.isEmpty()) {
int cur = queue.poll();
for (int next : graph[cur]) {
if (colors[next] == 0) {
// 用不同色渲染相邻节点
colors[next] = -colors[cur];
// BFS记录该节点
queue.offer(next);
} else if (colors[next] != -colors[cur]) {
// 碰到相邻不同色判断为假
return false;
}
}
}
}
return true;
}
}