如果邻居没有染色, 则染为另一种颜色。即 color * - 1,其中 color 为当前节点的颜色
否则,判断当前节点和邻居的颜色是否一致,不一致则返回 False,否则返回 True
强烈建议两道题一起练习一下。
关键点
图的建立和遍历
colors 数组
代码
class Solution:
def dfs(self, grid, colors, i, color, N):
colors[i] = color
for j in range(N):
if grid[i][j] == 1:
if colors[j] == color:
return False
if colors[j] == 0 and not self.dfs(grid, colors, j, -1 * color, N):
return False
return True
def isBipartite(self, graph: List[List[int]]) -> bool:
N = len(graph)
grid = [[0] * N for _ in range(N)]
colors = [0] * N
for i in range(N):
for j in graph[i]:
grid[i][j] = 1
for i in range(N):
if colors[i] == 0 and not self.dfs(grid, colors, i, 1, N):
return False
return True
复杂度分析
令 v 和 e 为图中的顶点数和边数。
时间复杂度:$O(v+e)$
空间复杂度:$O(v)$, stack depth = $O(v)$, and colors array.length = $O(v)$
class Solution:
def isBipartite(self, graph: List[List[int]]) -> bool:
n = len(graph)
colors = [0] * n
def dfs(i, color):
colors[i] = color
for neibor in graph[i]:
if colors[neibor] == color: return False
if colors[neibor] == 0 and not dfs(neibor,-1*color): return False
return True
for i in range(n):
if colors[i] == 0 and not dfs(i,1): return False
return True
并查集
思路
遍历图,对于每一个顶点 i,将其所有邻居进行合并,合并到同一个联通域中。这样当发现某个顶点 i 和其邻居已经在同一个联通分量的时候可以直接返回 false,否则返回 true。
代码
代码支持:Python3,Java
Python3 Code:
class UF:
def __init__(self, n):
self.parent = {}
for i in range(n):
self.parent[i] = i
def union(self, i,j):
self.parent[self.find(i)] = self.find(j)
def find(self, i):
if i == self.parent[i]: return i
self.parent[i] = self.find(self.parent[i])
return self.parent[i]
def is_connected(self, i,j):
return self.find(i) == self.find(j)
class Solution:
def isBipartite(self, graph: List[List[int]]) -> bool:
n = len(graph)
uf = UF(n)
for i in range(n):
for neibor in graph[i]:
if uf.is_connected(i, neibor): return False
uf.union(graph[i][0], neibor)
return True
Java Code:
// weighted quick-union with path compression
class Solution {
class UF {
int numOfUnions; // number of unions
int[] parent;
int[] size;
UF(int numOfElements) {
numOfUnions = numOfElements;
parent = new int[numOfElements];
size = new int[numOfElements];
for (int i = 0; i < numOfElements; i++) {
parent[i] = i;
size[i] = 1;
}
}
// find the head/representative of x
int find(int x) {
while (x != parent[x]) {
parent[x] = parent[parent[x]];
x = parent[x];
}
return x;
}
void union(int p, int q) {
int headOfP = find(p);
int headOfQ = find(q);
if (headOfP == headOfQ) {
return;
}
// connect the small tree to the larger tree
if (size[headOfP] < size[headOfQ]) {
parent[headOfP] = headOfQ; // set headOfP's parent to be headOfQ
size[headOfQ] += size[headOfP];
} else {
parent[headOfQ] = headOfP;
size[headOfP] += size[headOfQ];
}
numOfUnions -= 1;
}
boolean connected(int p, int q) {
return find(p) == find(q);
}
}
public boolean isBipartite(int[][] graph) {
int n = graph.length;
UF unionfind = new UF(n);
// i is what node each adjacent list is for
for (int i = 0; i < n; i++) {
// i's neighbors
for (int neighbor : graph[i]) {
// i should not be in the union of its neighbors
if (unionfind.connected(i, neighbor)) {
return false;
}
// add into unions
unionfind.union(graph[i][0], neighbor);
}
}
return true;
}
复杂度分析
令 v 和 e 为图中的顶点数和边数。
时间复杂度:$O(v+e)$, using weighted quick-union with path compression, where union, find and connected are $O(1)$, constructing unions takes $O(v)$
空间复杂度:$O(v)$ for auxiliary union-find space int[] parent, int[] space