Stop A Minute.

But,Why Code1 Archive?

If you still haven't accessed Code1 Archive , you might wonder why Code1 Archive? C1A is collection of source code for Hundreds Of Problems.

OK , But..How?






Easy Isn't It? Now, Make It Count!

Stop A Minute,

But,Why Code1 Archive?
If you still haven't accessed Code1 Archive , you might wonder why Code1 Archive?.. C1A is collection source code for Hundreds Of Problems.
OK , But..How?











Easy Isn't It? Now, Make It Count!
Read More

Graph Traversal

 /* Program for traversing a graph through BFS and DFS */  
 #include<iostream.h>  
 #include<stdlib.h>  
 #include<conio.h>  
 #define MAX 20  
 int create_graph();  
 int dfs_rec(int);  
 int display();  
 int dfs(int);  
 int bfs(int);  
 int adj_nodes(int);  
 int components();  
 typedef enum boolean{false,true} bool;  
 int adj[MAX][MAX];  
 bool visited[MAX];  
 int n;  /* Denotes number of nodes in the graph */  
 void main()  
 {  
      int i,v,choice;  
      clrscr();  
      create_graph();  
      while(1)  
      {  
           clrscr();  
           cout<<endl;  
           cout<<"1. Adjacency matrix"<<endl;  
           cout<<"2. Depth First Search using stack"<<endl;  
           cout<<"3. Depth First Search through recursion"<<endl;  
           cout<<"4. Breadth First Search"<<endl;  
           cout<<"5. Adjacent vertices"<<endl;  
           cout<<"6. Components"<<endl;  
           cout<<"7. Exit"<<endl;  
           cout<<"\n\nEnter your choice : ";  
           cin>>choice;  
           switch(choice)  
           {  
            case 1:  
                cout<<"\n\n\nAdjacency Matrix\n"<<endl;  
                display();  
                break;  
            case 2:  
                cout<<"\n\n\nEnter starting node for Depth First Search : "<<endl;  
                cin>>v;  
                for(i=1;i<=n;i++)  
                     visited[i]=false;  
                dfs(v);  
                break;  
            case 3:  
                cout<<"\n\n\nEnter starting node for Depth First Search : ";  
                cin>>v;  
                for(i=1;i<=n;i++)  
                     visited[i]=false;  
                dfs_rec(v);  
                break;  
            case 4:  
                cout<<"\n\n\nEnter starting node for Breadth First Search : ";  
                cin>>v;  
                for(i=1;i<=n;i++)  
                     visited[i]=false;  
                bfs(v);  
                break;  
            case 5:  
                cout<<"\n\n\nEnter node to find adjacent vertices : ";  
                cin>>v;  
                cout<<"\n\nAdjacent Vertices are : ";  
                adj_nodes(v);  
                break;  
            case 6:  
                components();  
                break;  
            case 7:  
                exit(1);  
            default:  
                cout<<"\n\n\n\t\t\tWrong choice"<<endl;  
                break;  
            }/*End of switch*/  
            getch();  
      }/*End of while*/  
 }/*End of main()*/  
 create_graph()  
 {  
      int i,max_edges,origin,destin;  
      cout<<"Enter number of nodes : ";  
      cin>>n;  
      max_edges=n*(n-1);  
      for(i=1;i<=max_edges;i++)  
      {  
           cout<<"Enter edge %d( 0 0 to quit ) : "<<i<<endl;  
           cin>>origin>>destin;  
           if((origin==0) && (destin==0))  
                break;  
           if( origin > n || destin > n || origin<=0 || destin<=0)  
           {  
                cout<<"Invalid edge!\n";  
                i--;  
           }  
           else  
           {  
                adj[origin][destin]=1;  
                adj[destin][origin]=1;  
           }  
      }/*End of for*/  
 }/*End of create_graph()*/  
 display()  
 {  
      int i,j;  
      for(i=1;i<=n;i++)  
      {  
           for(j=1;j<=n;j++)  
                cout<<adj[i][j]<<'\t';  
           cout<<endl;  
      }  
 }/*End of display()*/  
 dfs_rec(int v)  
 {  
      int i;  
      visited[v]=true;  
      cout<<v<<'\t';  
      for(i=1;i<=n;i++)  
           if(adj[v][i]==1 && visited[i]==false)  
                dfs_rec(i);  
 }/*End of dfs_rec()*/  
 dfs(int v)  
 {  
      int i,stack[MAX],top=-1,pop_v,j,t;  
      int ch;  
      top++;  
      stack[top]=v;  
      while (top>=0)  
      {  
           pop_v=stack[top];  
           top--;           /*pop from stack*/  
           if( visited[pop_v]==false)  
           {  
                cout<<pop_v<<'\t';  
                visited[pop_v]=true;  
           }  
           else  
                continue;  
           for(i=n;i>=1;i--)  
           {  
                if( adj[pop_v][i]==1 && visited[i]==false)  
                {  
                     top++;  /* push all unvisited neighbours of pop_v */  
                     stack[top]=i;  
                }/*End of if*/  
           }/*End of for*/  
      }/*End of while*/  
 }/*End of dfs()*/  
 bfs(int v)  
 {  
      int i,front,rear;  
      int que[20];  
      front=rear= -1;  
      cout<<v<<'\t';  
      visited[v]=true;  
      rear++;  
      front++;  
      que[rear]=v;  
      while(front<=rear)  
      {  
           v=que[front];      /* delete from queue */  
           front++;  
           for(i=1;i<=n;i++)  
           {  
                /* Check for adjacent unvisited nodes */  
                if( adj[v][i]==1 && visited[i]==false)  
                {  
                     cout<<i<<'\t';  
                     visited[i]=true;  
                     rear++;  
                     que[rear]=i;  
                 }  
           }  
      }/*End of while*/  
 }/*End of bfs()*/  
 adj_nodes(int v)  
 {  
      int i;  
      for(i=1;i<=n;i++)  
      if(adj[v][i]==1)  
           cout<<i<<endl;  
 }/*End of adj_nodes()*/  
 components()  
 {  
      int i;  
      for(i=1;i<=n;i++)  
           visited[i]=false;  
      for(i=1;i<=n;i++)  
      {  
           if(visited[i]==false)  
                dfs_rec(i);  
      }  
      cout<<endl;  
 }/*End of components()*/  
Read More

Shell Sort

 #include<iostream.h>  
 #include<conio.h>  
 class shell  
 {  
      private:  
           int a[10], n, i, k, temp, dis, swap;  
      public:  
           void get();  
           void sort();  
           void disp();  
 };  
 void shell :: get()  
 {  
      cout<<endl<<"Enter the array size : ";  
      cin>>n;  
      cout<<endl<<"Enter the array elements"<<endl<<endl;  
      for(i=0; i<n; i++)  
           cin>>a[i];  
 }  
 void shell :: sort()  
 {  
      dis=n/2;  
      do  
      {  
           static int s=0;  
           s++;  
           do  
           {  
                swap=0;  
                for(i=0; i<n-dis; i++)  
                {  
                     if(a[i]>a[i+dis])  
                     {  
                          temp=a[i];  
                          a[i]=a[i+dis];  
                          a[i+dis]=temp;  
                          swap=1;  
                     }  
                }  
           }while(swap);  
           cout<<endl<<"PASS "<< s <<" : ";  
           for(int k=0; k<n; k++)  
                cout<<a[k]<<'\t';  
           cout<<endl;  
      }while(dis=dis/2);  
 }  
 void shell :: disp()  
 {  
      cout<<endl<<endl<<endl<<"SORTED ARRAY"<<endl<<endl;  
      for(i=0; i<n; i++)  
           cout<<a[i]<<'\t';  
 }  
 void main()  
 {  
      shell s;  
      clrscr();  
      s.get();  
      s.sort();  
      s.disp();  
      getch();  
 }  
Read More

Selection Sort

 #include<iostream.h>  
 #include<conio.h>  
 class selection  
 {  
      private:  
           int a[10], n, i, j, temp, min, loc;  
      public:  
           void get();  
           void sort();  
           void disp();  
 };  
 void selection :: get()  
 {  
      cout<<endl<<"Enter the array size : ";  
      cin>>n;  
      cout<<endl<<"Enter the array elements"<<endl<<endl;  
      for(i=0; i<n; i++)  
           cin>>a[i];  
 }  
 void selection :: sort()  
 {  
      for(i=0; i<n; i++)  
      {  
           min=a[i];  
           loc=i;  
           for(j=i+1; j<n; j++)  
           {  
                if(a[j]<min)  
                {  
                     min=a[j];  
                     loc=j;  
                }  
           }  
           temp=a[i];  
           a[i]=a[loc];  
           a[loc]=temp;  
           cout<<endl<<"PASS "<< i+1 <<" : ";  
           for(int k=0; k<n; k++)  
                cout<<a[k]<<'\t';  
           cout<<endl;  
      }  
 }  
 void selection :: disp()  
 {  
      cout<<endl<<endl<<endl<<"SORTED ARRAY"<<endl<<endl;  
      for(i=0; i<n; i++)  
           cout<<a[i]<<'\t';  
 }  
 void main()  
 {  
      selection sel;  
      clrscr();  
      sel.get();  
      sel.sort();  
      sel.disp();  
      getch();  
 }  
Read More

Radix Sort

 #include<iostream.h>  
 #include<conio.h>  
 class radix  
 {  
      private:  
           int i, j, k, max, re, pass, dc, div, bucket[10][10];  
           int bc[10], a[15], n;  
      public:  
           void get();  
           void sort();  
           void maxi();  
           void disp();  
 };  
 void radix :: get()  
 {  
      cout<<endl<<"Enter the array size : ";  
      cin>>n;  
      cout<<endl<<"Enter the elements : ";  
      for(i=0; i<n; i++)  
           cin>>a[i];  
 }  
 void radix :: maxi()  
 {  
      max=a[0];  
      for(i=1; i<n; i++)  
      {  
           if(max < a[i])  
                max=a[i];  
      }  
      dc=0;  
      div=1;  
      while(max>0)  
      {  
           dc++;  
           max = max/10;  
      }  
 }  
 void radix :: sort()  
 {  
      for(pass=1; pass<=dc; pass++)  
      {  
           for(i=0; i<10; i++)  
                bc[i]=0;  
           for(i=0; i<n; i++)  
           {  
                re=(a[i]/div)%10;  
                bucket[re][bc[re]]=a[i];  
                bc[re]++;  
           }  
           for(k=0; k<10; k++)  
           {  
                for(j=0; j<bc[k]; j++)  
                     a[i++]=bucket[k][j];  
           }  
           div *=10;  
           cout<<endl<<"PASS "<< pass <<" : ";  
           for(k=0; k<n; k++)  
                cout<<'\t'<<a[k];  
      }  
 }  
 void radix :: disp()  
 {  
      cout<<endl<<endl<<"SORTED ARRAY"<<endl<<endl;  
      for(i=0; i<n; i++)  
           cout<<'\t'<<a[i];  
 }  
 void main()  
 {  
      clrscr();  
      radix r;  
      r.get();  
      r.maxi();  
      r.sort();  
      r.disp();  
      getch();  
 }  
Read More

Insertion Sort

 #include<iostream.h>  
 #include<conio.h>  
 class insertion  
 {  
      private:  
           int a[20], n, i, j, temp, k;  
      public:  
           void get();  
           void sort();  
           void display();  
 };  
 void insertion :: get()  
 {  
      cout<<endl<<"Enter the array size : ";  
      cin>>n;  
      cout<<endl<<"Enter the array elements"<<endl<<endl;  
      for(i=0; i<n; i++)  
           cin>>a[i];  
 }  
 void insertion :: sort()  
 {  
      for(j=1; j<n; j++)  
      {  
           temp=a[j];  
           i=j;  
           while((i>=1)&&(temp<a[i-1]))  
           {  
                a[i]=a[i-1];  
                i--;  
           }  
           a[i]=temp;  
           if(j<n)  
           {  
                cout<<endl<<"PASS "<< j <<" : ";  
                for(int k=0; k<n; k++)  
                     cout<<a[k]<<'\t';  
           }  
           cout<<endl;  
      }  
 }  
 void insertion :: display()  
 {  
      cout<<endl<<endl<<endl<<"SORTED ARRAY"<<endl<<endl;  
      for(i=0; i<n; i++)  
           cout<<a[i]<<'\t';  
 }  
 void main()  
 {  
      insertion i;  
      clrscr();  
      i.get();  
      i.sort();  
      i.display();  
      getch();  
 }  
Read More

Heap Sort

 #include<iostream.h>  
 #include<conio.h>  
 class heap  
 {  
      public:  
           void heapsort(int a[],int n);  
           void heapify(int a[],int n);  
           void adjust(int a[],int n,int i);  
 };  
 void heap :: heapsort(int a[],int n)  
 {  
      int t;  
      heapify(a,n);  
      for(int i=n;i>=2;i--)  
      {  
            t=a[i];  
            a[i]=a[1];  
            a[1]=t;  
            adjust(a,1,i-1);  
      }  
 }  
 void heap :: heapify(int a[],int n)  
 {  
      for(int i=n/2;i>=1;i--)  
           adjust(a,i,n);  
 }  
 void heap :: adjust(int a[],int i,int n)  
 {  
      int item, j;  
      j=2*i;  
      item=a[i];  
      while(j<=n)  
      {  
           if((j<n)&&(a[j]<a[j+1]))  
                  j=j+1;  
           if(item>=a[j])  
                break;  
           a[j/2]=a[j];  
           j=2*j;  
      }  
      a[j/2]=item;  
 }  
 void main()  
 {  
      int a[10],n,j,i;  
      heap h;  
      clrscr();  
      cout<<endl<<"Enter the array size: ";  
      cin>>n;  
      cout<<endl<<"Enter the elements : ";  
      for(i=1;i<=n;i++)  
           cin>>a[i];  
      h.heapsort(a,n);  
      cout<<endl<<endl<<"SORTED ARRAY"<<endl;  
      for(i=1;i<=n;i++)  
           cout<<'\t'<<a[i];  
      getch();  
 }  
Read More

Bubble Sort

 #include<iostream.h>  
 #include<conio.h>  
 class bubble  
 {  
      private:  
           int a[20], n, i, j, temp;  
      public:  
           void get();  
           void sort();  
           void display();  
 };  
 void bubble :: get()  
 {  
      cout<<endl<<"Enter the array size : ";  
      cin>>n;  
      cout<<"Enter the array elements"<<endl<<endl;  
      for(i=0; i<n; i++)  
           cin>>a[i];  
 }  
 void bubble :: sort()  
 {  
      for(i=0; i<n-1; i++)  
      {  
           for(j=0; j<n-1-i; j++)  
           {  
                if(a[j]>a[j+1])  
                {  
                     temp=a[j];  
                     a[j]=a[j+1];  
                     a[j+1]=temp;  
                }  
           }  
           if(i<n)  
           {  
                cout<<endl<<"PASS "<< i+1 <<" : ";  
                for(int k=0; k<n; k++)  
                     cout<<a[k]<<'\t';  
           }  
           cout<<endl;  
      }  
 }  
 void bubble :: display()  
 {  
      cout<<endl<<endl<<endl<<"SORTED ARRAY"<<endl<<endl;  
      for(i=0; i<n; i++)  
           cout<<a[i]<<'\t';  
 }  
 void main()  
 {  
      bubble b;  
      clrscr();  
      b.get();  
      b.sort();  
      b.display();  
      getch();  
 }  
Read More

Linear Search-C++

 #include<iostream.h>  
 #include<conio.h>  
 class linear( )  
 {  
      private:  
           int i, m, a[10], n, t;  
      public:   
           void get( );  
           void search( );  
 };  
 void linear :: get( )  
 {  
      cout<<”\nEnter the limit : “;  
      cin>>n;  
      cout<<”\nEnter the elements : “;  
      for(i=0; i<n ; i++)  
           cin>>a[i];  
 }  
 void linear :: serach( )  
 {  
      cout<<”\nEnter the element to be serached : “;  
      cin>>m;  
      for(i=0 ; i<n; i++)  
      {  
           if(a[i]==m)  
           {  
                t=1;  
                break;  
           }  
      }  
      if(t==1)  
            cout<<”\nElement is found”;  
      else  
           cout<<”\nElement is not found”;  
 }  
 void main( )  
 {  
      clrscr( );  
      linear l;  
      l.get( );  
      l.search( );  
      getch( );  
 }  
Read More

Fibonacci Search-C++

 #include<iostream.h>  
 #include<conio.h>  
 class fibonacci  
 {  
      private:  
           int f, a, b, c, x[20], d, i, low, high, mid, n;  
      public:  
           fibonacci( );  
           void get( );  
           void fibo( );  
           void fibosearch( );  
 };  
 fibonacci :: fibonacci( )  
 {  
      a=-1;  
      b=1;  
      low=0;  
 }  
 void fibonacci :: fibo( )  
 {  
      cout<<”\nEnter the limit for Fibonacci Series : “;  
      cin>>n;  
      cout<<”\nFIBONACCI SERIES is “  
      for(i=0; i<n; i++)  
      {  
           c=a+b;  
           cout<<’\t’<<c;  
           x[i]=c;  
           a=b;  
           b=c;  
      }  
 }  
 void fibonacci :: fibosearch( )  
 {  
      high=n;  
      while(low<=high)  
      {  
           mid=(low+high)/2;  
           if(d<x[mid])  
                high=mid-1;  
           else if(d>x[mid])  
                low=mid+1;  
           else  
           {  
                f=1;  
                break;  
           }  
      }  
      if(f==1)  
           cout<<”\nElement Found in “<<mid<<” position”;  
      else  
           cout<<”\nElement Not Found”;  
 }  
 void fibonacci :: get( )  
 {  
      cout<<”\nEnter the element to be searched : “;  
      cin>>d;  
 }  
 void main( )  
 {  
      clrscr( );  
      fibonacci fib;  
      fib.fibo( );  
      fib.get( );  
      fib.fibosearch( );  
      getch( );  
 }  
Read More

Binary Search- C++

 #include<iostream.h>  
 #include<conio.h>  
 class binary  
 {  
      private:  
           int a[20], n, x, i, high, low, mid, flag;  
      public:  
           void get( )  
           {  
                cout<<”\nEnter the limit : “;  
                cin>>n;  
                cout<<”\nEnter the elements : “;  
                for(i=0; i<n; i++)  
                     cin>>a[i];  
                cout<<”\nEnter the search element : “;  
                cin>>x;  
           }  
           void search( )  
           {  
                low=0;  
                high=n-1;  
                mid=(low+high)/2;  
                for(i=0; i<n; i++)  
                {  
                     if(x<a[mid])  
                     {  
                          high=mid-1;  
                          mid=(low+high)/2;  
                     }  
                     else if(x>a[mid]  
                     {  
                          low=mid+1;  
                          mid=(low+high)/2;  
                     }  
                     else if(x==a[mid])  
                     {  
                          cout<<”\nThe number is found”;  
                          break;  
                     }  
                     else  
                          cout<<”\nThe number is not found”;  
                }  
           }  
 };   
 void main( )  
 {  
      binary b;  
      clrscr( );  
      b.get( );  
      b.search( );  
      getch( );  
 }  
Read More

Single Source Shortest Path

 #include<iostream.h>  
 #include<conio.h>  
 class path  
 {  
       private:  
              int i,j,m,n,b[50],cost[20][20],k,d[50];  
              int u,v,w,num,s[20];  
       public:  
              void getdata();  
              void sp();  
              void display();  
 };  
 void path::getdata()  
 {  
       cout<<"\n****SINGLE SOURCE SHORTEST PATH****\n";  
       cout<<"\nEnter the number of nodes : ";  
       cin>>n;  
       cout<<"\nEnter the cost\n";  
       for(i=1;i<=n;i++)  
       {  
             for(j=1;j<=n;j++)  
             {  
                   if(i==j)  
                      cost[i][j]=0;  
                   else  
                   {  
                         cout<<"\ncost["<<i<<","<<j<<"]:";  
                         cin>>cost[i][j];  
                   }  
            }  
            cout<<"\n";  
       }  
 }  
 void path::sp()  
 {  
       k=1;  
       clrscr();  
       cout<<"\nThe cost of vertices are\n";  
       cout<<"\t";  
       for(i=1;i<=n;i++)  
              cout<<i<<"\t";  
       cout<<"\n\t";  
       for(i=1;i<=n;i++)  
       {  
             cout<<i<<"\t";  
             for(j=1;j<=n;j++)  
                   cout<<cost[i][j]<<"\t";  
             cout<<"\n";  
       }  
       cout<<"\nEnter the source vertex:";  
       cin>>v;  
       for(i=1;i<=n;i++)  
       {  
             s[i]=0;  
             d[i]=cost[v][i];  
       }  
       s[v]=1;  
       b[k++]=v;  
       d[v]=0;  
       for(num=2;num<=n;num++)  
       {  
             m=9999;  
             for(j=1;j<=n;j++)  
           {  
                  if((m>d[j])&&(s[j]==0))  
                  {  
                         m=d[j];  
                         u=j;  
                  }  
           }  
             s[u]=1;  
             b[k++]=u;  
             for(w=1;w<=n;w++)  
             {  
                   if(d[w]>(d[u]+cost[u][w])&&(s[w]==0))  
                     d[w]=(d[u]+cost[u][w]);  
             }  
             cout<<"\n";  
       }  
 }  
 void path::display()  
 {  
       cout<<"\n";  
       cout<<"\nThe shortest path with source vertex"<<v<<"is\n";  
       cout<<"\n\t\t"<<b[1];  
       for(i=2;i<k;i++)  
             cout<<"----->"<<b[i];  
 }  
 void main()  
 {  
       clrscr();  
       path p;  
       p.getdata();  
       p.sp();  
       p.display();  
       getch();  
 }  
Read More

PRIMS-Greedy

 #include<iostream.h>  
 #include<conio.h>  
 class prims  
 {  
       private:  
             int i,j,k,l,close[50];  
             int cost[50][50],mincost,t[50][50];  
             int min_cost;  
       public:  
             void getdata(int n);  
             void printdata(int);  
             void prim_s(int n);  
 };  
 void prims::getdata(int n)  
 {  
      cout<<"\nEnter the cost of adjacency matrix";  
      cout<<"\n(If there is no edge then enter 9999)";  
      for(i=1;i<=n;i++)  
      {  
           for(int j=i;j<=n;j++)  
           {  
                if(i!=j)  
                {  
                     cout<<"\nCost between "<<i<<" and "<<j<<" : ";  
                     cin>>cost[i][j];  
                     cost[j][i]=cost[i][j];  
                }  
           }  
      }  
      k=1;  
      l=2;  
      for(i=1;i<=n;i++)  
      {  
           for(j=i;j<=n;j++)  
           {  
                if((i!=j)&&(cost[i][j]<cost[k][l]))  
                {  
                     k=i;  
                     l=j;  
                }  
           }  
      }  
 }  
 void prims::prim_s(int n)  
 {  
      mincost=cost[k][l];  
      t[1][1]=k;  
      t[1][2]=l;  
      for(i=1;i<=n;i++)  
      {  
           if(cost[i][l]<cost[k][i])  
                close[i]=l;  
           else  
                close[i]=k;  
      }  
      close[l]=0;  
      close[k]=0;  
      for(i=2;i<=n-1;i++)  
      {  
           min_cost=9999;  
           for(int a=1;a<=n;a++)  
           {  
                if((close[a]!=0)&&(min_cost>cost[a][close[a]]))  
                {  
                     j=a;  
                     min_cost=cost[j][close[j]];  
                }  
           }  
           t[i][1]=close[j];  
           t[i][2]=j;  
           mincost=mincost+min_cost;  
           close[j]=0;  
           for(k=1;k<=n;k++)  
           {  
                if((close[k]!=0)&&(cost[k][close[k]]>cost[k][j]))  
                     close[k]=j;  
           }  
      }  
 }  
 void prims::printdata(int n)  
 {  
      cout<<"\nMinimum Cost = "<<mincost;  
      cout<<"\nThe path is \n";  
      for(i=1;i<n;i++)  
           cout<<’\t’<<t[i][1]<<" to "<<t[i][2]<<"\n";  
 }  
 void main()  
 {  
      int n;  
      prims pr;  
      clrscr();  
      cout<<"\n\t\t\tPRIMS ALGORITHM";  
      cout<<"\nEnter the limit : ";  
      cin>>n;  
      pr.getdata(n);  
      pr.prim_s(n);  
      pr.printdata(n);  
      getch();  
 }  
Read More

Kruskal-Greedy

 #include<iostream.h>  
 #include<conio.h>  
 int parent[100];  
 int find1(int i)  
 {  
      while(parent[i]>=0)  
           i=parent[i];  
      return(i);  
 }  
 void union1(int x1,int x2)  
 {  
      parent[x1]=x2;  
 }  
 void main()  
 {  
      int a[20][20],x[20],y[20],t[10][10],z[20];  
      int mincost,n1,r,te,n,u,v,i,j,k;  
      clrscr();  
      cout<<"\n Enter the number of nodes : ";  
      cin>>n;  
      cout<<"\n\t ENTER THE COST OF ADJACENCY MATRIX ";  
      cout<<"\n\t(If no edges exist enter 9999)\n";  
      for(i=1;i<n;i++)  
      {  
           for(j=i+1;j<=n;j++)  
           {  
                a[i][j]=0;  
                cout<<"\na["<<i<<"]["<<j<<"] = ";  
                cin>>a[i][j];  
           }  
      }  
      k=1;  
      for(i=1;i<=n;i++)  
      {  
           for(j=1;j<=n;j++)  
           {  
                if((i<j)&&(a[i][j]!=0))  
                {  
                     x[k]=i;  
                     y[k]=j;  
                     z[k]=a[i][j];  
                     k++;  
                }  
           }  
      }  
      for(i=1;i<k;i++)  
      {  
           for(j=1;j<k;j++)  
           {  
                if(i<j)  
                {  
                     if(z[i]>z[j])  
                     {  
                          te=z[i];z[i]=z[j];z[j]=te;  
                          te=x[i];x[i]=x[j];x[j]=te;  
                          te=y[i];y[i]=y[j];y[j]=te;  
                     }  
                }  
           }  
      }  
      n1=k-1;  
      for(i=1;i<=n;i++)  
           parent[i]=-1;  
      r=1;  
      mincost=0;  
      i=0;  
      while((i<n-1)&&(r!=n1))  
      {  
           u=x[r];v=y[r];j=find1(u);  
           k=find1(v);  
           if(j!=k)  
           {  
                i=i+1;  
                t[i][1]=u;  
                t[i][2]=v;  
                mincost=mincost+z[r];  
                union1(j,k);  
           }  
           r++;  
      }  
      if(i!=n-1)  
           cout<<"NO SPANNING TREE\n";  
      else  
      {  
           cout<<"SPANNING TREE\n";  
           for(i=1;i<n;i++)  
                cout<<"\t"<<t[i][1]<<"--->"<<t[i][2];  
      }  
      cout<<"\nTOTAL MINCOST = "<<mincost;  
      getch();  
 }  
Read More

Knapsnack-Greedy

 #include<iostream.h>  
 #include<conio.h>  
 float w[100],v[100],x[100],wt,wgt[100];  
 class greed  
 {  
      private:  
           int i,j,n;  
           float r[100],f,total,t,rat,pro,wei,op,opti;  
      public:  
           void getdata();  
           void ratio();  
           void profit();  
           void weight();  
           void optimal();  
 };  
 void greedy(int n)  
 {  
      int wgt,i;  
      for(i=0;i<n;i++)  
           x[i]=0;  
      wgt=i=0;  
      while(wgt<wt)  
      {  
           if(wgt+w[i]<=wt)  
           {  
                x[i]=1;  
                wgt+=w[i];  
           }  
           else  
           {  
                x[i]=(wt-wgt)/w[i];  
                wgt+=(x[i]*w[i]);  
           }  
           i++;  
      }  
 }  
 void greed:: getdata()  
 {  
      cout<<"Enter no of objects : ";  
      cin>>n;  
      cout<<"Enter the capacity of the bag : ";  
      cin>>wt;  
      for(i=0;i<n;i++)  
      {  
           cout<<"Weight = ";  
           cin>>w[i];  
           cout<<"Profit = ";  
           cin>>v[i];  
           r[i]=v[i]/w[i];  
      }  
 }  
 void greed:: ratio()  
 {  
      cout<<endl<<"\t\t\tGREEDY BY RATIO\n";  
      for(i=0;i<n;i++)  
      {  
           for(j=i+1;j<n;j++)  
           {  
                if(r[i]<r[j])  
                {  
                     t=w[i];w[i]=w[j];w[j]=t;  
                     t=v[i];v[i]=v[j];v[j]=t;  
                     t=r[i];r[i]=r[j];r[j]=t;  
                }  
           }  
      }  
      greedy(n);  
      for(i=0;i<n;i++)  
      {  
           cout<<"\nWeight "<<w[i];  
           cout<<"\tProfit "<<v[i];  
           cout<<"\tRatio "<<r[i];  
           cout<<"\tFraction "<<x[i];  
           total+=(x[i]*v[i]);  
      }  
      cout<<"\nFeasible profit = "<<total;  
      rat=total;  
      getch();  
      total=0;  
 }  
 void greed:: profit()  
 {  
      cout<<endl<<"\t\tGREEDY BY PROFIT\n";  
      for(i=0;i<n;i++)  
      {  
           for(j=i+1;j<n;j++)  
           {  
                if(v[i]<v[j])  
                {  
                     t=w[i];w[i]=w[j];w[j]=t;  
                     t=v[i];v[i]=v[j];v[j]=t;  
                     t=r[i];r[i]=r[j];r[j]=t;  
                }  
           }  
      }  
      greedy(n);  
      for(i=0;i<n;i++)  
      {  
           cout<<"\nWeight "<<w[i];  
           cout<<"\tProfit "<<v[i];  
           cout<<"\tRatio "<<r[i];  
           cout<<"\tFraction "<<x[i];  
           total+=(x[i]*v[i]);  
      }  
      cout<<"\nFeasible profit = "<<total;  
      pro=total;  
      getch();  
      total=0;  
 }  
 void greed:: weight()  
 {  
      cout<<"GREEDY BY WEIGHT\n";  
      for(i=0;i<n;i++)  
      {  
           for(j=i+1;j<n;j++)  
           {  
                if(w[i]<w[j])  
                {  
                     t=w[i];w[i]=w[j];w[j]=t;  
                     t=v[i];v[i]=v[j];v[j]=t;  
                     t=r[i];r[i]=r[j];r[j]=t;  
                }  
           }  
      }  
      greedy(n);  
      for(i=0;i<n;i++)  
      {  
           cout<<"\nWeight "<<w[i];  
           cout<<"\tProfit "<<v[i];  
           cout<<"\tRatio "<<r[i];  
           cout<<"\tFraction "<<x[i];  
           total+=(x[i]*v[i]);  
      }  
      cout<<"\nFeasible profit = "<<total;  
      wei=total;  
      getch();  
 }  
 void greed:: optimal()  
 {  
      if(rat>pro)  
           op=rat;  
      else  
           op=pro;  
      if(op>wei)  
           opti=op;  
      else  
           opti=wei;  
      cout<<"\n\n\n  
 OPTIMAL SOLUTION IS "<<opti;  
      getch();  
 }  
 void main()  
 {  
      clrscr();  
      greed s;  
      cout<<"KNAPSACK PROBLEM\n";  
      s.getdata();  
      s.ratio();  
      s.profit();  
      s.weight();  
      s.optimal();  
      getch();  
 }  
Read More

Floyd–Warshall algorithm

 #include<iostream.h>  
 #include<conio.h>  
 int a[20][20],cost[20][20],p[20][20],i,j,n,k;  
 class path  
 {  
  public:  
    void get();  
    void allpair(int a[20][20],int n);  
    void put();  
 };  
 void path:: get()  
 {  
  cout<<"\n\t\tALL PAIR SHORTEST PATH\n";  
  cout<<"\nEnter the no of vertices : ";  
  cin>>n;  
  cout<<"\nEnter the cost\n";  
  for(i=1;i<=n;i++)  
  {  
    for(j=1;j<=n;j++)  
    {  
      if(i==j)  
      {  
        cost[i][j]=p[i][j]=0;  
        a[i][j]=cost[i][j];  
      }  
      else  
      {  
        cout<<"\ncost["<<i<<"]["<<j<<"]=";  
        cin>>cost[i][j];  
        a[i][j]=cost[i][j];  
      }  
    }  
      cout<<"\n";  
  }  
 }  
 void path:: allpair(int a[20][20],int n)  
 {  
  for(k=1;k<=n;k++)  
  {  
    for(i=1;i<=n;i++)  
    {  
      for(j=1;j<=n;j++)  
      {  
        if((a[i][k]+a[k][j])<a[i][j])  
        {  
         a[i][j]=a[i][k]+a[k][j];  
         p[i][j]=k;  
        }  
      }  
    }  
  }  
 }  
 void path:: put()  
 {  
  cout<<"\n\n\n\n\n\nALL PAIR SHORTEST PATH\n\n\n";  
  for(i=1;i<=n;i++)  
  {  
    for(j=1;j<=n;j++)  
      cout<<'\t'<<a[i][j];  
    cout<<"\n";  
  }  
  cout<<"\n\n\nPath Matrix\n";  
  for(i=1; i<=n; i++)  
  {  
    for(j=1; j<=n; j++)  
      cout<<'\t'<<p[i][j];  
    cout<<"\n";  
  }  
 }  
 void main()  
 {  
  path p;  
  clrscr();  
  p.get();  
  p.allpair(a,n);  
  p.put();  
  getch();  
 }  
Read More

Maximum And Minimum

 #include<iostream.h>  
 #include<conio.h>  
 class maxmin  
 {  
           int a[10], max, min, n;  
      public:  
           void maximin(int , int);  
           void get();  
           void disp();  
 };  
 void maxmin :: get()  
 {  
      cout<<endl<<"Enter the number of elements : ";  
      cin>>n;  
      cout<<endl<<"Enter the elements :\t";  
      for(int i=1; i<=n; i++)  
           cin>>a[i];  
      maximin(1, n);  
 }  
 void maxmin :: maximin(int i, int j)  
 {  
      int mid, max1, min1;  
      if(i==j)  
           max=min=a[i];  
      else if(i+1==j)  
      {  
           if(a[i]<a[j])  
           {  
                max=a[j];  
                min=a[i];  
           }  
           else  
           {  
                max=a[i];  
                min=a[j];  
           }  
      }  
      else  
      {  
           mid=(i+j)/2;  
           maximin(i, mid);  
           max1=max;  
           min1=min;  
           maximin(i, mid);  
           maximin(mid+1, j);  
           if(max<max1)  
                max=max1;  
           if(min>min1)  
                min=min1;  
      }  
 }  
 void maxmin :: disp()  
 {  
      cout<<endl<<"Maximum number is  "<<max;  
      cout<<endl<<"Minimun number is  "<<min;  
 }  
 void main()  
 {  
      clrscr();  
      maxmin m;  
      m.get();  
      m.disp();  
      getch();  
 }  
Read More

MergeSort

 #include<iostream.h>  
 #include<conio.h>  
 class merge  
 {  
      private:  
           int n,a[20],b[20];  
      public:  
           void get();  
           void sort(int,int,int);  
           void msort(int,int);  
           void disp();  
 };  
 void merge::get()  
 {  
      cout<<"Enter the limit :";  
      cin>>n;  
      cout<<"\nEnter the elements :";  
      for(int i=0;i<n;i++)  
           cin>>a[i];  
      msort(0,n-1);  
 }  
 void merge::msort(int low,int high)  
 {  
      if(low<high)  
      {  
           int mid=(low+high)/2;  
           msort(low,mid);  
           msort(mid+1,high);  
           sort(low,mid,high);  
      }  
 }  
 void merge::sort(int l,int m,int h)  
 {  
      int low=l;  
      int k;  
      int i=l;  
      int j=m+1;  
      while((low<=m)&&(j<=h))  
      {  
           if(a[low]<=a[j])  
                b[i++]=a[low++];  
           else  
                b[i++]=a[j++];  
      }  
      if(low>m)  
      {  
           for(k=j;k<=h;k++)  
                b[i++]=a[k];  
      }  
      else  
      {  
           for(k=low;k<=m;k++)  
                b[i++]=a[k];  
      }  
      for(k=0;k<=h;k++)  
           a[k]=b[k];  
 }  
 void merge :: disp()  
 {  
      cout<<"\nSORTED ARRAY \n";  
       for(int z=0;z<n;z++)  
           cout<<'\t'<<a[z];  
 }  
 void main()  
 {  
      merge m;  
      clrscr();  
      m.get();  
      m.disp();  
      getch();  
 }  
Read More

QuickSort

 #include<iostream.h>  
 #include<conio.h>  
 int a[15], m, i, j, n, p, v;  
 class quick  
 {  
      protected:  
           int partition(int a[], int, int);  
      public:  
           void get();  
           void sort(int a[], int, int);  
           void disp();  
 };  
 void quick :: get()  
 {  
      cout<<endl<<"Enter the limit : ";  
      cin>>n;  
      cout<<endl<<"Enter the elements :\t";  
      for(i=0; i<n; i++)  
           cin>>a[i];  
 }  
 void quick :: sort(int a[], int p, int q)  
 {  
      if(p<q)  
      {  
           j=partition(a, p, q+1);  
           sort(a, p, j-1);  
           sort(a, j+1, q);  
      }  
 }  
 int quick :: partition(int a[], int m, int p)  
 {  
      v=a[m];  
      i=m;  
      j=p;  
      do  
      {  
           do  
                i++;  
           while(a[i]<v);  
           do  
                j--;  
           while(a[j]>v);  
           if(i<j)  
           {  
                p=a[i];  
                a[i]=a[j];  
                a[j]= p;  
           }  
      }while(i<=j);  
      a[m]=a[j];  
      a[j]=v;  
      return j;  
 }  
 void quick :: disp()  
 {  
      cout<<endl<<endl<<"SORTED ARRAY"<<endl;  
      for(i=0; i<n; i++)  
           cout<<'\t'<<a[i];  
 }  
 void main()  
 {  
      quick q;  
      clrscr();  
      q.get();  
      q.sort(a, 0, n-1);  
      q.disp();  
      getch();  
 }  
Read More

Knapsack Branch And Bound

 #include<iostream.h>  
 #include<conio.h>  
 int c,z[20],t[50][50],s[20],i,j,pt[20],wt[20],x[20],m,n,y=1;  
 class knp  
 {  
      public:  
           void get();  
           void table();  
           void select();  
           void display();  
 }k;  
 void knp:: get()  
 {  
      cout<<"\nKNAPSACK PROBLEM";  
      cout<<"Enter the number of values: ";  
        cin>>n;  
        cout<<"Enter the capacity of the bag: ";  
        cin>>m;  
        cout<<"Enter the values: ";  
        int i;  
        for(i=1;i<=n;i++)  
           cin>>wt[i];  
        cout<<"Enter the profits: ";  
        for(i=1; i<=n; i++)  
           cin>>pt[i];  
 }  
 void knp:: table()  
 {  
      for(i=1;i<=n;i++)  
      {  
           for(j=0;j<=m;j++)  
           {  
                if((i==1)&&(j<wt[i]))  
                     t[i][j]=0;  
                if((i==1)&&(j>=wt[i]))  
                     t[i][j]=pt[i];  
                if((i>1)&&(j<wt[i]))  
                     t[i][j]=t[i-1][j];  
                if((i>1)&&(j>=wt[i]))  
                     t[i][j]=pt[i]+t[i-1][j-wt[i]];  
           }  
      }  
 }  
 void knp:: select()  
 {  
      int q=0;  
      c=0;  
      int a,b,d;  
      i=n;  
      for(j=m;j>=0;j--)  
      {  
           if(t[i][j]==t[i-1][j])  
           {  
                a=i-1;  
                if(t[a][j]!=t[a-1][j-wt[a]]+pt[a])  
                {  
                     z[c]=t[a][j];  
                     c++;  
                     s[q]=a;  
                     q++;  
                }  
                b=a-1;  
                d=j-wt[a];  
                if(t[a-1][d]!=t[b-1][d-wt[b]])  
                {  
                     z[c]=t[a-1][j-wt[a]];  
                     c++;  
                     s[q]=a-1;  
                     q++;  
                }  
           }  
           else  
           {  
                a=i-1;  
                if(t[a][j]!=t[a-1][j-wt[a]]+pt[a])  
                {  
                     z[c]=t[a][j];  
                     s[q]=a;  
                     q++;  
                }  
                b=a-1;  
                d=j-wt[a];  
                if(t[a-1][d]!=t[b-1][d-wt[b]])  
                {  
                     z[c]=t[a-1][j-wt[a]];  
                     c++;  
                     s[q]=a-1;  
                     q++;  
                }  
           }  
           j=d+1;  
      }  
 }  
 void knp:: display()  
 {  
      int profit=0;  
      for(int x=0; x<c;x++)  
        profit += pt[s[x]];  
      cout<<"\nThe Solution is\n";  
      for(x=0; x<c; x++)  
        cout<<s[x]<<'\t';  
      cout<<"\n\nMaximum Profit : "<<profit;  
 }  
 void main()  
 {  
      clrscr();  
      k.get();  
      k.table();  
      k.select();  
      k.display();  
      getch();  
 }  
Read More

Knapsack Problem


Read More

Tree Traversal

1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  #include<alloc.h>  
4:  struct bnode  
5:  {  
6:   int data;  
7:   struct bnode *left;  
8:   struct bnode *right;  
9:  }*start;  
10:  typedef struct bnode b;  
11:  struct bnode *binsearch(struct bnode *,int);  
12:  struct bnode *findmin(struct bnode *);  
13:  struct bnode *delete(struct bnode *,int);  
14:  void main()    
15:  {  
16:   int n,chk,choice,chk1;  
17:   start = NULL;  
18:   clrscr();  
19:   do  
20:   {  
21:    printf("\n\n\t\t\tMENU");  
22:    printf("\n\t\t\t*******");  
23:    printf("\n\n\t\t\t1. Adding new node.");  
24:    printf("\n\n\t\t\t2. Traversing tree");  
25:    printf("\n\n\t\t\t3. Searching");  
26:    printf("\n\n\t\t\t4. Deletion");  
27:    printf("\n\n\t\t\t5. Exit");  
28:    printf("\n\n\t\t\t Enter ur choice");  
29:    scanf("%d",&n);  
30:    if(n == 1)  
31:    {  
32:     add();  
33:    }  
34:    if(n == 3)  
35:    {  
36:     struct bnode *src;  
37:     printf("\n\t\t\tEnter the value to be searched: ");  
38:     scanf("%d",&chk);  
39:     src = binsearch(start,chk);  
40:     if(src != NULL)  
41:       printf("\n\t\t\tThe searched node is %d",src->data);  
42:     else  
43:       printf("\n The node does not exist");  
44:    }  
45:    if(n == 2)  
46:    {  
47:     printf("\n\t\t\tBinary search traversal");  
48:     printf("\n\t\t\t***********************");  
49:     printf("\n\n\t\t\t1. Inorder");  
50:     printf("\n\n\t\t\t2. Preorder");  
51:     printf("\n\n\t\t\t3. Postorder");  
52:     printf("\n\n\t\t\tEnter choice for traversal");  
53:     scanf("%d",&choice);  
54:     if(choice == 1)  
55:       travinorder(start);  
56:     if(choice == 2)  
57:       travpreorder(start);  
58:     if(choice == 3)  
59:       travpostorder(start);  
60:    }  
61:    if(n == 4)  
62:    {  
63:     printf("\n\t\t\tEnter the value to be deleted");  
64:     scanf("%d",&chk1);  
65:     delete(start,chk1);  
66:    }  
67:   }while(n < 5);  
68:  getch();  
69:  }   
70:  /*Function to create a new node*/  
71:  struct bnode *getdata()  
72:  {  
73:   struct bnode *new1,*chek;  
74:   int t;  
75:   new1 = (b *)malloc(sizeof(b));  
76:   printf("\n\t\t\tEnter the number");  
77:   scanf("%d",&new1->data);  
78:   chek = binsearch(start,new1->data);  
79:   if(chek->data == new1->data)  
80:   {  
81:    printf("\n Already exist");  
82:    return NULL;  
83:   }  
84:   new1->left = NULL;  
85:   new1->right = NULL;  
86:   return new1;  
87:  }  
88:  /*function to add new nodes*/  
89:  add()  
90:  {  
91:   struct bnode *ptr,*prev,*x,*root;  
92:   x = getdata();  
93:   if(start == NULL)  
94:   {  
95:    start = x;  
96:    x->left = NULL;  
97:    x->right = NULL;  
98:   }  
99:   else  
100:   {  
101:    buildtree(start,x);  
102:   }  
103:   return;  
104:  }  
105:  buildtree(struct bnode *node,struct bnode *x)  
106:  {  
107:   if(x->data >node->data)  
108:   {  
109:    if(node->right == NULL)  
110:    {  
111:     printf("\n\t The added node is %d's right",node->data);  
112:     node->right = x;  
113:     x->left = NULL;  
114:     x->right = NULL;  
115:    }  
116:    else  
117:     buildtree(node->right,x);  
118:   }  
119:   else  
120:   {  
121:    if(x->data < node->data)  
122:    {  
123:     if(node->left == NULL)  
124:     {  
125:       printf("\n\t The added node is %d's left",node->data);  
126:       node->left = x;  
127:       x->left = NULL;  
128:       x->right = NULL;  
129:     }  
130:     else  
131:       buildtree(node->left,x);  
132:    }  
133:   }  
134:   return;  
135:  }  
136:  /*Function to search in the binary search tree*/  
137:  struct bnode *binsearch(struct bnode *node,int chk)  
138:  {  
139:   if(node != NULL)  
140:   {  
141:    if(node->data == chk)  
142:    {  
143:     printf("\n\t Node found in the tree");  
144:     return node;  
145:    }  
146:    else  
147:     if(chk < node->data)  
148:      binsearch(node->left,chk);  
149:    else  
150:      binsearch(node->right,chk);  
151:   }  
152:   else  
153:   {  
154:    printf("\n\t Node not found in the tree");  
155:    return NULL;  
156:   }  
157:  }  
158:  travinorder(struct bnode *node)  
159:  {  
160:   if(node!= NULL)  
161:   {  
162:    travinorder(node->left);  
163:    printf("\n%d",node->data);  
164:    travinorder(node->right);  
165:   }  
166:   return;  
167:  }  
168:  travpreorder(struct bnode *node)  
169:  {  
170:   if(node != NULL)  
171:   {  
172:    printf("\n%d",node->data);  
173:    travpreorder(node->left);  
174:    travpreorder(node->right);  
175:   }  
176:   return;  
177:  }  
178:  travpostorder(struct bnode *node)  
179:  {  
180:   if(node != NULL)  
181:   {  
182:    travpostorder(node->left);  
183:    travpostorder(node->right);  
184:    printf("\n%d",node->data);  
185:   }  
186:   return;  
187:  }  
188:  struct bnode *delete(struct bnode *fnd,int chk)  
189:  {  
190:   struct bnode *tmp;  
191:   if(fnd == NULL)  
192:     printf("\n Cant be deleted");\  
193:   else  
194:    if(chk < fnd->data)  
195:      fnd->left = delete(fnd->left,chk);  
196:   else  
197:    if(chk > fnd->data)  
198:      fnd->right = delete(fnd->right,chk);  
199:   else  
200:    if(fnd->left && fnd->right)  
201:    {  
202:     tmp = findmin(fnd->right);  
203:     fnd->data = tmp->data;  
204:     fnd->right = delete(fnd->right,tmp->data);  
205:    }  
206:   else  
207:   {  
208:    tmp = fnd;  
209:    if(fnd->left == NULL)  
210:     fnd = fnd->right;  
211:    else if(fnd->right == NULL)  
212:     fnd = fnd->left;  
213:    printf("\n Successfully deleted");  
214:   }  
215:   return fnd;  
216:  }  
217:  struct bnode *findmin(struct bnode *fnd)  
218:  {  
219:   if(fnd == NULL)  
220:    return NULL;  
221:   else  
222:    if(fnd->left == NULL)  
223:     return fnd;  
224:   else  
225:    return findmin(fnd->left);  
226:  }  
Read More

Prim's Algorithm

1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  void main()  
4:  {  
5:  int n,i,j,c[20][20],k,l,t[2][2],min=9999,mincost,n1[10]={0},min1=9999,j1;  
6:  clrscr();  
7:  printf("\n\tMINIMUM SPANNING TREE");  
8:  printf("\n\tENTER THE NO OF NODES");  
9:  scanf("%d",&n);  
10:  printf("\n\tENTER THE COST MATRIX");  
11:  printf("\n\tENTER 9999 for INFINITY");  
12:  for(i=1;i<=n;i++)  
13:  {  
14:       for(j=1;j<=n;j++)  
15:       {  
16:            if(i==j)  
17:            {  
18:            c[i][j]=0;  
19:            }  
20:       }  
21:  }  
22:  for(i=1;i<=n;i++)  
23:  {  
24:       for(j=i+1;j<=n;j++)  
25:       {  
26:       printf(" COST [%d][%d]: ",i,j);  
27:       scanf("%d",&c[i][j]);  
28:       }  
29:  }  
30:  for(i=2;i<=n;i++)  
31:  {  
32:       for(j=1;j<i;j++)  
33:       {  
34:       c[i][j]=c[j][i];  
35:       }  
36:  }  
37:  for(i=1;i<=n;i++)  
38:  {  
39:       for(j=i+1;j<=n;j++)  
40:       {  
41:            if(min>c[i][j])  
42:            {  
43:            min=c[i][j];  
44:            k=i;  
45:            l=j;  
46:            }  
47:       }  
48:  }  
49:  mincost=min;  
50:  min=9999;  
51:  t[1][1]=k;  
52:  t[1][2]=l;  
53:  for(i=1;i<=n;i++)  
54:  {  
55:       if(c[i][l]<c[i][k])  
56:       {  
57:       n1[i]=l;  
58:       }  
59:       else  
60:       n1[i]=k;  
61:  }  
62:  n1[k]=0;  
63:  n1[l]=0;  
64:  for(i=2;i<=n-1;i++)  
65:  {  
66:       for(j=1;j<=n;j++)  
67:       {  
68:            if(n1[j]!=0)  
69:            {  
70:                 if(min1>c[j][n1[j]])  
71:                 {  
72:                 min1=c[j][n1[j]];  
73:                 j1=j;  
74:                 }  
75:            }  
76:       }  
77:       t[i][1]=j1;  
78:       t[i][2]=n1[j1];  
79:       printf("%d\n%d",t[i][1],t[i][2]);  
80:       mincost=mincost+c[j1][n1[j1]];  
81:       min1=9999;  
82:       n1[j1]=0;  
83:       for(k=1;k<=n;k++)  
84:       {  
85:            if(n1[k]!=0)  
86:            {  
87:                 if((c[k][n1[k]])>(c[k][j1]))  
88:                 {  
89:                 n1[k]=j1;  
90:                 }  
91:            }  
92:       }  
93:  }  
94:  for(i=1;i<=n;i++)  
95:  {  
96:  for(j=1;j<=n;j++)  
97:  {  
98:  printf("%d",t[i][j]);  
99:  }  
100:  printf("\n");  
101:  }  
102:  printf("%d",mincost);  
103:  getch();  
104:  }  
Read More

Hamiltonian

 /*HAMILTIONIAN CYCLE*/  
 #include<iostream.h>  
 #include<conio.h>  
 void hamil(int);  
 void nv(int);  
 int n=4,g[10][10],x[20];  
 void main()  
 {  
      int i,j,k=2;  
      clrscr();  
      cout<<"\n\t\t\tHAMILTIONIAN CYCLE\n";  
      cout<<"\nEnter the no. of nodes:";  
      cin>>n;  
      cout<<"\nEnter the adjacency matrix\n";  
      for(i=1;i<=n;i++)  
      {  
           for(j=1;j<=n;j++)  
           {  
                cout<<" "<<i<<","<<j<<":";  
                cin>>g[i][j];  
           }  
      }  
      for(j=2;j<=n;j++)  
           x[j]=0;  
      hamil(k);  
      getch();  
 }  
 void hamil(int k)  
 {  
      int i;  
      x[1]=1;  
      while(1)  
      {  
           nv(k);  
           if(x[k]==0)  
           {  
             getch();  
             exit(1);  
                }  
           else if(k==n)  
           {  
                cout<<"\nThe solution is:";  
                for(i=1;i<=n;i++)  
                     cout<<x[i]<<’\t’;  
             cout<<"\n";  
             cout<<"\nThe given cycle is hamiltonian";  
           }  
           else  
                hamil(k+1);  
      }  
 }  
 void nv(int k)  
 {  
      int i;  
      do  
      {  
           x[k]=(x[k]+1)%(n+1);  
           if(x[k]==0)  
                return;  
           if(g[x[k-1]][x[k]]==1)  
           {  
                for(int j=1;j<=n;j++)  
                {  
                     if(x[j]==x[k])  
                          break;  
                }  
                if(j==k)  
                {  
                     if((k<n)||((k==n)&&(g[x[n],x[1]]!=0)))  
                          return;  
                }  
           }  
      }while(1);  
 }  
Read More

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);  
 }  
Read More

Chained matrix

 #include<iostream.h>  
 #include<conio.h>  
 class chained  
 {  
        private:  
              int n,i,j,k,min1,min2;  
              int m[100],p[100],q[100][100];  
       public:  
              void get();  
              void process();  
 }c;  
 void chained::get()  
 {  
       cout<<"\nEnter the total number of matrices:";  
       cin>>n;  
       for(i=1;i<=n;i++)  
       {  
              cout<<"\nEnter the size of the matrix M:";  
              cout<<i<<"\n";  
              cin>>m[i];  
              cin>>p[i];  
       }  
 }  
 void chained::process()  
 {  
       for(i=2;i<=n;i++)  
       {  
              for(j=1;j<i;j++)  
              {  
                 q[i][j]=9999;  
              }  
       }  
       for(i=1;i<=n;i++)  
              q[i][i]=0;  
       for(i=1;i<n;i++)  
              q[i][i+1]=(m[i]*p[i+1])*p[i];  
       for(i=1;i<n-1;i++)  
       {  
              min1=((m[i]*p[i+2])*p[i])+q[i][i+1];  
              min2=((m[i]*m[i+2])*p[i])+q[i+1][i+3];  
              if(min1<min2)  
                q[i][i+2]=min1;  
              else  
                q[i][i+2]=min2;  
       }  
       for(i=1;i<=n;i++)  
       {  
              cout<<"\n";  
              for(j=1;j<=n;j++)  
              {  
                cout<<"\t";  
                cout<<q[i][j];  
              }  
       }  
 }  
 void main()  
 {  
       clrscr();  
       c.get();  
       c.process();  
       getch();  
 }  
Read More

NQueen Problem

 #include<iostream.h>  
 #include<conio.h>  
 #include<math.h>  
 #include<dos.h>  
 void nqueen(int);  
 int place(int);  
 void disp(int);  
 int x[20], n;  
 void main()  
 {  
  clrscr();  
  cout<<"\n\t\tNQUEEN PROBLEM";  
  cout<<"\n\n\nEnter the number of queens ; ";  
  cin>>n;  
  nqueen(n);  
  getch();  
 }  
 void nqueen(int n)  
 {  
  int k,i,z;  
  k=z=1;  
  while(k>0)  
  {  
    x[k]=x[k]+1;  
    while((x[k]<=n)&&(!place(k)))  
      x[k]=x[k]+1;  
    if(x[k]<=n)  
    {  
      if(k==n)  
      {  
        cout<<"\n\n\nCOMBINATION "<<z<<'\n';  
        z++;  
        for(i=1;i<=n;i++)  
            disp(x[i]);  
        cout<<"\t";  
        delay(600);  
      }  
      else  
        x[++k]=0;  
    }  
    else  
      k--;  
  }  
 }  
 int place(int k)  
 {  
  int i;  
  for(i=1;i<=k-1;i++)  
    if((x[i]==x[k])||((abs(x[i]-x[k]))==(abs(i-k))))  
      return(0);  
  return(1);  
 }  
 void disp(int ch)  
 {  
  for(int f=1; f<=n; f++)  
    if(ch==f)  
      cout<<ch<<'\t';  
    else  
      cout<<"- \t";  
  cout<<'\n';  
 }  
Read More

SUm Of Subsets

 #include<iostream.h>  
 #include<conio.h>  
 int m,n,w[10],x[10]; //Global declaration of variables  
 void sum_subset(int s,int k,int r)  
 {  
   int i;  
   x[k]=1;  
   if(s+w[k]==m)  
   {  
     for(i=1;i<=n;i++)  
       cout<<"x["<<i<<"]:"<<x[i]<<"\t";  
     cout<<"\n";  
   }  
   else  
   {  
     if((s+w[k]+w[k+1])<=m)  
       sum_subset(s+w[k],k+1,r-w[k]); //Recursive call  
   }  
   if(((s+r-w[k])>=m)&&((s+w[k+1])<=m))  
   {  
     x[k]=0;  
     sum_subset(s,k+1,r-w[k]);  
   }  
 }  
 void main()  
 {  
   clrscr();  
   cout<<"Enter the number of values:";  
   cin>>n;  
   cout<<"Enter the sum: ";  
   cin>>m;  
   cout<<"Enter the values:";  
   int i;  
   for(i=1;i<=n;i++)  
     cin>>w[i];  
   int r;  
   r=0;  
   for(i=1;i<=n;i++)  
     r+=w[i];  
   sum_subset(0,1,r); //call the function to solve the sum of subsets problem  
   getch();  
 }  
Read More

Kruskal Algorithm

1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  int i, parent[10],t[20][2],mincost,k,ne;  
4:  struct edge  
5:  {  
6:  int u,v,c;  
7:  }e[20],temp;  
8:  swap(struct edge *p, struct edge *q)  
9:  {  
10:  temp=*p;  
11:  *p=*q;  
12:  *q=temp;  
13:  return;  
14:  }  
15:  int find(int c)  
16:  {  
17:  while(parent[c]>=0)  
18:  c=parent[c];  
19:  return c;  
20:  }  
21:  unionn(int x,int y)  
22:  {  
23:  parent[x]=y;  
24:  return;  
25:  }  
26:  void main()  
27:  {  
28:  int n, ch, j,k,a,b,ed,cycles;  
29:  clrscr();  
30:  printf("enter how many vertices:");  
31:  scanf("%d", &n);  
32:  printf("enter how many edges:");  
33:  scanf("%d", &ed);  
34:  printf("enter the edges and their costs...\n");  
35:  printf("\tfrom\tto\tcost\n");  
36:  for(i=1;i<=ed;i++)  
37:  {  
38:  printf("\t");  
39:  scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].c);  
40:  }  
41:  i--;  
42:  for(j=1;j<i;j++)  
43:  {  
44:  for(k=j+1;k<=i;k++)  
45:  if(e[k].c<e[j].c)  
46:  swap(&e[j],&e[k]);  
47:  }  
48:  for(j=1;j<=n;j++)  
49:  parent[j]=-1;  
50:  mincost=0;  
51:  k=ne=0;  
52:  cycles=0;  
53:  while((k<n-1)&&(ne<=i))  
54:  {  
55:  ne++;  
56:  a=find(e[ne].u);  
57:  b=find(e[ne].v);  
58:  if(a!=b)  
59:  {  
60:       k++;  
61:       t[k][1]=e[ne].u;  
62:       t[k][2]=e[ne].v;  
63:       mincost=mincost+e[ne].c;  
64:       unionn(a,b);  
65:  }  
66:  }  
67:  if(k!=n-1)  
68:  printf("\n no spanning tree");  
69:  else  
70:  {  
71:  printf("\n the edges in the min cost spanning tree....\n");  
72:  for(k=1;k<=n;k++)  
73:  printf("%d-->%d\n",t[k][1],t[k][2]);  
74:  printf("\n the min cost=%d",mincost);  
75:  }  
76:  getch();  
77:  }  
Read More

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