key for important topicsBFS(breath first search) program in c++

 /* C++ Program to implement BFS Algorithm for Connected Graph */

#include<iostream>
using namespace std;

#define MAX 100

#define initial 1
#define waiting 2
#define visited 3

int n; /*Number of vertices in the graph*/
int adj[MAX][MAX]; /*Adjacency Matrix*/
int state[MAX]; /*can be initial, waiting or visited*/

void create_graph();
void BF_Traversal();
void BFS(int v);

int queue[MAX], front = -1,rear = -1;
void insert_queue(int vertex);
int delete_queue();
int isEmpty_queue();

int main()
{
create_graph();
BF_Traversal();

return 0;

}/*End of main()*/

void BF_Traversal()
{
int v;

for(v=0; v<n; v++)
state[v] = initial;

cout<<("\nEnter starting vertex for Breadth First Search : ");
cin>>(v);
BFS(v);
}/*End of BF_Traversal()*/

void BFS(int v)
{
int i;

insert_queue(v);
state[v] = waiting;

while(!isEmpty_queue())
{
v = delete_queue( );
cout<<(v);
state[v] = visited;

for(i=0; i<n; i++)
{
/*Check for adjacent unvisited vertices */
if(adj[v][i] == 1 && state[i] == initial)
{
insert_queue(i);
state[i] = waiting;
}
}
}
cout<<("\n");
}/*End of BFS()*/

void insert_queue(int vertex)
{
if(rear == MAX-1)
cout<<("\nQueue Overflow\n");
else
{
if(front == -1) /*If queue is initially empty */
front = 0;
rear = rear+1;
queue[rear] = vertex ;
}
}/*End of insert_queue()*/

int isEmpty_queue()
{
if(front == -1 || front > rear)
return 1;
else
return 0;
}/*End of isEmpty_queue()*/

int delete_queue()
{
int del_item;
if(front == -1 || front > rear)
{
cout<<("\nQueue Underflow\n");
exit(1);
}

del_item = queue[front];
front = front+1;
return del_item;
}/*End of delete_queue() */

void create_graph()
{
int i,max_edges,origin,destin;

cout<<("\nEnter number of vertices : ");
cin>>(n);
max_edges = n*(n-1);

for(i=1; i<=max_edges; i++)
{
cout<<"\nEnter edge( -1 -1 to quit )"<<i<<":";
cin>>origin;
cin>>destin;

if((origin == -1) && (destin == -1))
break;

if(origin>=n || destin>=n || origin<0 || destin<0)
{
cout<<("\nInvalid edge!\n");
i--;
}
else
{
adj[origin][destin] = 1;
}
}/*End of for*/
}/*End of create_graph()*/

Comments

Popular Posts