-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBipartite.java
More file actions
143 lines (117 loc) · 3.01 KB
/
Copy pathBipartite.java
File metadata and controls
143 lines (117 loc) · 3.01 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*Program To find bipartite Graph
Step to run the program is as follows:-
Enter the number of vertices:
4
Enter the number of edges:
4
Enter the edges: <to> <from>
1 3
0 2
1 3
0 2
Output = True
*/
import java.util.*;
// Data structure to store graph edges
class Edge
{
int source, dest;
public Edge(int source, int dest) {
this.source = source;
this.dest = dest;
}
};
// Class to represent a graph object
class Graph
{
// An array of Lists to represent adjacency list
List<List<Integer>> adjList = null;
// Constructor
Graph(List<Edge> edges, int N) {
adjList = new ArrayList<>(N);
for (int i = 0; i < N; i++) {
adjList.add(i, new ArrayList<>());
}
// add edges to the undirected graph
for (int i = 0; i < edges.size(); i++)
{
int src = edges.get(i).source;
int dest = edges.get(i).dest;
// add an edge from source to destination
adjList.get(src).add(dest);
// add an edge from destination to source
adjList.get(dest).add(src);
}
}
}
class Bipartite
{
// Perform BFS on graph starting from vertex v
public static boolean BFS(Graph graph, int v, int N)
{
// stores vertex is discovered or not
boolean[] discovered = new boolean[N];
// stores level of each vertex in BFS
int[] level = new int[N];
// mark source vertex as discovered and
// set its level to 0
discovered[v] = true;
level[v] = 0;
// create a queue to do BFS and enqueue
// source vertex in it
Queue<Integer> q = new ArrayDeque<>();
q.add(v);
// run till queue is not empty
while (!q.isEmpty())
{
// pop front node from queue and print it
v = q.poll();
// do for every edge (v -> u)
for (int u : graph.adjList.get(v))
{
// if vertex u is explored for first time
if (!discovered[u])
{
// mark it discovered
discovered[u] = true;
// set level as level of parent node + 1
level[u] = level[v] + 1;
// push the vertex into the queue
q.add(u);
}
// if the vertex is already been discovered and
// level of vertex u and v are same, then the
// graph contains an odd-cycle & is not biparte
else if (level[v] == level[u])
return false;
}
}
return true;
}
public static void main(String[] args)
{
Scanner sc= new Scanner(System.in);
final int N ;
int v, e, count = 1, to = -1, from = -1;
List<Edge> edges= new ArrayList<Edge>();
System.out.println("Enter the number of vertices: ");
N = sc.nextInt();
System.out.println("Enter the number of edges: ");
e = sc.nextInt();
System.out.println("Enter the edges: <to> <from>");
while(count <= e)
{
to = sc.nextInt();
from = sc.nextInt();
edges =Arrays.asList(new Edge(to, from));
count++;
}
// create a graph from edges
Graph graph = new Graph(edges, N);
// Do BFS traversal starting from vertex 1
if (BFS(graph, 1, N))
System.out.println("Output = True ");
else
System.out.println("Not a Bipartite Graph");
}
}