Thursday 11 July 2013

Program to implement Dijkstra's Algorithm in c++

#include<iostream>
#define SIZE 12
#define INFINITY 999
using namespace std;

class Queu
{
      public:
             Queu();
             int isempty();
             void insert(int node);
             int remove();
             void display();
      private:
              int front;
              int rear;
              int* queue;
};


Queu :: Queu()
{
     queue = new int[SIZE];
     front = 0 ;
     rear = 0 ;
   
}
void Queu :: insert(int node)
{
     if( (front == 1) && (rear == 11) )
     {
              cout << "Overflow" <<endl;
     }
     else if(front == 0)
     {
          queue[++front] = node;
          rear++;
     }
     else
     {
         queue[++rear] = node;
     }
           
}
int Queu :: remove()
{
     int item = 0;
     if(front == 0 )
     {
              cout << "underflow" <<endl;
     }
     else if(front == rear )
     {
          item = queue[rear];
          front = 0 ;
          rear = 0;
          return item;
     }
     else
     {
         item = queue[front++];
         return item;
     }
     return 0;
       
   
}
int Queu :: isempty()
{
    if(front == 0)
    {
             return 0;
    }
    else
    {
        return 1;
    }
}

void Queu :: display()
{
     cout<<"Queue is ";
     for(int i = front ; i <= rear ;  i++)
     {
             cout<<queue[front]<<" ";
     }
}

class Djalgo
{
      public:
             Djalgo();
             void insert();
             void search();
             void initialize();
      private:
              int vertices;
              int* dist;
              int** cost;
              int edges;
              int* visited;
              int *pvisit;
              int min();
              Queu Q;
};

Djalgo :: Djalgo()
{
       vertices = 0 ;
       dist = new int[SIZE];
       cost = new int*[SIZE];
       edges = 0 ;
       visited = new int[SIZE];
       pvisit = new int[SIZE];
}

void Djalgo :: initialize()
{
   
     int n = 0 ;
     int i = 0 , j = 0 ;
     cout << "Enter number of vertices " << endl;
     cin >> vertices ;
     cout << " Enter The vertices " << endl;
     for(int l = 0 ; l < SIZE  ; l ++)
     {
             cost[l] = new int [SIZE];
             visited[l] = 0;
             dist[l] = INFINITY;
             pvisit[l] = 0;
             if(l < vertices)
             {
             cin >> n ;
             Q.insert(n);
             }
     for(int p = 0 ; p < SIZE ; p++)
     {
             cost[l][p] = 0;
     }
           
     }
     cout << " Enter the number of edges" <<endl;
     cin>>edges;
     for(int k = 0 ; k < edges ; k++)
     {
             cout <<"Enter the edge" << endl;
             cin>>i>>j;
             cout<<"Its cost "<< endl;
             cin >> cost[i][j];
           
     }
}
int Djalgo :: min()
{
    int temp = INFINITY;
    int node = 1 ;
    int flag = 0;
    for( int i = 1 ; i < SIZE ; i ++)
    {
         if( (visited[i] == 0 ) )
         {
             if(temp > dist[i])
             {
                     temp = dist[i];
                     node = i;
             }
         }
    }
    return node;
}
           
void Djalgo :: search()
{
     int i = 1 , v = 0 , s = 0 ;
     cout << "Enter the source Vertex " <<endl;
     cin>>s;
     dist[s] = 0;
     while(Q.isempty())
     {
                       v = min();
                       cout<<"Current "<<v<<endl;
                 Q.remove();
                visited[v] = 1;
                 pvisit[i++] = v;
                 for(int k = 1 ; k < SIZE ; k++)
                 {
                         if( (cost[v][k] != 0) && (visited[k] == 0) )
                         {
                             if(dist[k] > dist[v] + cost[v][k])
                             {
                             dist[k] = dist[v] + cost[v][k];
                             }
                         }
                 }
     }
     for(int f = 1 ; f < i ; f++)
     {
             cout << "source vertex" << s << "Destination" << pvisit[f] << " Cost" << dist[pvisit[f]] << endl;
     }
   
}
                   
   
   

int main()
{
    Djalgo obj;
    obj.initialize();
    obj.search();
    cin.get();
    cin.get();
    return 0;
}

                                                                         BY: SAURABH BHATIA

Wednesday 10 July 2013

Program to implement Breadth First Search in C++

#include<iostream>
#define SIZE 12
using namespace std;

class Queu
{
      public:
             Queu();
             int isempty();
             void insert(int node);
             int remove();
             void display();
      private:
              int front;
              int rear;
              int* queue;
};

Queu :: Queu()
{
     queue = new int[SIZE];
     front = 0 ; 
     rear = 0 ; 
     
}
void Queu :: insert(int node)
{
     if( (front == 1) && (rear == 11) )
     {
              cout << "Overflow" <<endl;
     }
     else if(front == 0)
     {
          queue[++front] = node;
          rear++;
     }
     else
     {
         queue[++rear] = node;
     }
              
}
int Queu :: remove()
{
     int item = 0;
     if(front == 0 )
     {
              cout << "underflow" <<endl;
     }
     else if(front == rear )
     {
          item = queue[rear];
          front = 0 ; 
          rear = 0;
          return item;
     }
     else
     {
         item = queue[front++];
         return item;
     }
          
     
}
int Queu :: isempty()
{
    if(front == 0)
    {
             return 0;
    }
    else
    {
        return 1;
    }
}

void Queu :: display()
{
     cout<<"Queue is ";
     for(int i = front ; i <= rear ;  i++)
     {
             cout<<queue[front]<<" ";
     }
}
class Bfs
{
      public:
             Bfs();
             void insert();
             void search();
             void initialize();
             private:
                     int vertices;
                     int* status;
                     int** cost;
                     int edges;
                     int* pvisit; // used to store the parent of current processed node
};

      

Bfs :: Bfs ()
{
    vertices =  0 ; 
    edges = 0 ;  
    status = new int[SIZE];
    pvisit = new int [SIZE];
    cost = new int*[SIZE];
    initialize();
}

void Bfs :: initialize()
{
     for(int i = 0 ; i < SIZE ; i ++)
     {
             cost[i] = new int [SIZE];
             status[i] = 0 ; 
             pvisit[i] = 0 ;
     }

}
 

void Bfs :: insert()
{
     cout << "Enter Number of Vertices" << endl;
     cin >> vertices;
     cout << "Enter number of edges" << endl;
     cin >> edges;
     cout << "Enter the Edge " <<endl;
     int i = 0 , j = 0 ;
     for(int k = 0 ; k < edges ; k++)
     {
             cin>>i>>j;
             cost[i][j] = 1;
             
     
      }
}

void Bfs :: search()
{
     Queu Q;
     int v1 = 0 , v2 = 0 ;
     cout << "Enter the initial vertex" << endl;
     cin >> v1;
     cout<< "Enter the Final vertex " << endl;
     cin >> v2;
     Q.insert(v1);
     pvisit[v1] = v1;
     status[v1] = 1 ;
     int flag = 0;
     while(Q.isempty())
     {
                  v1 = Q.remove();
                  status[v1] = 2;
                  if(flag == 0 )
                  for(int i = 1 ; i <= vertices; i ++)
                  {
                          if( (cost[v1][i] == 1 ) && (status[i] == 0) )
                          {
                              Q.insert(i);
                              pvisit[i] = v1;
                              status[i] = 1 ;
                              if( i == v2)
                              {
                              flag = 1;
                              break;
                              }
                          }
                  }
                  else
                  break;
                  
     }
     cout << v2 << " " ;
     while(v2 != pvisit[v2])
     {
             cout << pvisit[v2] <<" " ;
             v2 = pvisit[v2];
     }

     
}

int main()
{
    Bfs obj;
    obj.initialize();
    obj.insert();
    obj.search();
    cin.get();
    cin.get();
    return 0;
}
                                                                       BY:  SAURABH BHATIA

Program to implement Depth First Search in C++

#include<iostream>
#define SIZE
using namespace std;
class Dfs
{
      public:
             Dfs();
             void insert();
             void traverse();
             void initialize();
             private:
                     int status*;
                     int cost**;
                     int stack*;
                     int top ;
                     int vertices;
                     int edges;
                     int k ;
                   
};

Dfs :: Dfs()
{
    top = 0 ;
    k = 0 ;
    edges = 0;
    vertices = 0 ;
    status = new int[SIZE];
    stack = new int[SIZE];
    cost  = new int*[SIZE];
    initialize();
 
}
void Dfs :: initialize()
{
     for(int i = 0 ; i < SIZE ; i++)
     {
             cost[i] = new int[SIZE];
             status[i] = 0 ;
     }
}
           
void Dfs :: insert()
{
   
     int i = 0 , j = 0 ;
     cout << "Enter number of vertices (1 - 12)" <<endl;
     cin>>vertices;
     cout << "Enter number of edges" << endl;
     cin>>edges;
     cout << "Enter the edges" << endl;
     for(k = 1 ; k <= edges ; k ++)
     {
     cin >> i >> j;
     cost[i][j]  = 1 ;
     }
}

void Dfs :: traverse()
{
     int v = 0 ;
     cout << "Enter The initial vertex" << endl;
     cin >> v;
     k = 0;

     stack[++top] = v;
     while(top != 0 )
     {
                status[v] = 2;
                cout << v <<" " ;
               for(int i  = 1 ; i <= 11 ; i++ )
               {
                     if( (cost[v][i] == 1) && (status[i] == 0) )
                     {
                       
                         stack[++top] = i ;
                         status[i] = 1 ;
                     }
               }
           
              v = stack[top--];
           
             
           
             
     }
}
int main()
{
    Dfs obj;
    obj.insert();
    obj.traverse();
    cin.get();
    cin.get();
    return 0 ;
}
                                                                        BY : SAURABH BHATIA

Tuesday 9 July 2013

Program to implement the traversals (Postorder , Inorder , Postorder) and searching in Binary Search Tree in c++

#include<iostream>
using namespace std;
class Btree
{
      private:
      int data;
      Btree *left;
      Btree *right;
      Btree *root;
      void insert(Btree* troot,int value);
      void destroy(Btree* troot);
      void preOrder(Btree *troot);
      void inOrder(Btree *troot);
      void postOrder(Btree *troot);
      void search(Btree *troot , int value);
     
      public:
             Btree();
             void insert(int value);
             void preOrder();
             void inOrder();
             void postOrder();
             void search(int value);
             void destroy();
           
};
     
     
      Btree::Btree()
      {
             data = 0;
       
             left = NULL;
             right = NULL;
             root = NULL;
      }
      void Btree:: insert(int value)
      {
           if( root == NULL )
           {
               Btree *temp = new Btree();
           temp -> data = value;
                  root = temp;
                  temp -> left = NULL;
                  temp -> right = NULL;
             
           }
           else
           {
               insert(root , value);
           }
       }
     
      void Btree :: insert( Btree *troot , int value)
      {
         
           if(troot -> data > value)
           {
                if( troot -> left == NULL)
                {
                     Btree *temp = new Btree();
                     temp -> data = value;
                     troot -> left = temp;
                     temp -> left = NULL;
                     temp -> right = NULL;
                   
                }
                else
                {
               
                    insert(troot->left , value);
                }
           }
           else if(troot -> data < value)
           {
                if( troot -> right == NULL )
                {
                    Btree *temp = new Btree();
                    temp -> data = value;
                    troot -> right = temp;
                    temp -> left = NULL ;
                    temp -> right = NULL;
                   
                }
                else
                {
                 insert (troot->right , value);
                }
           }
           else
           {
               cout << " The given data is already in the the tree " << endl;
           }
      }  
   
      void Btree :: preOrder()
      {
           cout << "Preorder Traversal " << endl;
           preOrder(root);
       }
     
        void Btree :: preOrder(Btree* troot)
       {
            if(troot != NULL)
            {
            cout<<troot -> data<<endl;
            preOrder(troot -> left);
            preOrder(troot -> right);
            }
        }
     
       void Btree :: inOrder()
       {
            cout << " Inorder Traversal " << endl;
            inOrder(root);
        }
       
        void Btree :: inOrder (Btree* troot)
        {
           
             if(troot != NULL)
             {
             inOrder(troot -> left);
             cout << troot -> data <<endl;
             inOrder(troot -> right);
             }
         }
     
     
        void Btree:: postOrder()
        {
             cout << "Postorder Traversal "<<endl;
             postOrder(root);
        }
        void Btree :: postOrder(Btree* troot)
        {
             if(troot != NULL)
             {
             postOrder(troot -> left);
             postOrder(troot -> right);
             cout << troot -> data <<endl;
             }
         }
       
         void Btree :: search(int value)
         {
             
              search(root , value);
             
         }
         void Btree :: search(Btree* troot , int value)
         {
              if(troot -> data == value)
                  {
                           cout <<"The value is present"<<endl;
                  }
             
              else if( (value < troot -> data) && (troot -> left != NULL))
              {
                 
                 
                      search(troot -> left , value);
                 
                 
              }
              else if ( (value > troot -> data) && (troot -> right != NULL) )
              {
         
                       search (troot -> right , data);
                 
              }
              else
              {
                  cout << "The value is not in the Tree " << endl;
              }
             
         }
       
       void Btree :: destroy()
      {
           destroy(root);
      }
     
      void Btree :: destroy(Btree* troot)
      {
           if(troot != NULL)
           {
                    destroy(troot -> left);
                    destroy(troot -> right);
                    delete troot;
           }
      }
     
      int main()
      {
          Btree obj;
          int data = 0;
         obj.insert(40);
         obj.insert(60);
         obj.insert(50);
         obj.insert(33);
         obj.insert(55);
         obj.insert(11);
         obj.insert(10);
       
         obj.preOrder();   // Preorder traversal
         obj.inOrder();    // inorder traversal
         obj.postOrder();  // postorder traversal
       
         cout << " Enter the Data to be searched " << endl;
         cin >> data;
         obj.search(data);  // we can also return the node
         obj.destroy();
         
          cin.get();
          cin.get();
          return 0;
}
                                                                                  BY: SAURABH BHATIA          
     

Monday 8 July 2013

Program to find larger of two number without using relational operator , conditional operator ,if else statements in C/C++...?

I have written the simple code for this without any use of ant bitwise operator....


#include<stdio.h>
#include<iostream>
using namespace std;
int main(void)
{
    int a = 0 , b = 0 ;
    cout << "Enter two Numbers" << endl;
    scanf("%d",&a);
    scanf("%d",&b);
    int c = a - b ;
    int temp = c + abs(c);
    c = a % b ;
    switch(c)
    {
                case 0 :
                     printf(" a and b are equal \n");
                     break;
                     default:
                             switch(temp)
                             {
                                         case 0:
                                              printf(" a is smaller than b \n");
                                              break;
                                              default:
                                                      printf(" a is greater than b \n");
                             }
    }
    return 0;
}
                                                                                    By: Saurabh Bhatia