The BOTTOM LINE Quote Of The Day

The BOTTOM LINE Quote Of The Day

Don't Ever Tell GOD How BIG Your Problems are.
Just Tell Your Problems How BIG your GOD is ;)

Saturday, March 5, 2011

Breadth-First Search (BFS) Algorithm

#include<stdio.h>

#include<conio.h>
#include<iostream.h>
#include"graph01.cpp"          /* Refer this Source Code */
#define max 50

struct queue
{
 int front;
 int rear;
 struct node *ver[max];
}qu;

char string[max];

void BFS()
{
 printf("\n Enter the starting Node : ");
 cin>>item;
 qu.front = qu.rear = 1;
 find_node();
 if(loc != NULL)
 {
  loc->status = 2;
  qu.ver[qu.front] = loc;
 }
 else
  printf("\n\n Node not found !!\n\n");
 printf("\n\n The BFS Traversal Is : ");
 while(qu.front != 0)
 {
  loc = qu.ver[qu.front];
  printf(" %c",loc->info);
  loc->status = 3;
  qu.front++;
  ptre = loc->adj;
  while(ptre!=NULL)
  {
   if(ptre->dest->status == 1)
   {
    ptre->dest->status = 2;
    qu.rear += 1;
    qu.ver[qu.rear] = ptre->dest;
   }
  ptre = ptre->link;
  }
  if(qu.front > qu.rear)
   qu.front = 0;
 }
}

void main()
{
clrscr();
create_node();
create_vertices();
BFS();
getch();
}

No comments:

Post a Comment