Skip to content

Commit bef5cd9

Browse files
authored
studied bipartite_bfs for first time
1 parent d9e47aa commit bef5cd9

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

graph_series/bipartite_bfs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
bool is_bipartite_bfs_util(int temp,vector<int> adjL[],vector<int> &col){
2+
queue<int> q;
3+
q.push(temp);
4+
col[temp]=0;
5+
while(!q.empty()){
6+
int node=q.front();
7+
q.pop();
8+
for(auto it:adjL[node]){
9+
if(col[it]==-1){
10+
q.push(it);
11+
col[it]=(col[node]==0?1:0);
12+
}
13+
else{
14+
if(col[it]==col[node]) return false;
15+
}
16+
}
17+
}
18+
return true;
19+
}
20+
21+
bool is_bipartite_bfs(int v,vector<int> adjL[]){
22+
vector<int> col(v+1,-1);
23+
for(int i=1;i<v+1;i++){
24+
if(col[i]==-1){
25+
if(!is_bipartite_bfs_util(i,adjL,col)) return false;
26+
}
27+
}
28+
return true;
29+
}

0 commit comments

Comments
 (0)