-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoColor.java
More file actions
33 lines (28 loc) · 758 Bytes
/
TwoColor.java
File metadata and controls
33 lines (28 loc) · 758 Bytes
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
package Graph;
public class TwoColor {
private boolean[] visited;
private boolean[] color;
private boolean isTwoColorable = true;
public TwoColor(Graph g){
visited = new boolean[g.V()];
color = new boolean[g.V()];
for (int i = 0; i < g.V(); i++) {
if (!visited[i]) {
dfs(g, i);
}
}
}
public void dfs(Graph g, int v) {
visited[v] = true;
for (int w : g.adj(v)) {
if (!visited[w]) {
color[w] = !color[v];
dfs(g, w);
}
else if (color[w] == color[v]) isTwoColorable = false;
}
}
public boolean isBipartite() {
return isTwoColorable;
}
}