-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBipartite Graph.cpp
34 lines (32 loc) · 1011 Bytes
/
Bipartite Graph.cpp
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
bool DFSisBipartite(int node, vector<int> adj[], vector<bool> &visited, vector<int> &color)
{
visited[node] = true;
for (auto nbr : adj[node])
{
if (!visited[nbr])
{
color[nbr] = !color[node]; //child ka color parent se alag hoga
if (!DFSisBipartite(nbr, adj, visited, color))
return false;
}
else
{
if (node != nbr && color[node] == color[nbr]) //agar baap aur bete ka color same he matlab Graph is not Bipartite
return false;
}
}
return true;
}
bool isBipartite(int V, vector<int>adj[]){
vector<bool> visited(V , 0) ;
vector<int> color(V , 0) ; //ye node ka color store karega
for(int i=0 ; i<V ; i++)
{
if( !visited[i] )
{
if( !DFSisBipartite( i , adj , visited , color) )
return false ;
}
}
return true ;
}