answersLogoWhite

0

#include<stdio.h>

#include<conio.h>

int a[20][20],q[20],visited[20],n,i,j,f=0,r=-1;

void bfs(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;

printf("\n Enter the number of vertices:");

scanf("%d",&n);

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

{

q[i]=0;

visited[i]=0;

}

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();

}

User Avatar

Wiki User

12y ago

What else can I help you with?

Related Questions

Who is faster between dfs and bfs?

dfs better then from bfs..


What is the population of BFS Group Ltd?

The population of BFS Group Ltd is 4,200.


Can anyone be jb bfs?

No. why


Difference between DFS and BFS?

1. bfs uses queue implementation ie.FIFO dfs uses stack implementation ie. LIFO 2. dfs is faster than bfs 3. dfs requires less memory than bfs 4. dfs are used to perform recursive procedures.


What does BFS mean and how is it used in the context of computer science and algorithms?

BFS stands for Breadth-First Search. It is a graph traversal algorithm used in computer science to explore and search through the nodes of a graph or tree in a breadthward motion. BFS starts at the root node and explores all the neighboring nodes at the present depth before moving on to the nodes at the next depth level. This algorithm is commonly used to find the shortest path in unweighted graphs and to solve problems like finding connected components or checking for bipartiteness.


What is the bfs workout?

going to the gym,daily.


WHAT IS BFS meetings?

Banking &amp; Financial Sector


Should you capitalize he is the president of a the BFS?

No you don't.


Is BFS recursive?

No, Breadth-First Search (BFS) is not inherently recursive. It is typically implemented using a queue data structure rather than recursion.


Compare and contrast judaism with mesopotamian religions?

bfs


Can apply bfs and dfs on weighted graph?

nop


Why you use DFS and BFS in graphs?

DFS and BFS are both searching algorithms. DFS, or depth first search, is a simple to implement algorithm, especially when written recursively. BFS, or breadth first search, is only slightly more complicated. Both search methods can be used to obtain a spanning tree of the graph, though if I recall correctly, BFS can also be used in a weighted graph to generate a minimum cost spanning tree.