-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLeetcode785.java
32 lines (30 loc) · 970 Bytes
/
Leetcode785.java
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
package BFS;
import java.util.LinkedList;
import java.util.Queue;
class Leetcode785 {
/*
Check if the graph can be divide into two seperate graph
Use color method to check if two node that connect with one edge has same color, if so it is not a bipartie graph.
Use bfs function to do that.
*/
public boolean isBipartite(int[][] graph) {
int[] color = new int[graph.length];
for(int i = 0;i < graph.length;i++){
if(color[i] != 0)continue;
Queue<Integer> q = new LinkedList<>();
q.offer(i);
color[i] = 1;
while(!q.isEmpty()){
int t = q.poll();
for(int a:graph[t]){
if(color[a] == color[t])return false;
if(color[a] == 0){
color[a] = -1*color[t];
q.offer(a);
}
}
}
}
return true;
}
}