-
-
Notifications
You must be signed in to change notification settings - Fork 421
/
Is-Graph-Bipartite.cpp
39 lines (35 loc) · 997 Bytes
/
Is-Graph-Bipartite.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
35
36
37
38
39
//CPP BFS solution
//Time complexity O(V+E)
//Space Complexity O(V)
class Solution {
public:
bool isBipartite(vector<vector<int>>& graph) {
int n=graph.size();
vector<bool>v(n,false);
vector<int>c(n,-1);
queue<int>q;
for(int i=0;i<n;i++){
if(v[i]||graph[i].size()==0){
continue;
}
q.push(i);
v[i]=true;
c[i]=1;
while(!q.empty()){
int j=q.front();
q.pop();
for(int k=0;k<graph[j].size();k++){
if(c[graph[j][k]]==-1){
c[graph[j][k]]=1-c[j];
q.push(graph[j][k]);
v[graph[j][k]]=true;
}
else if(c[j]==c[graph[j][k]]){
return false;
}
}
}
}
return true;
}
};