-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.java
51 lines (40 loc) · 1.38 KB
/
App.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
40
41
42
43
44
45
46
47
48
49
50
51
package Graph.Question11;
public class App {
private static boolean isBipartiteHelper(Graph graph, int source, boolean[] visited, boolean[] color) {
visited[source] = true;
for (int nbr: graph.getAdjList()[source]) {
if (!visited[nbr]) {
color[nbr] = !color[source];
if (!isBipartiteHelper(graph, nbr, visited, color))
return false;
} else if (color[source] == color[nbr]) {
return false;
}
}
return true;
}
public static boolean isBipartite(Graph graph) {
boolean[] visited = new boolean[graph.getVertices()];
boolean[] color = new boolean[graph.getVertices()];
color[0] = false;
return isBipartiteHelper(graph, 0, visited, color);
}
public static void main(String[] args) {
Graph graph1 = new Graph(7);
graph1.addEdge(0, 1);
graph1.addEdge(1, 2);
graph1.addEdge(2, 3);
graph1.addEdge(3, 4);
graph1.addEdge(4, 5);
graph1.addEdge(5, 0);
System.out.println(isBipartite(graph1));
Graph graph2 = new Graph(7);
graph2.addEdge(2, 1);
graph2.addEdge(0, 3);
graph2.addEdge(1, 0);
graph2.addEdge(4, 2);
graph2.addEdge(5, 1);
graph2.addEdge(2, 0);
System.out.println(isBipartite(graph2));
}
}