Friday, August 31, 2012

C Program on Graph traversing BFS and DFS

/*Link List Representation of Graph*/
#include<iostream.h>
#include<alloc.h>
#include<conio.h>
struct link
{
 struct node *points;
 struct link *next;
};
struct node
{
 int item,visit;
 struct link *adjacent;
 struct node *next;
}*head;
//---------------------------------------------------
//                Create Graph
//---------------------------------------------------

void create(struct node *h)
{
  cout<<"Enter Value of Vertex";
  cin>>h->item;
  h->visit=0;
  cout<<"Continue(Y/N)";
  if(getche()=='y')
  {
   h->next=(node*)malloc(sizeof(node));
   create(h->next);
  }
  else
  {
   h->next=NULL;
  }
}
node *search(node *h,int p)
{
 while(h!=NULL)
 {
  if(h->item==p)
  {return h;}
  else
  { h=h->next;}
 }
 return NULL;
}
void getAdjacent(node *h,link *l)
{
  if(h==NULL)return;
  if(l==NULL)
  {
   l=(link*)malloc(sizeof(link));
   h->adjacent=l;
  }
  cout<<"\nDoes the Vertex "<<h->item<<" has an adjacent (Y/N)?";
  if(getche()=='y')
  {

    int t;
    cin>>t;
    l->points=search(head,t);
    l->next=(link*)malloc(sizeof(link));
    getAdjacent(h,l->next);
   }
  else
  {
    l->next=NULL;
    getAdjacent(h->next,l->next);
  }
}
//---------------------------------------------------
//      Print Complete Graph With Adjacent
//---------------------------------------------------

void print (node *p)
{
  if(p==NULL)return;
  link *l=p->adjacent;
  cout<<"\nVertex.: "<<p->item;
  cout<<"\t Adjacent.: ";
  while(l->next!=NULL&&l!=NULL)
  {
   cout<<" "<<l->points->item;
   l=l->next;
  }
  print(p->next);
}

//---------------------------------------------------
//                D.F.S
//---------------------------------------------------
void DFS(node *q)
{
 if(q->visit==1)return;
 else
 {
  q->visit=1;
  cout<<" "<<q->item;
 }
 link *l;
 l=q->adjacent;
 while(l->next!=NULL)
 {
  DFS(l->points);
  l=l->next;
 }
}
//---------------------------------------------------
//                B.F.S
//---------------------------------------------------
struct queue
{
  struct node *element;
  struct queue *next;
}*Fq,*Rq;
void addQueue(node *n)
{
  if(Fq==NULL)
  {
   Rq=Fq=(queue*)malloc(sizeof(queue));
   Rq->element=n;
   return;
  }
  Rq->next=(queue*)malloc(sizeof(queue));
  Rq=Rq->next;
  Rq->element=n;
  Rq->next=NULL;
}
node *delQueue()
{
  if(Fq==NULL) return NULL;
  node *t=Fq->element;
  if(Rq==Fq)
  {
   Rq=Fq=NULL;
   return t;
  }
  queue *q=Fq;
  Fq=Fq->next;
  free(q);
  return t;
}
void BFS(node *h)
{
 node *t=h;
 while(t!=NULL)
 {
  t->visit=0;
  t=t->next;
 }
 addQueue(h);
 while(Fq!=NULL||Rq!=NULL)
 {
   t=delQueue();
   if(t->visit==0)
   {
     cout<<" "<<t->item;
     t->visit=1;
     link *l;
     l=t->adjacent;
     while(l->next!=NULL)
     {
      addQueue(l->points);
      l=l->next;
     }
   }
 }
}
//---------------------------------------------------
//                Main Function
//---------------------------------------------------
void main()
{
 clrscr();
 node *a;
 a=(node*)malloc(sizeof(node));
 head=a;
 create(a);
 link *l;
 l=NULL;
 getAdjacent(a,l);
 print(a);
 cout<<"\nD.F.S\n";
 DFS(a);
 Fq=NULL;
 Rq=NULL;
 cout<<"\nB.F.S\n";
 BFS(a);
 getch();
}

No comments:

Post a Comment