-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathisBipartite.java
More file actions
44 lines (43 loc) · 1.6 KB
/
isBipartite.java
File metadata and controls
44 lines (43 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
public class isBipartite {
public static void main(String[] args){
int[][] graph = {{1,2,3},{0,2},{0,1,3},{0,2}};
System.out.println(isBip(graph));
}
/*
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.
非常经典的一道题:
1。 从一个没有染色的node开始,BFS把所有与之构成graph的node染色
2。 继续从另一个没有染色的node开始,直到所有的node都染色了
* */
public static boolean isBip(int[][] graph){
// 0: no color 1:blue color -1: red color
int[] colors = new int[graph.length];
for(int i = 0; i < colors.length; i++){
// find a node which is not colored yet
if(colors[i] == 0){
// bfs, fill colors to its connected nodes
Queue<Integer> queue = new LinkedList<>();
queue.add(i);
colors[i] = 1;
while(!queue.isEmpty()){
int cur = queue.remove();
for(int child:graph[cur]){
if(colors[child] == 0){
colors[child] = -colors[cur];
queue.add(child);
}else if(colors[child] == colors[cur]){
return false;
}else{
continue;
}
}
}
}
}
return true;
}
}