0% found this document useful (0 votes)
5 views8 pages

BFS DFS

The document contains two C programs for graph traversal algorithms: Breadth-First Search (BFS) and Depth-First Search (DFS). Each program prompts the user for the number of vertices and edges, constructs an adjacency matrix representation of the graph, and then performs the respective traversal starting from a user-specified root vertex. Both programs include user input for edges and display the traversal order of the vertices.

Uploaded by

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

BFS DFS

The document contains two C programs for graph traversal algorithms: Breadth-First Search (BFS) and Depth-First Search (DFS). Each program prompts the user for the number of vertices and edges, constructs an adjacency matrix representation of the graph, and then performs the respective traversal starting from a user-specified root vertex. Both programs include user input for edges and display the traversal order of the vertices.

Uploaded by

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

write c program to perform BFS TRAVERSAL

CODE:

#include<stdio.h>

#include<stdlib.h>

void main()

int vertices,edges,i,j,cnt=0;

int a=0,b=0;

printf("Enter no.of Vertices: ");

scanf("%d",&vertices);

printf("Enetr no.of Edges: ");

scanf("%d",&edges);

int graph[vertices][vertices];

printf("Assuming vertices as 0,1,2..%d give the list of edges.\n",vertices-1);

for(i=0;i<edges;i++)

printf("Edge is between\nVertex-1: ");

scanf("%d",&a);

printf("Vertex-2: ");

scanf("%d",&b);

graph[a][b]=1;

graph[b][a]=1;

for(i=0;i<vertices;i++)

for(j=0;j<vertices;j++)

{
if(graph[i][j]!=1)

graph[i][j]=0;

for(i=0;i<vertices;i++)

for(j=0;j<vertices;j++)

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

printf("\n");

int Queue[100]={-1};

int front=0,rear=-1;

printf("which vertex do you want to consider as Root: ");

scanf("%d",&a);

printf("BFS TRAVERSAL: ");

rear++;

Queue[rear]=a;

while(rear<vertices)

for(i=0;i<vertices;i++)

if(graph[a][i]==1)

int c=0;

for(j=0;j<vertices;j++)
{

if(i==Queue[j])

c++;

break;

if(c==0)

rear++;

Queue[rear]=i;

if(cnt<vertices)

printf("%d ",Queue[front]);

cnt++;

front++;

a=Queue[front];

else

break;

}
write c program to implement DFS TRAVERSAL

CODE:

#include<stdio.h>

#include<stdlib.h>

void main()

int vertices,edges,i,j,cnt=0,printCount=0;

int a=0,b=0;

printf("Enter no.of Vertices: ");

scanf("%d",&vertices);

printf("Enetr no.of Edges: ");


scanf("%d",&edges);

int graph[vertices][vertices];

printf("Assuming vertices as 0,1,2..%d give the list of edges.\n",vertices-1);

for(i=0;i<edges;i++)

printf("Edge is between\nVertex-1: ");

scanf("%d",&a);

printf("Vertex-2: ");

scanf("%d",&b);

graph[a][b]=1;

graph[b][a]=1;

for(i=0;i<vertices;i++)

for(j=0;j<vertices;j++)

if(graph[i][j]!=1)

graph[i][j]=0;

for(i=0;i<vertices;i++)

for(j=0;j<vertices;j++)

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

printf("\n");

}
int stack[100]={-1};

int top=-1;

printf("which vertex do you want to consider as Root: ");

scanf("%d",&a);

printf("DFS TRAVERSAL: ");

top++; stack[top]=a;

while(top<vertices)

for(i=0;i<vertices;i++)

cnt=0;

if(graph[a][i]==1)

int c=0;

for(j=0;j<vertices;j++)

if(i==stack[j])

c++;

break;

if(c==0)

top++;

stack[top]=i;

cnt++;

break;

}
}

if(printCount<vertices)

printf("%d ",stack[top]);

printCount++;

if(cnt==0)

top--;

a=stack[top];

else

break;

You might also like