Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*The function takes an adjacency matrix representation of the graph (g)
- and an integer(v) denoting the no of vertices as its arguments.
- You are required to complete below method */
- bool bfsutil(int a[][MAX], int V,int r){
- string color[V+1];
- for(int i=0; i<V; i++)
- color[i]="white";
- queue<int>q;
- q.push(r);
- color[r]="red";
- while(!q.empty()){
- int node=q.front();
- q.pop();
- for(int j=0; j<V; j++){
- if(a[node][j]==1){
- if(color[j]=="white"){
- q.push(j);
- if(color[node]=="red") color[j]="blue";
- else color[j]="red";
- }
- else{
- if(color[j]==color[node] || node==j) return false;
- }
- }
- }
- }
- return true;
- }
- bool isBipartite(int a[][MAX],int V){
- if(V==1) {
- if(a[0][0]) return false;
- }
- for(int i=0; i<V; i++){
- if(bfsutil(a,V,i)) continue;
- else
- return false;
- }
- return true;
- }
Add Comment
Please, Sign In to add comment