-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBipartiteGraph.java
More file actions
67 lines (65 loc) · 1.6 KB
/
BipartiteGraph.java
File metadata and controls
67 lines (65 loc) · 1.6 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
56
57
58
59
60
61
62
63
64
65
66
67
import java.util.*;
public class BipartiteGraph {
static boolean bfsCall(int i,int[] col,ArrayList<ArrayList<Integer>> adj)
{
Queue<Integer> q = new ArrayDeque<>();
q.add(i);
col[i]=1;
while(!q.isEmpty())
{
int pp = q.poll();
for(int j:adj.get(pp))
{
if(col[j]==-1)
{
col[j]=1-col[i];
}
else if(col[j]==col[i])
{
return false;
}
}
}
return true;
}
static boolean bipartite(ArrayList<ArrayList<Integer>> adj, int n)
{
int[] col = new int[8];
for(int i=0;i<8;i++)
{
col[i]=-1;
}
for(int i=0;i<n;i++)
{
if(col[i]==-1)
{
if(!bfsCall(i,col,adj)){
return false;
}
}
}
return true;
}
public static void main(String arg[])
{
ArrayList<ArrayList<Integer>> adj = new ArrayList<ArrayList<Integer>>();
for(int i=0;i<8;i++)
{
adj.add(new ArrayList<Integer>());
}
adj.get(0).add(1);
adj.get(1).add(2);
adj.get(2).add(3);
adj.get(3).add(4);
adj.get(4).add(5);
adj.get(5).add(6);
adj.get(6).add(7);
adj.get(7).add(2);
if(bipartite(adj,8) == true)
{
System.out.println("it's a bipartite graph");
}
else
System.out.println("not a bipartite");
}
}