Practical 8 to 10
Practical 8 to 10
Aim: Explain Breadth-First search and implement BFS to print all the nodes reachable from a
given starting node in a diagraph.
Resources:
Software Required: Turbo C++.
Hardware Required: Computer System.
Theory:-
Breadth-first search is a graph traversal algorithm that starts traversing the graph from the root
node and explores all the neighboring nodes. Then, it selects the nearest node and explores all the
unexplored nodes. While using BFS for traversal, any node in the graph can be considered as the
root node. There are many ways to traverse the graph, but among them, BFS is the most
commonly used approach. It is a recursive algorithm to search all the vertices of a tree or graph
data structure. BFS puts every vertex of the graph into two categories - visited and non-visited. It
selects a single node in a graph and, after that, visits all the nodes adjacent to the selected node.
The only catch here is, unlike trees, graphs may contain cycles, so we may come to the same
node again. To avoid processing a node more than once, we use a Boolean visited array. For
simplicity, it is assumed that all vertices are reachable from the starting vertex.
Program Code:
#include<stdio.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");
}
Output:
Enter the number of vertices: 6
Results/Conclusion: Thus, we have implemented BFS to print all the nodes reachable from a
given starting node in a diagraph.
2
DEPT.: CSE/VI Sem /DAA
PRACTICAL NO: 9
Aim: Develop a program to print all the nodes reachable from a given starting node in a
diagraph using Depth First Search.
Resources:
Software Required: Turbo C++.
Hardware Required: Computer System.
Theory: Depth-first search is an algorithm for traversing or searching tree or graph data
structures. The algorithm starts at the root node (selecting some arbitrary node as the root node
in the case of a graph) and explores as far as possible along each branch before backtracking.
So the basic idea is to start from the root or any arbitrary node and mark the node and move to
the adjacent unmarked node and continue this loop until there is no unmarked adjacent node.
Then backtrack and check for other unmarked nodes and traverse them. Finally, print the nodes
in the path.
Algorithm:
Create a recursive function that takes the index of the node and a visited array.
1. Mark the current node as visited and print the node.
2. Traverse all the adjacent and unmarked nodes and call the recursive function with the index
of the adjacent node.
Program Code:
#include<stdio.h>
int a[20][20],reach[20],n;
void dfs(int v)
{
int i;
reach[v]=1;
for (i=1;i<=n;i++)
if(a[v][i] && !reach[i])
{
printf("\n %d->%d",v,i);
dfs(i);
}
}
void main()
{
int i,j,count=0;
printf("\n Enter number of vertices:");
scanf("%d",&n);
for (i=1;i<=n;i++)
3
DEPT.: CSE/VI Sem /DAA
{
reach[i]=0;
for (j=1;j<=n;j++)
a[i][j]=0;
}
printf("\n Enter the adjacency matrix:\n");
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
scanf("%d",&a[i][j]);
dfs(1);
printf("\n");
for(i=1;i<=n;i++)
{
if(reach[i])
count++;
}
if(count==n)
printf("\n Graph is connected");
else
printf("\n Graph is not connected");
}
Output:
1->2
2->4
4->5
5->3
5->6
Graph is connected
Results/Conclusion: Thus, we have developed a program to print all the nodes reachable
from a given starting node in a diagraph using Depth First Search.
4
DEPT.: CSE/VI Sem /DAA
PRACTICAL NO: 10
Aim: Study an algorithm Tower of Hanoi where the aim is to move the entire stack to another
rod for n=3 and understand the concept of recursion.
Resources:
Software Required: Turbo C++.
Hardware Required: Computer System.
Theory: - Tower of Hanoi puzzle with n disks can be solved in minimum 2n−1 steps. This
presentation shows that a puzzle with 3 disks has taken 23 - 1 = 7 steps.
Algorithm
To write an algorithm for Tower of Hanoi, first we need to learn how to solve this problem with
lesser amount of disks, say → 1 or 2. We mark three towers with
name, source, destination and aux (only to help moving the disks). If we have only one disk,
then it can easily be moved from source to destination peg.
If we have 2 disks −
Program Code:
#include <stdio.h>
int main()
{
int num;
5
DEPT.: CSE/VI Sem /DAA
printf ("Enter the number of disks : ");
scanf ("%d", &num);
printf ("The sequence of moves involved in the Tower of Hanoi are :\n");
towers (num, 'A', 'C', 'B');
return 0;
}
Output:
Enter the number of disks: 3
The sequence of moves involved in the Tower of Hanoi are:
Results/Conclusion: Thus, we have studied an algorithm Tower of Hanoi where the aim is to
move the entire stack to another rod for n=3 and also understand the concept of recursion.
6
DEPT.: CSE/VI Sem /DAA
7
DEPT.: CSE/VI Sem /DAA