0% found this document useful (0 votes)
51 views6 pages

Experiment-9 AIM:-write A Program For BFS (Breadath First Search) and DFS (Depth First Search)

The document describes a program to implement breadth-first search (BFS) and depth-first search (DFS) on a graph. It takes the number of vertices and adjacency matrix of the graph as input. It implements queues for BFS and stacks for DFS to traverse the graph. The main functions are bfs() and dfs() which take the source vertex and number of vertices as parameters and print the order of visited vertices.

Uploaded by

HARI PRASAD
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views6 pages

Experiment-9 AIM:-write A Program For BFS (Breadath First Search) and DFS (Depth First Search)

The document describes a program to implement breadth-first search (BFS) and depth-first search (DFS) on a graph. It takes the number of vertices and adjacency matrix of the graph as input. It implements queues for BFS and stacks for DFS to traverse the graph. The main functions are bfs() and dfs() which take the source vertex and number of vertices as parameters and print the order of visited vertices.

Uploaded by

HARI PRASAD
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

EXPERIMENT-9

AIM:-write a program for BFS(breadath first search) and DFS(depth first


search).
#include<stdio.h>

#include<conio.h>

int q[20],a[20][20],vis[20],stack[20];

int top=-1,front=-1,rear=-1;

int delet(void);

void add(int item);

void bfs(int s,int n);

void dfs(int s,int n);

void push(int item);

int pop();

void main()

int n,i,s,ch,j;

int c;

printf("\nEnter The Number Vertices\t");

scanf("%d",&n);

printf("\nEnter The Adjacency Matrix\n");

for(i=1;i<=n;i++)

for(j=1;j<=n;j++)

scanf("%d",&a[i][j]);

printf("\nAdjacency Matrix is\n");

for(i=1;i<=n;i++)

for(j=1;j<=n;j++)
{

printf(" %d",a[i][j]);

printf("\n");

do

for(i=1;i<=n;i++)

vis[i]=0;

printf("\nMENU");

printf("\n1.D.F.S");

printf("\n2.B.F.S");

printf("\nEnter Your Choice\t");

scanf("%d",&ch);

printf("\nSource Vertex :\t");

scanf("%d",&s);

switch(ch)

case 1:dfs(s,n);

break;

case 2:

bfs(s,n);

break;

printf("\nEnter 1 to continue else 0 ");

scanf("%d",&c);

}while((c!=0));

void bfs(int s,int n)

{
int p,i;

add(s);

vis[s]=1;

p=delet();

if(p!=0)

printf(" %d",p);

while(p!=0)

for(i=1;i<=n;i++)

if((a[p][i]!=0)&&(vis[i]==0))

add(i);

vis[i]=1;

p=delet();

if(p!=0)

printf(" %d ",p);

for(i=1;i<=n;i++)

if(vis[i]==0)

bfs(i,n);

void add(int item)

if(rear==19)

printf("QUEUE FULL");

else

if(rear==-1)

q[++rear]=item;
front++;

else

q[++rear]=item;

int delet()

int k;

if((front>rear)||(front==-1))

return(0);

else

k=q[f++];

return(k);

void dfs(int s,int n)

int i,k;

push(s);

vis[s]=1;

k=pop();

if(k!=0)

printf(" %d ",k);

while(k!=0)

for(i=1;i<=n;i++)

if((a[k][i]!=0)&&(vis[i]==0))

push(i);
vis[i]=1;

k=pop();

if(k!=0)

printf(" %d ",k);

for(i=1;i<=n;i++)

if(vis[i]==0)

dfs(i,n);

void push(int item)

if(top==19)

printf("Stack overflow ");

else

stack[++t]=item;

int pop()

int k;

if(top==-1)

return(0);

else

k=stack[top--];

return(k);

}
}

18341A05E0

You might also like