Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
22 views3 pages

Daa PGM2

The document outlines an algorithm for performing a breadth-first search (BFS) on a directed graph to find all nodes reachable from a given source node. It includes a C++ implementation that uses an adjacency matrix to represent the graph and a queue for traversal. The program prompts the user for the number of nodes, the adjacency matrix, and the source node, then outputs the reachable nodes from the source.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views3 pages

Daa PGM2

The document outlines an algorithm for performing a breadth-first search (BFS) on a directed graph to find all nodes reachable from a given source node. It includes a C++ implementation that uses an adjacency matrix to represent the graph and a queue for traversal. The program prompts the user for the number of nodes, the adjacency matrix, and the source node, then outputs the reachable nodes from the source.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

2.

Print all the nodes reachable from a given starting node in a digraph using BFS
method.

ALGORITHM bfs_traversal( a[][],n,source)


// find nodes reachable from given source node
//Input Adjacency matrix ,number of nodes ,source node
//Output: list of nodes reachable from source
//Set status of n nodes as visited

For i=0 to n-1


S[i]=0

//Initialise Empty queue q


f=r=0
q[r]=source// insert source into q
s[source]=1// source is visited
while (f<=r) // As long as Q is not Empty
{
u=q[f++]; // remove first element from Q and update f pter
//Find the nodes v which are adjacent to u and not yet visited
for ( v=0;v<n;v++)
{
if (a[u][v]==1 && s[v]==0)
{
s[v]=1; // Update status of v as visited
q[++r]=v;//Insert new vertex into Q for exploration
}
}
}

1
2. Print all the nodes reachable from a given starting node in a digraph using BFS
method.

//2 REACHABLE NODES USING BFS

#include <iostream> using namespace std; #define MAX 10


int s[MAX]; //Holds the info of what nodes are reachable

void bfs_traversal(int a[MAX][MAX],int n,int source)


{
int q[MAX],f,r; //for Q operation
int u,v; //Represent 2 vertices
int i;//index variable

for (i=0;i<n;i++)
s[i]=0;// No node is visited in the beginning
f=r=0; //Q is Empty
q[r]=source; //Insert source vertex into Q
s[source]=1; // Add source to S
while (f<=r) // As long as Q is not Empty
{
u=q[f++];
for ( v=0;v<n;v++)
{
//Find the nodes v which are adjacent to u
if (a[u][v]==1 && s[v]==0)
{
s[v]=1; //add v to S to indicate that v is visited
q[++r]=v;//Insert new vertex into Q for exploration
}
}
}
}

int main()
{

int n; //No of nodes


int a[MAX][MAX]; //Adjacency matrix int source;
cout<<"Enter Number of nodes"<<endl;
cin>>n;
cout<<"Enter Adjacency Matrix"<<endl;

2
for (int i=0;i<n;i++)

{ for (int j=0;j<n;j++)


{
cin>>a[i][j];
}
cout<<endl;
}
cout<<"Enter the source node :" ;
cin>>source; bfs_traversal(a,n,source);
cout<<"The Nodes Reachable from Source Node"<<source<<endl;
for ( int i=0; i<n;i++)
{
if ( (s[i]==1) && (i!=source)) cout<< "Node: " <<i<<endl;
}
return 0;
}

1
0

3
2

Enter Number of nodes 4


Enter Adjacency Matrix
0110
0010
0001
0100
Enter the source node :3
The Nodes Reachable from Source Node3
Node: 1

Node: 2

You might also like