-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBipartite.java
More file actions
45 lines (44 loc) · 1.27 KB
/
Bipartite.java
File metadata and controls
45 lines (44 loc) · 1.27 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
45
public class Bipartite {
public boolean isBipartite(int[][] graph){
int n = graph.length;
int[] colors = new int[n];
for(int i=0;i<n;i++){
if(colors[i] ==0){
if(!dfsCheck(graph, colors, i, 1)){
return false;
}
}
}
return true;
}
private static boolean dfsCheck(int[][] graph, int[] colors, int node, int color){
colors[node] = color;
for(int neighbor : graph[node]){
if(colors[neighbor] == 0){
if(!dfsCheck(graph, colors, neighbor, -color)){
return false;
}
} else if(colors[neighbor] == color){
return false;
}
}
return true;
}
public static void main(String[] args){
Bipartite bp = new Bipartite();
int[][] graph1 = {
{1,3},
{0,2},
{1,3},
{0,2}
};
System.out.println("Graph1 is Bipartite: " + bp.isBipartite(graph1)); // true
int[][] graph2 = {
{1,2,3},
{0,2},
{0,1,3},
{0,2}
};
System.out.println("Graph2 is Bipartite: " + bp.isBipartite(graph2)); // false
}
}