Write a program to implement BFS/ DFS routine in a connected
graph

Answer Posted / akshay p

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

void create(); // For creating a graph
void dfs(); // For Deapth First Search(DFS) Traversal.
void bfs(); // For Breadth First Search(BFS) Traversal.

struct node // Structure for elements in the graph
{
int data,status;
struct node *next;
struct link *adj;
};

struct link // Structure for adjacency list
{
struct node *next;
struct link *adj;
};

struct node *start,*p,*q;
struct link *l,*k;

int main()
{
int choice;
clrscr();
create();
while(1)
{
cout<<"-----------------------\n\n";
cout<<"1: DFS\n\n2: BSF\n\n3: Exit\n\nEnter
your choice: \n";
cin>>choice;
switch(choice)
{
case 1:
dfs();
break;
case 2:
bfs();
break;
case 3:
exit(0);
break;
default:
cout<<"Incorrect choice!Re-enter your
choice.";
getch();
}
}
return 0;
}

void create() // Creating a graph
{
int dat,flag=0;
start=NULL;
cout<<"Enter the nodes in the graph(0 to end): ";
while(1)
{
cin>>dat;
if(dat==0)
break;
p=new node;
p->data=dat;
p->status=0;
p->next=NULL;
p->adj=NULL;
if(flag==0)
{
start=p;
q=p;
flag++;
}
else
{
q->next=p;
q=p;
}
}
p=start;
while(p!=NULL)
{
cout<<"Enter the links to "<<p->data<<" (0 to
end) : ";
flag=0;
while(1)
{
cin>>dat;
if(dat==0)
break;
k=new link;
k->adj=NULL;
if(flag==0)
{
p->adj=k;
l=k;
flag++;
}
else
{
l->adj=k;
l=k;
}
q=start;
while(q!=NULL)
{
if(q->data==dat)
k->next=q;
q=q->next;
}
}
p=p->next;
}
cout<<"-------------------------";
return;
}


// Deapth First Search(DFS) Traversal.
void bfs()
{
int qu[20],i=1,j=0;
p=start;
while(p!=NULL)
{
p->status=0;
p=p->next;
}
p=start;
qu[0]=p->data;
p->status=1;
while(1)
{
if(qu[j]==0)
break;
p=start;
while(p!=NULL)
{
if(p->data==qu[j])
break;
p=p->next;
}
k=p->adj;
while(k!=NULL)
{
q=k->next;
if(q->status==0)
{
qu[i]=q->data;
q->status=1;
qu[i+1]=0;
i++;
}
k=k->adj;
}
j++;
}
j=0;
cout<<"Breadth First Search Results";
cout<<"---------------------------";
while(qu[j]!=0)
{
cout<<qu[j]<<" ";
j++;
}
getch();
return;
}


// Breadth First Search(BFS) Traversal.
void dfs()
{
int stack[25],top=1;
cout<<"Deapth First Search Results";
cout<<"---------------------------";
p=start;
while(p!=NULL)
{
p->status=0;
p=p->next;
}
p=start;
stack[0]=0;
stack[top]=p->data;
p->status=1;
while(1)
{
if(stack[top]==0)
break;
p=start;
while(p!=NULL)
{
if(p->data==stack[top])
break;
p=p->next;
}
cout<<stack[top]<<" ";
top--;
k=p->adj;
while(k!=NULL)
{
q=k->next;
if(q->status==0)
{
top++;
stack[top]=q->data;
q->status=1;
}
k=k->adj;
}
}
getch();
return;
}

Is This Answer Correct ?    22 Yes 12 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

they asked me about srs (software requirement specifcation)? how can i get anydocumentation about srs & other documnts infomation like bdd, in testing? its urgent?

1454


hi This is radhika.Can anyone help me to know the question papers of NATIONAL INFORMATICS CENTRE for the post of scientific officer/engineer? if anyone know plz tell me question paper pattern

1528


How to merge Action Form with Dyna Action Form in Struts.

1896


how to stop execution of step 3 in a job mainframe

1711


Write a program to create a process in UNIX

2090






what is the diffrence between software and hardware language?

1821


How can recruiter justified that the candidate is expert in Algorithm and datastructure for Software product development ?

1503


V2 SOLUTIONS APTI paper is very easy there are 3 sections 1'st is quant problem on age train traveling speed is given length of train is given we have to find out length of bridge then prob on calender one date is given on Friday hv to find out day of othe date ans is Friday then there was 1 prob on percentage which was very easy ans was 21340 then 2’nd section was english grammer had has been being like that hv to fill in the blanks was very wasy then last section was find relative words there was five Q 1 pant ANS:-breath 2inquire ans :- ask And 3 more out of that 1 ‘s ans was:- body 4 And others ans was :-through THEN THEY HAVE GD There was three topics 1 should we allow india’s talent to go to abrod 2 protest against seperate state is justify 3 inturuption of politics in cricket Thats all guys i cleared both the rounds now preparing 4 interview best of luck

1775


how to convert hashmap to arraylist with iteration

1196


In project we have Documentation phase also,in that what is micro and macro designing?

1424


what are stubs related to foxpro?

1660


Hi can you please help for the following. I have a ASP.Net web page I want to print the whole page how is it possible? I want It in ASP also.Please send me the solutions dipankar.hazari@gmail.com . Thanks in advance.

2324


5. Which of the following can you do with DB2 Express- C? Query databases with SQL Query databases with XML using XQuery and XPath Use SQL in Xquery and Xquery in SQL All of the above

2125


Difference between debugging, running, executing of an application

3485


What is dialog programming?

1580