Graph Colouring

 #include<iostream.h>  
 #include<conio.h>  
 #include<process.h>  
 class graph  
 {  
      int k,m,n,x[20],i,j,g[20][20];  
      public:  
      void get();  
      void mcolor(int);  
      int nextvalue(int);  
 }gc;  
 void main()  
 {  
      int k=1,n,x[20],i,j,g[20][20];  
      clrscr();  
      gc.get();  
      gc.mcolor(k);  
      getch();  
 }  
 void graph::get()  
 {  
      cout<<"\n ENTER THE LIMIT";  
      cin>>n;  
      cout<<"\nENTER THE MAX.COLOR";  
      cin>>m;  
      for(i=1;i<=n;i++)  
      {  
           x[i]=0;  
           for(j=1;j<=n;j++)  
           {  
                cout<<i<<"to"<<j<<"=";  
                cin>>g[i][j];  
        }  
      }  
 }  
 void graph::mcolor(int k)  
 {  
      while(1)  
      {  
           nextvalue(k);  
           if (x[k]==0)  
           {  
                cout<<"\n Nothing can be done";  
                exit(1);  
           }  
           else if(k==n)  
           {  
                cout<<"\n";  
                cout<<"THE value is";  
                for(i=1;i<=n;i++)  
                {  
                     cout<<"\n\t";  
                     cout<<x[i] <<"\n";  
                }  
                getch();  
                exit(1);  
           }  
           else  
                mcolor(k+1);  
      }  
 }  
 int graph:: nextvalue(int k)  
 {  
      do  
      {  
           x[k]=(x[k]+1)%(m+1);  
           if(x[k]==0)  
           {  
                cout<<"\nColourcant be done";  
                exit(1);  
           }  
           for(j=1;j<=n;j++)  
           {  
           if((g[k][j]!=0)&&(x[k]==x[j]))  
                break;  
           }  
           if(j==n+1)  
                return(1);  
      }while(1);  
 }  

Share This!


No comments:

Post a Comment

Code Of The day - Suggest An Output For The Snippet

  #include<stdio.h>   
  int main()   
  {   
   float a=3.15529;   
   printf("%2.1f\n", a);   
   return 0;   
  }   
· A Code Archive - code1archive