0% found this document useful (0 votes)
7 views7 pages

Practical 8 to 10

The document outlines practical exercises involving graph traversal algorithms, specifically Breadth-First Search (BFS) and Depth-First Search (DFS), along with the Tower of Hanoi problem. It provides theoretical explanations, program codes, and expected outputs for each algorithm. The results demonstrate the implementation of these algorithms to print all reachable nodes from a starting node in a directed graph and to solve the Tower of Hanoi puzzle using recursion.
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)
7 views7 pages

Practical 8 to 10

The document outlines practical exercises involving graph traversal algorithms, specifically Breadth-First Search (BFS) and Depth-First Search (DFS), along with the Tower of Hanoi problem. It provides theoretical explanations, program codes, and expected outputs for each algorithm. The results demonstrate the implementation of these algorithms to print all reachable nodes from a starting node in a directed graph and to solve the Tower of Hanoi puzzle using recursion.
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/ 7

PRACTICAL NO: 8

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

Enter graph data in matrix form:


011000
100110
000010
010011
011101
000110

Enter the starting vertex: 1

The node which are reachable are:


1 2 3 4 5 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:

Enter number of vertices: 6


Enter the adjacency matrix:
011000
100110
000010
010011
011101
000110

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 −

 First, we move the smaller (top) disk to aux peg.


 Then, we move the larger (bottom) disk to destination peg.
 And finally, we move the smaller disk from aux to destination peg.
So now, we are in a position to design an algorithm for Tower of Hanoi with more than two
disks. We divide the stack of disks in two parts. The largest disk (nth disk) is in one part and all
other (n-1) disks are in the second part.
Our ultimate aim is to move disk n from source to destination and then put all other (n1) disks
onto it. We can imagine to apply the same in a recursive way for all given set of disks.

The steps to follow are −


Step 1 − Move n-1 disks from source to aux
Step 2 − Move nth disk from source to dest
Step 3 − Move n-1 disks from aux to dest

Program Code:

#include <stdio.h>

void towers(int, char, char, char);

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;
}

void towers(int num, char frompeg, char topeg, char auxpeg)


{
if (num == 1)
{
printf("\n Move disk 1 from peg %c to peg %c", frompeg, topeg);
return;
}
towers(num - 1, frompeg, auxpeg, topeg);
printf("\n Move disk %d from peg %c to peg %c", num, frompeg, topeg);
towers(num - 1, auxpeg, topeg, frompeg);
}

Output:
Enter the number of disks: 3
The sequence of moves involved in the Tower of Hanoi are:

Move disk 1 from peg A to peg C


Move disk 2 from peg A to peg B
Move disk 1 from peg C to peg B
Move disk 3 from peg A to peg C
Move disk 1 from peg B to peg A
Move disk 2 from peg B to peg C
Move disk 1 from peg A to peg C

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

You might also like