-
Notifications
You must be signed in to change notification settings - Fork 0
/
IsGraphBipartite.java
39 lines (35 loc) · 1.29 KB
/
IsGraphBipartite.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
33
34
35
36
37
38
39
package graph;
import java.util.*;
//785. Is Graph Bipartite?
public class IsGraphBipartite {
public boolean isBipartite(int[][] graph) {
// 0 = no color = 1 = red 2 =blue
//The nodes array will keep the value of all the unvisited , visited colors
int []visited = new int[graph.length];
for (int i=0;i<graph.length; i++) {
int []adjacent = graph[i];
//checking if already visited or not and if it even has adjacent nodes
if(adjacent.length>0 && visited[i]==0){
//OK Lets paint it red
visited[i]=1;
Queue<Integer> q= new LinkedList<Integer>();
q.offer(i);
while(!q.isEmpty()) {
int curr= q.poll();
for(int c: graph[curr]) {
if(visited[c]==0) {
// Lets paint it with alternate colors
visited[c] = visited[curr] ==1?2:1;
q.offer(c);
}else{
if(visited[c]==visited[curr]){
return false;
}
}
}
}
}
}
return true;
}
}