Skip to content

Commit

Permalink
Create BipartiteGraph.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
kshitiz11101 authored May 28, 2024
1 parent 9f62905 commit 4134efb
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions Graphs/BipartiteGraph.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class Solution {
private:
bool check(int start,int V, vector<int>adj[],vector<int>&colors){

queue<int>q;
q.push(start);
colors[start]=0;
while(!q.empty()){
int node=q.front();
q.pop();
for(auto it:adj[node]){
//if adj[node] is yet not color
if(colors[it]==-1){
colors[it]=!colors[node];
q.push(it);
}

// is the adjacent node have the same color

else if(colors[it]==colors[node]){
return false;
}
}
}
return true;
}
public:
bool isBipartite(int V, vector<int>adj[]){
// Code here
vector<int>colors(V);
for(int i=0;i<V;i++){
colors[i]=-1;

}
for(int i=0;i<V;i++){
if(colors[i]==-1){
if(check(i,V,adj,colors)==false){
return false;
}
}
}
return true;
}

};

0 comments on commit 4134efb

Please sign in to comment.