ADA Lab Manual
ADA Lab Manual
DESIGN OF
ALGORITHM
LAB MANUAL
EVEN SEM
2024-25
Sl.No Experiment
s
1 Design and implement C/C++ Program to find Minimum Cost Spanning Tree of a given
connected undirected graph using Kruskal's algorithm.
2 Design and implement C/C++ Program to find Minimum Cost Spanning Tree of a given
connected undirected graph using Prim's algorithm.
3 a. Design and implement C/C++ Program to solve All-Pairs Shortest Paths problem using
Floyd's algorithm.
b.Design and implement C/C++ Program to find the transitive closure using Warshal's
algorithm.
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.
5 Design and implement C/C++ Program to obtain the Topological ordering of vertices in a
given digraph.
6 Design and implement C/C++ Program to solve 0/1 Knapsack problem using Dynamic
Programming method.
7 Design and implement C/C++ Program to solve discrete Knapsack and continuous Knapsack
problems using greedy approximation method.
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.
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.
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.
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.
12 Design and implement C/C++ Program for N Queen's problem using Backtracking.
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<conio.h>
#include<stdlib.h>
int i,j,k,a,b,u,v,n,ne=1;
int min,mincost=0,cost[9][9],parent[9];
int find(int);
int uni(int,int);
void main()
{
clrscr();
printf("\n\n\tImplementation of Kruskal's algorithm\n\n");
printf("\nEnter the no. of vertices\n");
scanf("%d",&n);
printf("\nEnter the cost adjacency matrix\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
}
}
printf("\nThe edges of Minimum Cost Spanning Tree are\n\n");
while(ne<n)
{
for(i=1,min=999;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(cost[i][j]<min)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
}
}
u=find(u);
v=find(v);
if(uni(u,v))
{
printf("\n%d edge (%d,%d) =%d\n",ne++,a,b,min);
mincost +=min;
}
cost[a][b]=cost[b][a]=999;
}
printf("\n\tMinimum cost = %d\n",mincost);
getch();
}
int find(int i)
{
while(parent[i])
i=parent[i];
return i;
}
int uni(int i,int j)
{
if(i!=j)
{
parent[j]=i;
return 1;
}
return 0;
}
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<stdlib.h>
#define inf 99999
#define MAX 10
int G[MAX][MAX] = {
{0, 19, 8},
{21, 0, 13},
{15, 18, 0}
};
int S[MAX][MAX], n;
int prims();
int main(){
int i, j, cost;
n = 3;
cost=prims();
printf("Spanning tree:");
for(i=0; i<n; i++) {
printf("\n");
for(j=0; j<n; j++)
printf("%d\t",S[i][j]);
}
printf("\nMinimum cost = %d", cost);
return 0;
}
int prims(){
int C[MAX][MAX];
int u, v, min_dist, dist[MAX], from[MAX];
int visited[MAX],ne,i,min_cost,j;
3.a.Design
and implement C/C++ Program to solve All-Pairs Shortest Paths
problem using Floyd's algorithm.
#include <iostream
using namespace std;
// defining the number of vertices
#define V 4
#define INF 9999
void printMatrix(int matrix[][V]);
// Implementing floyd warshall algorithm
void floydWarshall(int graph[][V]) {
int matrix[V][V], i, j, k;
for (i = 0; i < V; i++)
for (j = 0; j < V; j++)
matrix[i][j] = graph[i][j];
// Adding vertices individually
for (k = 0; k < V; k++) {
for (i = 0; i < V; i++) {
for (j = 0; j < V; j++) {
if (matrix[i][k] + matrix[k][j] < matrix[i][j])
matrix[i][j] = matrix[i][k] + matrix[k][j];
}
}
}
printMatrix(matrix);
}
void printMatrix(int matrix[][V]) {
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
if(i==j){
continue;
}
else if (matrix[i][j] == INF){
cout<<"no path exist between "<<i<<" and "<<j<<endl;
}
else{
cout<<"shortest path from "<<i<<" to "<<j<<" is "<<matrix[i][j]<<endl;
}
}
}
}
int main() {
int graph[V][V] = {{0,INF,-3,INF},
{5,0,4,INF},
{INF,INF,0,3},
{INF,-2,INF,0}};
floydWarshall(graph);
return 0;
// End of Program
}
3.bDesign and implement C/C++ Program to find the transitive closure using
Warshal's algorithm.
# include <stdio.h>
# include <conio.h>
int n,a[10][10],p[10][10];
void path()
{
int i,j,k;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
p[i][j]=a[i][j];
for(k=0;k<n;k++)
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(p[i][k]==1&&p[k][j]==1) p[i][j]=1;
}
void main()
{
int i,j;
clrscr();
printf("Enter the number of nodes:");
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
path();
printf("\nThe path matrix is showm below\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%d ",p[i][j]);
printf("\n");
}
getch();
}
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 <stdbool.h>
#define INF 99999
#define V 5 // Number of vertices
// A function to find the vertex with the minimum distance value, from the set of
vertices not yet included in shortest path tree
int minDistance(int dist[], bool sptSet[]) {
int min = INF, min_index;
return min_index;
}
dist[src] = 0;
printSolution(dist);
}
// Driver code
int main() {
int graph[V][V] = {{0, 9, 6, 5, 3},
{9, 0, 0, 0, 0},
{6, 0, 0, 0, 0},
{5, 0, 0, 0, 0},
{3, 0, 0, 0, 0}};
dijkstra(graph, 0);
return 0;
}
#include<stdio.h>
int a[10][10],n,indegre[10];
void find_indegre()
{ int j,i,sum;
for(j=0;j<n;j++)
{
sum=0;
for(i=0;i<n;i++)
sum+=a[i][j];
indegre[j]=sum;
}
}
void topology()
{
int i,u,v,t[10],s[10],top=-1,k=0;
find_indegre();
for(i=0;i<n;i++)
{
if(indegre[i]==0) s[++top]=i;
}
while(top!=-1)
{
u=s[top--];
t[k++]=u;
for(v=0;v<n;v++)
{
if(a[u][v]==1)
{
indegre[v]--;
if(indegre[v]==0) s[++top]=v;
}
}
}
printf("The topological Sequence is:\n");
for(i=0;i<n;i++)
printf("%d ",t[i]);
}
void main()
{
int i,j;
clrscr();
printf("Enter number of jobs:");
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
topology();
}
#include <stdio.h>
#define MAX 10
int a[MAX][MAX], n, indegre[MAX];
// Function to calculate in-degrees of all vertices
void find_indegre() {
int i, j, sum;
for (j = 0; j < n; j++) {
sum = 0;
for (i = 0; i < n; i++) {
sum += a[i][j];
}
indegre[j] = sum;
}
}
int main() {
int i, j;
// Input the number of jobs (vertices)
printf("Enter number of jobs: ");
scanf("%d", &n);
// Input the adjacency matrix
printf("\nEnter the adjacency matrix:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &a[i][j]);
}
}
// Perform topological sorting
topology();
return 0;
}
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],v[10][10],n,i,j,cap,x[10]={0};
int max(int i,int j)
{
return ((i>j)?i:j);
}
int knap(int i,int j)
{
int value;
if(v[i][j]<0)
{
if(j<w[i])
value=knap(i-1,j);
else
value=max(knap(i-1,j),p[i]+knap(i-1,j-w[i]));
v[i][j]=value;
}
return(v[i][j]);
}
void main()
{
int profit,count=0;
clrscr();
printf("\nEnter the number of elements\n");
scanf("%d",&n);
printf("Enter the profit and weights of the elements\n");
for(i=1;i<=n;i++)
{
printf("For item no %d\n",i);
scanf("%d%d",&p[i],&w[i]);
}
printf("\nEnter the capacity \n");
scanf("%d",&cap);
for(i=0;i<=n;i++)
for(j=0;j<=cap;j++)
if((i==0)||(j==0))
v[i][j]=0;
else
v[i][j]=-1;
profit=knap(n,cap);
i=n;
j=cap;
while(j!=0&&i!=0)
{
if(v[i][j]!=v[i-1][j])
{
x[i]=1;
j=j-w[i];
i--;
}
else
i--;
}
printf("Items included are\n");
printf("Sl.no\tweight\tprofit\n");
for(i=1;i<=n;i++)
if(x[i])
printf("%d\t%d\t%d\n",++count,w[i],p[i]);
printf("Total profit = %d\n",profit);
getch();
for(i=0;i<n;i++)
ratio[i]=profit[i]/weight[i];
temp = weight[j];
weight[j] = weight[i];
weight[i] = temp;
temp = profit[j];
profit[j] = profit[i];
profit[i] = temp;
}
printf("Knapsack problems using Greedy Algorithm:\n");
for (i = 0; i < n; i++)
{
if (weight[i] > capacity)
break;
else
{
Totalvalue = Totalvalue + profit[i];
capacity = capacity - weight[i];
}
}
if (i < n)
Totalvalue = Totalvalue + (ratio[i]*capacity);
printf("\nThe maximum value is :%f\n",Totalvalue);
return 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>
int n = 5;
int p[10] = {3, 3, 2, 5, 1};
int w[10] = {10, 15, 10, 12, 8};
int W = 10;
int main(){
int cur_w;
float tot_v;
int i, maxi;
int used[10];
for (i = 0; i < n; ++i)
used[i] = 0;
cur_w = W;
while (cur_w > 0) {
maxi = -1;
for (i = 0; i < n; ++i)
if ((used[i] == 0) &&
((maxi == -1) || ((float)w[i]/p[i] > (float)w[maxi]/p[maxi])))
maxi = i;
used[maxi] = 1;
cur_w -= p[maxi];
tot_v += w[maxi];
if (cur_w >= 0)
printf("Added object %d (%d, %d) completely in the bag. Space left: %d.\n",
maxi + 1, w[maxi], p[maxi], cur_w);
else {
printf("Added %d%% (%d, %d) of object %d in the bag.\n", (int)((1 +
(float)cur_w/p[maxi]) * 100), w[maxi], p[maxi], maxi + 1);
tot_v -= w[maxi];
tot_v += (1 + (float)cur_w/p[maxi]) * w[maxi];
}
}
printf("Filled the bag with objects worth %.2f.\n", tot_v);
return 0;
}
#include<stdio.h>
# include <time.h>
int main(){
int a[10000],i;
int j,temp,num;
clock_t st,et;
printf("Enter the number to give\n");
scanf("%d",&num);
}
printf(“Array before sorting”);
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
st=clock();
for(i=0; i<num-1; i++){
for(j=i+1;j<num; j++){
if(a[i]>a[j]){
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
et=clock();
printf("Selection Sort in C\n");
for(i=0; i<num; i++){
printf("a[%d]=\t%d\n",i,a[i]);
}
printf(“Time taken :%f”,(et-st)/CLK_TCK);
return 0;
}
# include <stdio.h>
# include <conio.h>
# include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
FILE *fp;
fp = fopen("time_complexity_data.txt", "w");
if (fp == NULL) {
printf("Error opening file!\n");
exit(1);
}
int n_values[] = {5000, 10000, 15000, 20000}; // Change or add more values as needed
int num_values = sizeof(n_values) / sizeof(n_values[0]);
fclose(fp);
return 0;
}
#include <stdio.h>
#include <stdbool.h>
return true;
}
// Consider this column and try placing this queen in all rows one by one
for (int i = 0; i < N; i++) {
// Check if the queen can be placed on board[i][col]
if (isSafe(board, i, col)) {
// Place this queen in board[i][col]
board[i][col] = 1;
// If the queen cannot be placed in any row in this column col, then return false
return false;
}
if (solveNQUtil(board, 0) == false) {
printf("Solution does not exist");
return false;
}
printSolution(board);
return true;
}
int main() {
solveNQ();
return 0;
}
ADDITIONAL PROGRAMS
1.Find the binomial co-efficient using dynamic programming.
#include<stdio.h>
#include<conio.h>
long int bin(int n,int k)
{
int i,j;
long int arr[20][20];
for(i=0;i<=n;i++)
{
for(j=0;j<=(k<i?k:i);j++)
{
if(i==j||j==0)
{
arr[i][j]=1;
}
else
{
arr[i][j]=arr[i-1][j]+arr[i-1][j-1];
}
}
}
return(arr[n][k]);
}
void main()
{
int n,k;
clrscr();
printf("\nEnter the value of n ");
scanf("%d",&n);
printf("\nEnter the value of k ");
scanf("%d",&k);
if(n<0||k<0||k>n)
{
printf("\nValue cannot be calculated!!");
}
else
{
printf("\nThe binomial coefficient is %ld.",bin(n,k));
}
getch();
}
2.Find the minimum cost spanning tree of a given undirected graph using Prim's algorithm.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20][20],add[20],f[20][20],min,posx,posy,i,j,k,n,tot,flag=0,weight=0;
clrscr();
tot=0;
printf("\nEnter the value of n ");
scanf("%d",&n);
printf("\nEnter the adjacency matrix ");
for(i=0;i<n;i++)
{
add[i]=0;
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
f[i][j]=0;
}
}
add[0]=1;
while(flag==0)
{
flag=1;
min=9999;
for(i=0;i<n;i++)
{
if(add[i]==1)
{
for(j=0;j<n;j++)
{
if(add[j]==0&&a[i][j]!=0&&a[i][j]<min)
{
min=a[i][j];
posx=i;
posy=j;
}
}
}
}
if(min!=9999)
{
f[posx][posy]=min;
f[posy][posx]=min;
weight=weight+f[posx][posy];
tot++;
flag=0;
add[posx]=1;
add[posy]=1;
}
}
if(tot!=n-1)
{
printf("\nMinimum spannin does not exist!!");
}
else
{
printf("\nMinimum spanning tree with weight=%d is ",weight);
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%d ",f[i][j]);
}
}
}
getch();
}