forked from anku580/Java-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBipartite.java
More file actions
55 lines (41 loc) · 1.29 KB
/
Bipartite.java
File metadata and controls
55 lines (41 loc) · 1.29 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
46
47
48
49
50
51
52
53
54
55
import java.util.*;
public class Bipartite {
public int[] color;
public boolean[] marked;
public LinkedList<Integer>queue = new LinkedList<Integer>();
Bipartite(Graph g,int s) {
color = new int[g.V()];
marked = new boolean[g.V()];
for ( int i = 0 ; i<g.V(); i++) {
color[i] = -1;
}
color[s] = 0; // 0 is white color and 1 is RED. Intially source vertex is given white color.
}
boolean findingBipartite(Graph g, int s) {
queue.add(s);
while(!queue.isEmpty()) {
int ss = queue.remove();
marked[ss] = true;
for(int w: g.adj(ss)) {
if(color[w] == -1 && !marked[w]) {
color[w] = 1 - color[ss];
queue.add(w);
}
else if ( color[w] == color[ss])
return false;
}
}
return true;
}
public static void main (String args[]) {
Graph g = new Graph(6);
g.addEdge(0, 1);
g.addEdge(0, 2);
//g.addEdge(0, 3);
g.addEdge(3, 2);
g.addEdge(4, 5);
Bipartite b = new Bipartite(g, 0);
boolean res = b.findingBipartite(g, 0);
System.out.println(res + ", Bipartatie");
}
}