lelouche29

bipartite Samsung

Aug 20th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 KB | None | 0 0
  1. /*The function takes an adjacency matrix representation of the graph (g)
  2. and an integer(v) denoting the no of vertices as its arguments.
  3. You are required to complete below method */
  4.  
  5. bool bfsutil(int a[][MAX], int V,int r){
  6.     string color[V+1];
  7.     for(int i=0; i<V; i++)
  8.             color[i]="white";
  9.  
  10.     queue<int>q;
  11.     q.push(r);
  12.     color[r]="red";
  13.     while(!q.empty()){
  14.         int node=q.front();
  15.         q.pop();
  16.         for(int j=0; j<V; j++){
  17.             if(a[node][j]==1){
  18.                 if(color[j]=="white"){
  19.                     q.push(j);
  20.                     if(color[node]=="red") color[j]="blue";
  21.                     else color[j]="red";
  22.                 }
  23.                 else{
  24.                     if(color[j]==color[node] || node==j) return false;
  25.                 }
  26.             }
  27.         }
  28.     }
  29.     return true;
  30. }
  31.  
  32.  
  33. bool isBipartite(int a[][MAX],int V){
  34.     if(V==1) {
  35.         if(a[0][0]) return false;
  36.     }
  37.     for(int i=0; i<V; i++){
  38.         if(bfsutil(a,V,i)) continue;
  39.         else
  40.             return false;
  41.     }
  42.     return true;
  43. }
Add Comment
Please, Sign In to add comment