0% found this document useful (0 votes)
62 views4 pages

BFS-Binary First Search

The document contains source code for a C program that implements breadth-first search (BFS) on a graph. It takes the number of vertices as input, initializes arrays to track the graph data, queue, visited nodes, and more. It then takes the graph data and starting vertex as input. The bfs function performs BFS traversal, marking visited nodes and adding neighbors to the queue. It prints the reachable nodes from the starting vertex as output.

Uploaded by

Ràgúl RS
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)
62 views4 pages

BFS-Binary First Search

The document contains source code for a C program that implements breadth-first search (BFS) on a graph. It takes the number of vertices as input, initializes arrays to track the graph data, queue, visited nodes, and more. It then takes the graph data and starting vertex as input. The bfs function performs BFS traversal, marking visited nodes and adding neighbors to the queue. It prints the reachable nodes from the starting vertex as output.

Uploaded by

Ràgúl RS
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/ 4

D.

RAGUL SINGH(1806037)

SOURCE CODE:

#include<stdio.h>
#include<conio.h>
int a[10][10],q[10],visited[10],n,i,j,f=0,r=-1;
voidbfs(int v)
{
for (i=1;i<=n;i++)
if(a[v][i] && !visited[i])
q[++r]=i;
if(f<=r)
{
visited[q[f]]=1;
bfs(q[f++]);
}
}
void main()
{
int v;
clrscr();
printf("\n Enter the number of vertices:");
scanf("%d",&n);
for (i=1;i<=n;i++)
{
q[i]=0;
visited[i]=0;
}
D.RAGUL SINGH(1806037)

printf("\n Enter graph data in matrix form:\n");


for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
scanf("%d",&a[i][j]);
printf("\n Enter the starting vertex:");
scanf("%d",&v);
bfs(v);
printf("\n The node which are reachable are:\n");
for (i=1;i<=n;i++)
if(visited[i])
printf("%d\t",i);
else
printf("\n Bfs is not possible");
getch();
}

OUTPUT :
Enter the number of vertices: 2
Enter graph data in matrix form: 1 2 2 3
Enter the starting vertex: 1
The node which are reachable are: 1 2
D.RAGUL SINGH(1806037)

S.NO NOMENCLATURE MAX MARKS


MARKS OBTAINED
1 Pre viva 5
2 Pre lab preparation 6
3 In lab performance 7
4 Post viva 2
5 Total 20
D.RAGUL SINGH(1806037)

RESULT :
The program to perform Breadth-wise search has been successfully
implemented and the output is verified .

You might also like