-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCheckBipartiteGraph
48 lines (43 loc) · 1.35 KB
/
CheckBipartiteGraph
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
45
46
47
48
import java.util.*;
public class Solution {
public static boolean isGraphBirpatite(ArrayList<ArrayList<Integer>> edges) {
ArrayList<ArrayList<Integer>> adjList =new ArrayList<>();
int size =edges.size();
for(int i=0;i<size;i++){
adjList.add(new ArrayList<>());
}
for(int i=0;i<size;i++){
for(int j=0;j<size;j++){
if(edges.get(i).get(j)==1){
adjList.get(i).add(j);
adjList.get(j).add(i);
}
}
}
int[] color = new int[size];
for(int i=0;i<size;i++){
if(color[i]==0){
if(!bfs(i,color,adjList))
return false;
}
}
return true;
}
public static boolean bfs(int curr, int[] color,ArrayList<ArrayList<Integer>> adjList){
Queue<Integer> queue = new LinkedList<>();
color[curr] = 1;
queue.offer(curr);
while(!queue.isEmpty()){
int top = queue.poll();
for(Integer x : adjList.get(top)){
if(color[x]==0){
color[x] = 3 - color[top];
queue.offer(x);
}
else if(color[x]==color[top])
return false;
}
}
return true;
}
}