ADA LAB MANUAL
ADA LAB MANUAL
www.brindavancollege.com
IV Semester
ANALYSIS & DESIGN OF ALGORITHMS LAB
BCSL404
ACADEMIC YEAR
2023 – 2024
LABORATORY MANUAL
PREPARED BY
Prof. Padmavathi H G
IV Semester
BCSL404
ACADEMIC YEAR
2023 – 2024
BATCH :
PREPARED BY
Prof. Padmavathi H G
LABORATORY CERTIFICATE
MARKS
Date:
II
DEPARTMENT VISION
DEPARTMENT MISSION
III
DOs
Be on time and students should carry observation and completed records in all aspects.
Dress code & wearing ID card is compulsory.
Electronic gadgets are not allowed inside the lab.
Students should be at their concerned desktop.
After execution the students should get it verified by the concerned faculty.
The executed results should be noted in their observations and get it verified by the
concerned faculty.
Observe good housekeeping practices. Keep the equipment’s in proper place after the
conduction.
Students must ensure that all the switches are in the OFF position; desktop is shutdown
properly after completion of the assignments.
For circuits lab the components must returned properly.
For power electronics lab wearing shoes is compulsory.
DON’Ts
IV
PREFACE
The Analysis and Design of Algorithms lab gives the introduction about the
patterns like divide and conquer, dynamic programming and greedy algorithms. It
also covers major algorithms and data structures for searching and sorting,
graphs, and some optimization techniques. The aim is to provide students a good
knowledge with solid and strong foundations to deal with a wide variety of
Prof. Padmavathi H G
Course Objectives
To learn the methods for analyzing algorithms and evaluating their performance.
To demonstrate the efficiency of algorithms using asymptotic notations.
To solve problems using various algorithm design methods, including brute force,
greedy, divide and conquer, decrease and conquer, transform and conquer, dynamic
programming, backtracking, and branch and bound.
To use modern tool(s) for program development and recording of results /observations.
Laboratory Experiments
4. Design and implement C/C++ Program to find shortest paths from a given
vertex in a weighted connected graph to other vertices using Dijkstra's
algorithm.
VI
VII
Course Outcomes:
VIII
1. Design and implement C/C++ Program to find Minimum Cost Spanning Tree of a given
connected undirected graph using Kruskal's algorithm.
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int i, j, k, a, b, u, v, n, ne=1;
int min, mincost=0, adj[20][20], parent[20];
int find(int i)
{
while(partne[i])
i=parent[i];
return i;
}
int uni(int i,int j)
{
if(i<=j)
{
parent([j]=1);
return 1;
}
return 0;
}
void main()
{
printf(“\n Enter no. of vertices:”);
scanf(“%d”, &n);
printf(“\n Enter the cost adjacency matrix:\n”);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf(“%d”, &adj[i][j]);
if(adj[i][j]==0)
adj[i][j]=999;
}
}
printf(“ The edges of minimum cost spanning tree are\n”);
while(ne<n)
{
for(i=1,min=999;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(adj[i][j]<min)
{
min=adj[i][j];
a=u=i;
b=v=j;
}
}
}
u=find(u);
v=find(v);
if(uni(u,v))
{
printf(“%d edge (%d,%d)=%d\n”, ne++,a ,b, min);
mincost+=min;
}
adj[a][b]=adj[b][a]=999;
}
printf(“\n\n Minimum Cost=%d\n”, mincost);
}
OUTPUT:
Enter the no. of vertices:
5
Enter the cost adjacency matrix:
0 10 0 30 100
10 0 50 0 0
0 50 0 20 10
30 0 20 0 60
100 0 10 60 0
The edges of Minimum Cost Spanning tree are
1) (1,2) = 10
2) (3,5) = 10
3) (3,4) = 20
4) (1,4) = 30
Minimum Cost = 70
2. Design and implement C/C++ Program to find Minimum Cost Spanning Tree of a given
connected undirected graph using Prim's algorithm.
#include<stdio.h>
#include<conio.h>
int i,j,sum=0,visited[10];
int distance[10],vertex[10];
int min,u,v;
for(i=1;i<=n;i++)
vertex[i]=source;
visited[i]=0;
distance[i]=cost[source][i];
visited[source]=1;
for(i=1;i<n;i++)
min=INFINITY;
for(j=1;j<=n;j++)
if(!visited[j]&&distance[j]<min)
min=distance[j];
u=j;
visited[u]=1;
sum=sum+distance[u];
printf("\n%d->%d",vertex[u],u);
for(v=1;v<=n;v++)
if(!visited[v]&&cost[u][v]<distance[v])
distance[v]=cost[u][v];
vertex[v]=u;
return sum;
void main()
int a[10][10],n,i,j,m,source;
clrscr();
scanf("%d",&n);
printf("\n enter the cost matrix:\n 0-self loop and 999-no edge:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
scanf("%d",&source);
m=prim(a,source,n);
getch();
OUTPUT:
Enter the cost matrix 0-for self edge and 999-if no edge
0 3 4 999 5
3 0 999 6 1
4 999 0 9 7
999 6 9 0 2
5 1 7 2 0
2->5
5->4
2->1
1->3
Cost = 10
3. a. Design and implement C/C++ Program to solve All-Pairs Shortest Paths problem using
Floyd's algorithm.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,a[20][20],n;
clrscr();
printf("\nEnter the value of n ");
scanf("%d",&n);
printf("\nEnter the adjacency matrix ");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
for(k=0;k<n;k++)
{
for(j=0;j<n;j++)
{
for(i=0;i<n;i++)
{
a[i][j]=a[i][j]<(a[i][k]+a[k][j])?a[i][j]:(a[i][k]+a[k][j]);
}
}
}
printf("\nFloyd's shortest path is ");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%c%d",j==0?'\n':' ',a[i][j]);
}
}
getch();
}
OUTPUT:
Enter the value of n: 5
b. Design and implement C/C++ Program to find the transitive closure using Warshal's
algorithm.
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[20][20],i,j,k,n;
clrscr();
printf("\nEnter the number of nodes ");
scanf("%d",&n);
printf("\nEnter the adjacency matrix ");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&arr[i][j]);
}
}
for(k=0;k<n;k++)
{
for(j=0;j<n;j++)
{
for(i=0;i<n;i++)
{
arr[i][j]=arr[i][j]||(arr[i][k]&&arr[k][j]);
}
}
}
printf("\nThe transitive closure formed by Warshalls algorithm is ");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%d ",arr[i][j]);
}
}
getch();
}
OUTPUT:
4. Design and implement C/C++ Program to find shortest paths from a given vertex in a weighted
connected graph to other vertices using Dijkstra's algorithm.
#include<stdio.h>
#include<conio.h>
int visited[10],min,u;
int i,j;
for(i=1;i<=n;i++)
distance[i]=cost[source][i];
visited[i]=0;
visited[source]=1;
for(i=1;i<=n;i++)
min=INFINITY;
for(j=1;j<=n;j++)
min=distance[j];
u=j;
visited[u]=1;
for(j=1;j<=n;j++)
distance[j]=distance[u]+cost[u][j];
void main()
{
int n,cost[10][10],distance[10];
int i,j,source,sum;
clrscr();
scanf("%d",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&cost[i][j]);
scanf("%d",&source);
dijkstra(cost,n,source,distance);
for(i=1;i<=n;i++)
getch();
OUTPUT:
Cost Matrix
999 999 4 7
999 4 999 15
999 7 15 999
5. Design and implement C/C++ Program to obtain the Topological ordering of vertices in a given
digraph.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20][20],rem[20],ind,n,i,j,flag=0,t=0;
clrscr();
printf("\nEnter the value of n ");
scanf("%d",&n);
printf("\nEnter the adjacency matrix ");
for(i=0;i<n;i++)
{
rem[i]=0;
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
while(flag==0)
{
flag=1;
for(i=0;i<n;i++)
{
if(rem[i]==0)
{
ind=0;
for(j=0;j<n;j++)
{
if(!(rem[j]==1||a[j][i]==0))
{
ind=1;
break;
}
}
if(ind==0)
{
printf("%s",t==0?"\nTopological ordering is ":"");
rem[i]=1;
printf("%d ",i+1);
flag=0;
t++;
break;
}
}
}
}
if(t!=n)
{
printf("\nTopological ordering is not possible(it can only be partially ordered)!!");
}
getch();
}
OUTPUT:
Topological Ordering is 1 3 2 4 5
6. Design and implement C/C++ Program to solve 0/1 Knapsack problem using Dynamic
Programming method.
#include<stdio.h>
#include<conio.h>
int w[10],p[10],n;
return a>b?a:b;
return max(knap(i+1,m),knap(i+1,m-w[i])+p[i]);
void main()
int m,i,max_profit;
clrscr();
scanf("%d",&n);
scanf("%d",&m);
for(i=1;i<=n;i++)
scanf("%d%d",&p[i],&w[i]);
max_profit=knap(1,m);
getch();
OUTPUT:
100 12
12 15
20 30
Max profit=132
7. Design and implement C/C++ Program to solve discrete Knapsack and continuous
Knapsack problems using greedy approximation method.
#include <stdio.h>
#define MAX 50
int p[MAX], w[MAX], x[MAX];
double maxprofit;
int n, m, i;
void greedyKnapsack(int n, int w[], int p[], int m)
{
double ratio[MAX];
// Calculate the ratio of profit to weight for each item
for (i = 0; i < n; i++)
{
ratio[i] = (double)p[i] / w[i];
}
// Sort items based on the ratio in non-increasing order
for (i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (ratio[i] < ratio[j]) {
double temp = ratio[i];
ratio[i] = ratio[j];
ratio[j] = temp;
int temp2 = w[i];
w[i] = w[j];
w[j] = temp2;
temp2 = p[i];
p[i] = p[j];
p[j] = temp2;
}
}
}
int currentWeight = 0;
maxprofit = 0.0;
// Fill the knapsack with items
for (i = 0; i < n; i++) {
if (currentWeight + w[i] <= m) {
x[i] = 1; // Item i is selected
currentWeight += w[i];
maxprofit += p[i];
}
else {
// Fractional part of item i is selected
x[i] = (m - currentWeight) / (double)w[i];
maxprofit += x[i] * p[i];
break;
}
}
printf("Optimal solution for greedy method: %.1f\n", maxprofit);
printf("Solution vector for greedy method: ");
for (i = 0; i < n; i++)
printf("%d\t", x[i]);
}
int main() {
printf("Enter the number of objects: ");
scanf("%d", &n);
printf("Enter the objects' weights: ");
for (i = 0; i < n; i++)
scanf("%d", &w[i]);
printf("Enter the objects' profits: ");
for (i = 0; i < n; i++)
scanf("%d", &p[i]);
printf("Enter the maximum capacity: ");
scanf("%d", &m);
greedyKnapsack(n, w, p, m);
return 0;
}
OUTPUT:
Enter the no. of objects:4
Enter the object’s weights: 2 3 4 5
Enter the object’s profits: 1 2 5 6
Enter the maximum capacity: 8
Optimal solution for greedy method: 5
Solution vector for greedy method: 1 0 0 0
8. Design and implement C/C++ Program to find a subset of a given set S = {sl , s2,.....,sn} of n
positive integers whose sum is equal to a given positive integer d.
#include<stdio.h>
#include<conio.h>
#define MAX 10
int s[MAX],x[MAX],d;
void sumofsub(int p,int k,int r)
{
int i;
x[k]=1;
if((p+s[k])==d)
{
for(i=1;i<=k;i++) if(x[i]==1)
printf("%d ",s[i]);
printf("\n");
}
else
if(p+s[k]+s[k+1]<=d)
sumofsub(p+s[k],k+1,r-s[k]);
if((p+r-s[k]>=d) && (p+s[k+1]<=d))
{
x[k]=0;
sumofsub(p,k+1,r-s[k]);
}
}
int main()
{
int i,n,sum=0;
printf("\n Enter the n value:");
scanf("%d",&n);
printf("\n Enter the set in increasing order:");
for(i=1;i<=n;i++)
scanf("%d",&s[i]);
sumofsub(0,1,sum);
return 0;
}
OUTPUT:
9. Design and implement C/C++ Program to sort a given set of n integer elements using
Selection Sort method and compute its time complexity. Run the program for varied values
of n> 5000 and record the time taken to sort. Plot a graph of the time taken versus n. The
elements can be read from a file or can be generated using the random number generator.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Function to perform selection sort
void selectionSort(int arr[], int n) {
int i, j, min_idx;
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int *arr = (int *)malloc(n * sizeof(int));
printf("Enter %d elements: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
Dept. of AIML, BrCE 20
selectionSort(arr, n);
printf("\n");
free(arr);
return 0;
OUTPUT:
Enter the value of n: 5
Enter the 5 elements: 34 25 90 12 45
Sorted array: 12 25 34 45 90
Time taken for sorting: 0.000001 seconds
10. Design and implement C/C++ Program to sort a given set of n integer elements using Quick
Sort method and compute its time complexity. Run the program for varied values of n> 5000
and record the time taken to sort. Plot a graph of the time taken versus n. The elements can
be read from a file or can be generated using the random number generator.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
quickSort(arr, 0, n - 1);
return 0;
}
OUTPUT:
Enter the number of elements: 6
Enter 6 elements: 5 23 7 1 56
Sorted array: 1 5 7 23 56
Time taken for sorting is: 0.000002 seconds
11. Design and implement C/C++ Program to sort a given set of n integer elements using Merge
Sort method and compute its time complexity. Run the program for varied values of n> 5000,
and record the time taken to sort. Plot a graph of the time taken versus n. The elements can
be read from a file or can be generated using the random number generator.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d integers: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
OUTPUT:
12. Design and implement C/C++ Program for N Queen's problem using Backtracking.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void printSolution(int x[], int n)
{
for (int i = 0; i < n; i++)
{
printf("\n");
for (int j = 0; j < n; j++)
{
if (x[i] == j)
{
printf("Q ");
}
else
{
printf("X ");
}
}
}
printf("\n");
}
{
*count += 1;
printSolution(x, n);
return;
}
for (int col = 0; col < n; col++)
{
if (isValid(x, row, col))
{
x[row] = col;
solveNQueens(n, x, row + 1, count);
}
}
}
int main()
{
int n;
printf("Enter the total number of queens: ");
scanf("%d", &n);
if (n <= 0)
{
printf("Invalid input!\n");
return 1;
}
int x[n];
int count = 0;
solveNQueens(n, x, 0, &count);
if (count == 0) {
printf("No solution found!\n");
} else {
printf("\n The total number of solutions is %d.\n", count);
}
return 0;
}
OUTPUT:
X Q X X
X X X Q
Q X X X
X X Q X
X X Q X
Q X X X
X X X Q
X Q X X