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



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

Answer / 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

More Programming Languages AllOther Interview Questions

how many languages .net is supporting now?

2 Answers  


Who is providing best mainframes online training in Hyderabad

1 Answers  


how many types of operating system are avaliable?

0 Answers   IBM,


write algo for cobol program whichuse three flat file to extract some specific information 8 marks mainframe

0 Answers  


There are 3 jars containing two types of round marbles. One jar contains only red marbles, one jar contains only blue marbles and the third jar contains a mix of both red and blue marbles. Although the jars are labeled “red”, “blue” and “mixed” – all the jars are mislabeled. How many marbles would you have to pull out, and out of which jars, to find out how to fix the labels correctly?

1 Answers   Syntel,






what will we require to build project with the help of oracle

0 Answers  


1.) - Design the class model and produce a class diagram for a set of vehicle types for a vehicle manufacturer. At a minimum, the class model should include the following information: - Type of vehicle (e.g. sedan, truck, etc), - Number of seats - Number of doors - Color of the vehicle - Transmission type (auto vs manual) - Engine’s rated horsepower - Number of engine cylinders - Engine size (in liters) - Number of stereo speakers - Does it include satellite radio? - Does it have a CD player? (and if so, how many CDs can it hold?) - Is it front, rear, all, or 4 wheel drive? - Truck bed type (e.g. short, standard, long) - Trunk size (for those vehicle types that have a trunk) 2.) I want to create an in-memory definition for a set of airline routes for a small airline company. I want to maintain a list of all of my planes and their current locations (or their destination location if they are currently in the air). One of the possible locations needs to be “hangar” for maintenance or repairs. I want to have the full airline schedule available so that I can look for available flights. I also want to store information on which planes are operating each schedule. Assuming that a customer comes to the ticketing counter at one of my airports when it opens at 6:00am, I want to be able to give a customer the fastest option to get from one airport to another. Keep in mind that it may take more than one flight to get from one airport to another. Describe how you would fulfill this request with your design. 3.) I want to create a course registration database for a university. I want to store information for each department, information for each course (including which department they’re offered under as well as which professors are teaching them), information for each professor, and the list of the registered students and which courses each student has registered for. Keep in mind that an instance of a course could be taught by multiple instructors (i.e. one instructor for the first half of the course and then a different instructor for the second half). Also keep in mind that there could be multiple instances of a given course offered by different sets of instructors (e.g. offered by Bob Smith on Mondays & Wednesdays at 10am and Bill Jones on Tuesdays & Thursdays at 1pm). Design a set of database tables to store this information. 4.) Given the following: You have an application that consists of three parts: a front end GUI, a middle-ware layer where all the processing of data takes place and a database where data is read from. What are the areas that would be most likely to break? What would your testing strategy for this be? Why? 5.) Imagine I am handing you a wine glass and I ask you to test it. What would your testing strategy for this be?

1 Answers   GE,


What is meant by SQL,PL/SQL,SQL PLUS? Is there any differnece between them?

2 Answers   HSBC,


hai i am prasanna.I am MCA 2009 fresher.tell me about certifications.which certification helps me to improve my carrier and to get a technically oriented job ,which certification helps to get job faster.

0 Answers  


what is polymorphism in java.

3 Answers   Atos Origin,


1.Mutating table

0 Answers  


HTML is a subset of

8 Answers  


Categories