DAA Lab Manual Final
DAA Lab Manual Final
DAA Lab Manual Final
PROGRAM 1:
Deign and implement C/C++ Program to find Minimum Cost Spanning Tree of a given
undirected graph using Kruskal'salgorithm.
CODE
#include<stdio.h>
int ne=1,min_cost=0;
void main()
{
int n,i,j,min,a,u,b,v,cost[20][20],parent[20];
printf("Enter the no. of vertices:");
scanf("%d",&n);
printf("\nEnter the cost matrix:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&cost[i][j]);
for(i=1;i<=n;i++)
parent[i]=0;
printf("\nThe edges of spanning tree are\n");
while(ne<n)
{
min=999;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(cost[i][j]<min)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
}
}
while(parent[u])
u=parent[u];
while(parent[v])
v=parent[v];
if(u!=v)
{
printf("Edge %d\t(%d->%d)=%d\n",ne++,a,b,min);
min_cost=min_cost+min;
parent[v]=u;
}
cost[a][b]=cost[a][b]=999;
}
Analysis & Design of Algorithms 2023
printf("\nMinimum cost=%d\n",min_cost);
}
OUTPUT:
Analysis & Design of Algorithms 2023
PROGRAM 2:
Design and implement C/C++ Program to find Minimum Cost Spanning Tree of a given
connected undirected graph using Prim's algorithm.
CODE :
#include<stdio.h>
int ne=1,min_cost=0;
void main()
{
int n,i,j,min,cost[20][20],a,u,b,v,source,visited[20];
printf("Enter the no. of nodes:");
scanf("%d",&n);
printf("Enter the cost matrix:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
}
}
for(i=1;i<=n;i++)
visited[i]=0;
printf("Enter the root node:");
scanf("%d",&source);
visited[source]=1;
if(visited[u]==0||visited[v]==0)
{
printf("\nEdge %d\t(%d>%d)=%d\n",ne++,a,b,min);
min_cost=min_cost+min;
visited[b]=1;
}
cost[a][b]=cost[b][a]=999;
}
printf("\nMinimum cost=%d\n",min_cost);
}
OUTPUT:
Analysis & Design of Algorithms 2023-24
PROGRAM 3:
a. Design and implement C/C++ Program to solve All-Pairs Shortest Paths problem
using Floyd's algorithm.
CODE:
#include<stdio.h>
#define INF 999
int min(int a,int b)
{
return(a<b)?a:b;
}
void floyd(int p[][10],int n)
{
int i,j,k;
for(k=1; k<=n; k++)
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
p[i][j]=min(p[i][j],p[i][k]+p[k][j]);
}
void main()
{
int a[10][10],n,i,j;
printf("\nEnter the n value:");
scanf("%d",&n);
printf("\nEnter the graph data:\n");
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
scanf("%d",&a[i][j]);
floyd(a,n);
printf("\nShortest path matrix\n");
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
printf("%d ",a[i][j]);
printf("\n");
}
}
Analysis & Design of Algorithms 2023-24
OUTPUT:
Enter the n value:4
Enter the graph data:
0 999 3 999
2 0 999 999
999 7 0 1
6 999 999 0
CODE :
#include<stdio.h>
void warsh(int p[][10],int n)
{
int i,j,k;
for(k=1; k<=n; k++)
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
p[i][j]=p[i][j] || p[i][k] && p[k][j];
}
int main()
{
int a[10][10],n,i,j;
printf("\nEnter the n value:");
scanf("%d",&n);
printf("\nEnter the graph data:\n");
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
scanf("%d",&a[i][j]);
warsh(a,n);
printf("\nResultant path matrix\n");
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
printf("%d ",a[i][j]);
printf("\n");
}
return 0;
}
OUTPUT:
Enter the n value:4
OUTPUT:
Enter n value:4
CODE :
#include<stdio.h>
int temp[10],k=0;
void sort(int a[][10],int id[],int n)
{
int i,j;
for(i=1; i<=n; i++)
{
if(id[i]==0)
{
id[i]=-1;
temp[++k]=i;
for(j=1; j<=n; j++)
{
if(a[i][j]==1 && id[j]!=-1)
id[j]--;
}
i=0;
}
}
}
void main()
{
int a[10][10],id[10],n,i,j;
printf("\nEnter the n value:");
scanf("%d",&n);
for(i=1; i<=n; i++)
id[i]=0;
printf("\nEnter the graph data:\n");
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
{
scanf("%d",&a[i][j]);
if(a[i][j]==1)
id[j]++;
}
sort(a,id,n);
if(k!=n)
printf("\nTopological ordering not possible");
else
{
printf("\nTopological ordering is:");
for(i=1; i<=k; i++)
printf("%d ",temp[i]);
}
}
Analysis & Design of Algorithms 2023-24
OUTPUT:
*********************************OUTPUT1***************************
***************************OUTPUT2**********************************
CODE :
#include<stdio.h>
int w[10],p[10],n;
int max(int a,int b)
{
return a>b?a:b;
}
int knap(int i,int m)
{
if(i==n) return w[i]>m?0:p[i];
if(w[i]>m) return knap(i+1,m);
return max(knap(i+1,m),knap(i+1,m-w[i])+p[i]);
}
int main()
{
int m,i,max_profit;
printf("\nEnter the no. of objects:");
scanf("%d",&n);
printf("\nEnter the knapsack capacity:");
scanf("%d",&m);
printf("\nEnter profit followed by weight:\n");
for(i=1; i<=n; i++)
scanf("%d %d",&p[i],&w[i]);
max_profit=knap(1,m);
printf("\nMax profit=%d",max_profit);
return 0;
}
OUTPUT:
Enter the no. of objects:4
Enter the knapsack capacity:5
Enter profit followed by weight:
12 3
43 5
45 2
55 3
Max profit=100
Analysis & Design of Algorithms 2023-24
PROGRAM 7:
Design and implement C/C++ Program to solve discrete Knapsack and continuous
Knapsack problems using greedy approximation method.
CODE :
#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];
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];
Analysis & Design of Algorithms 2023-24
}
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 number of objects: 4
Enter the objects' weights: 56 78 98 78
Enter the objects' profits: 23 45 76 78
Enter the maximum capacity: 100
Optimal solution for greedy method: 78.0
Solution vector for greedy method: 1 0 0 0
Analysis & Design of Algorithms 2023-24
PROGRAM 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.
CODE :
#include<stdio.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("\nEnter the n value:");
scanf("%d",&n);
printf("\nEnter the set in increasing order:");
for(i=1; i<=n; i++)
scanf("%d",&s[i]);
printf("\nEnter the max subset value:");
scanf("%d",&d);
for(i=1; i<=n; i++)
sum=sum+s[i];
if(sum<d || s[1]>d)
printf("\nNo subset possible");
else
sumofsub(0,1,sum);
return 0;
}
Analysis & Design of Algorithms 2023-24
OUTPUT:
Enter the n value:9
CODE :
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
double measureSelectionSortTime(int n) {
int* arr = (int*)malloc(n * sizeof(int));
generateRandomArray(arr, n);
start = clock();
selectionSort(arr, n);
end = clock();
void plotGraph() {
FILE* gnuplotPipe = popen("gnuplot -persistent", "w");
if (gnuplotPipe) {
fprintf(gnuplotPipe, "set title 'Selection Sort Time Complexity'\n");
fprintf(gnuplotPipe, "set xlabel 'Number of elements (n)'\n");
fprintf(gnuplotPipe, "set ylabel 'Time taken (seconds)'\n");
fprintf(gnuplotPipe, "set grid\n");
fprintf(gnuplotPipe, "set term png\n");
fprintf(gnuplotPipe, "set output 'selection_sort_output.png'\n");
fprintf(gnuplotPipe, "plot 'selection_sort_data.txt' with linespoints\n");
pclose(gnuplotPipe);
}
}
void sortUserArray() {
int n,i;
printf("Enter the number of elements: ");
scanf("%d", &n);
selectionSort(arr, n);
printf("Sorted array:\n");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
free(arr);
}
void plotSelectionSortTimeComplexityGraph() {
int i;
int values_of_n[] = {5000, 10000, 15000, 20000, 25000, 30000, 35000, 40000, 45000,
50000};
int num_values = sizeof(values_of_n) / sizeof(values_of_n[0]);
fclose(dataFile);
plotGraph();
}
int main() {
srand(time(0)); // Seed the random number generator
int choice;
do {
printf("Menu:\n");
printf("1. Sort an array using Selection Sort\n");
printf("2. Plot time complexity graph for Selection Sort\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
sortUserArray();
break;
case 2:
plotSelectionSortTimeComplexityGraph();
break;
case 3:
printf("Exiting...\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 3);
return 0;
}
OUTPUT:
sdit@sdit-PC:~/spoorthi$ gcc pg9menu.c -o pg9menu -lm
sdit@sdit-PC:~/spoorthi$ ./pg9menu
Menu:
1. Sort an array using Selection Sort
2. Plot time complexity graph for Selection Sort
3. Exit
Analysis & Design of Algorithms 2023-24
Enter your choice: 1
Enter the number of elements: 5
Enter the elements:
65
215
6225
8455
62
Sorted array:
62 65 215 6225 8455
Menu:
1. Sort an array using Selection Sort
2. Plot time complexity graph for Selection Sort
3. Exit
Enter your choice: 2
Sorted 5000 elements in 0.059882 seconds
Sorted 10000 elements in 0.157748 seconds
Sorted 15000 elements in 0.281038 seconds
Sorted 20000 elements in 0.499068 seconds
Sorted 25000 elements in 0.781287 seconds
Sorted 30000 elements in 1.121721 seconds
Sorted 35000 elements in 1.526079 seconds
Sorted 40000 elements in 1.992634 seconds
Sorted 45000 elements in 2.521635 seconds
Sorted 50000 elements in 3.112383 seconds
Menu:
1. Sort an array using Selection Sort
2. Plot time complexity graph for Selection Sort
3. Exit
Enter your choice: 3
Exiting...
CODE :
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
double measureSortTime(int n) {
int* arr = (int*)malloc(n * sizeof(int));
generateRandomArray(arr, n);
Analysis & Design of Algorithms 2023-24
start = clock();
quickSort(arr, 0, n - 1);
end = clock();
void plotGraph() {
FILE* gnuplotPipe = popen("gnuplot -persistent", "w");
if (gnuplotPipe) {
fprintf(gnuplotPipe, "set title 'Quick Sort Time Complexity'\n");
fprintf(gnuplotPipe, "set xlabel 'Number of elements (n)'\n");
fprintf(gnuplotPipe, "set ylabel 'Time taken (seconds)'\n");
fprintf(gnuplotPipe, "set grid\n");
fprintf(gnuplotPipe, "set term png\n");
fprintf(gnuplotPipe, "set output 'output.png'\n");
fprintf(gnuplotPipe, "plot 'data.txt' with linespoints\n");
pclose(gnuplotPipe);
}
}
void sortUserArray() {
int n,i;
printf("Enter the number of elements: ");
scanf("%d", &n);
quickSort(arr, 0, n - 1);
printf("Sorted array:\n");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
free(arr);
}
void plotTimeComplexityGraph() {
int i;
Analysis & Design of Algorithms 2023-24
int values_of_n[] = { 5500, 10000, 20000, 30000, 40000, 50000};
int num_values = sizeof(values_of_n) / sizeof(values_of_n[0]);
fclose(dataFile);
plotGraph();
}
int main() {
srand(time(0)); // Seed the random number generator
int choice;
do {
printf("Menu:\n");
printf("1. Sort an array\n");
printf("2. Plot time complexity graph\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
sortUserArray();
break;
case 2:
plotTimeComplexityGraph();
break;
case 3:
printf("Exiting...\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 3);
return 0;
}
Analysis & Design of Algorithms 2023-24
OUTPUT:
sdit@sdit-PC:~/spoorthi$ ./pg10menu
Menu:
1. Sort an array
2. Plot time complexity graph
3. Exit
Enter your choice: 1
Enter the number of elements: 5
Enter the elements:
854
854
254488
696555
22555
Sorted array:
854 854 22555 254488 696555
Menu:
1. Sort an array
2. Plot time complexity graph
3. Exit
Enter your choice: 2
Sorted 5500 elements in 0.001411 seconds
Sorted 10000 elements in 0.002827 seconds
Sorted 20000 elements in 0.005744 seconds
Sorted 30000 elements in 0.008422 seconds
Sorted 40000 elements in 0.009561 seconds
Sorted 50000 elements in 0.007711 seconds
Menu:
1. Sort an array
2. Plot time complexity graph
3. Exit
Enter your choice: 3
Exiting...
sdit@sdit-PC:~/spoorthi$
CODE :
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
merge(arr, l, m, r);
}
}
double measureMergeSortTime(int n) {
int* arr = (int*)malloc(n * sizeof(int));
generateRandomArray(arr, n);
start = clock();
mergeSort(arr, 0, n - 1);
end = clock();
void plotGraph() {
FILE* gnuplotPipe = popen("gnuplot -persistent", "w");
if (gnuplotPipe) {
fprintf(gnuplotPipe, "set title 'Merge Sort Time Complexity'\n");
fprintf(gnuplotPipe, "set xlabel 'Number of elements (n)'\n");
fprintf(gnuplotPipe, "set ylabel 'Time taken (seconds)'\n");
fprintf(gnuplotPipe, "set grid\n");
fprintf(gnuplotPipe, "set term png\n");
fprintf(gnuplotPipe, "set output 'merge_sort_output.png'\n");
Analysis & Design of Algorithms 2023-24
fprintf(gnuplotPipe, "plot 'merge_sort_data.txt' with linespoints\n");
pclose(gnuplotPipe);
}
}
void sortUserArray() {
int n,i;
printf("Enter the number of elements: ");
scanf("%d", &n);
mergeSort(arr, 0, n - 1);
printf("Sorted array:\n");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
free(arr);
}
void plotMergeSortTimeComplexityGraph() {
int values_of_n[] = {5000, 10000, 15000, 20000, 25000, 30000, 35000, 40000, 45000,
50000};
int num_values = sizeof(values_of_n) / sizeof(values_of_n[0]);
int i;
fclose(dataFile);
plotGraph();
}
int main() {
Analysis & Design of Algorithms 2023-24
srand(time(0)); // Seed the random number generator
int choice;
do {
printf("Menu:\n");
printf("1. Sort an array using Merge Sort\n");
printf("2. Plot time complexity graph for Merge Sort\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
sortUserArray();
break;
case 2:
plotMergeSortTimeComplexityGraph();
break;
case 3:
printf("Exiting...\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 3);
return 0;
}
OUTPUT:
sdit@sdit-PC:~/spoorthi$ gcc pg11menu.c -o pg11menu -lm
sdit@sdit-PC:~/spoorthi$ ./pg11menu
Menu:
1. Sort an array using Merge Sort
2. Plot time complexity graph for Merge Sort
3. Exit
Enter your choice: 1
Enter the number of elements: 5
Enter the elements:
965
6524
9565
233
525
Sorted array:
233 525 965 6524 9565
Menu:
1. Sort an array using Merge Sort
2. Plot time complexity graph for Merge Sort
3. Exit
Analysis & Design of Algorithms 2023-24
Enter your choice: 2
Sorted 5000 elements in 0.001505 seconds
Sorted 10000 elements in 0.003080 seconds
Sorted 15000 elements in 0.004826 seconds
Sorted 20000 elements in 0.006641 seconds
Sorted 25000 elements in 0.007794 seconds
Sorted 30000 elements in 0.005461 seconds
Sorted 35000 elements in 0.005707 seconds
Sorted 40000 elements in 0.006696 seconds
Sorted 45000 elements in 0.007442 seconds
Sorted 50000 elements in 0.008439 seconds
Menu:
1. Sort an array using Merge Sort
2. Plot time complexity graph for Merge Sort
3. Exit
Enter your choice: 3
Exiting...
CODE :
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
return true;
}
board[i][col] = 0; // BACKTRACK
}
}
return false;
}
bool solveNQ(int N)
{
int i,j;
int **board = (int **)malloc(N * sizeof(int *));
for ( i = 0; i < N; i++)
{
board[i] = (int *)malloc(N * sizeof(int));
for ( j = 0; j < N; j++)
{
board[i][j] = 0;
}
}
if (!solveNQUtil(board, N, 0))
{
printf("Solution does not exist\n");
for ( i = 0; i < N; i++)
{
free(board[i]);
}
free(board);
return false;
}
Analysis & Design of Algorithms 2023-24
printSolution(board, N);
int main()
{
int N;
printf("Enter the number of queens: ");
scanf("%d", &N);
solveNQ(N);
return 0;
}
OUTPUT:
sdit@sdit-PC:~/spoorthi$ cc pg12.c
sdit@sdit-PC:~/spoorthi$ ./a.out
Enter the number of queens: 4
##Q#
Q###
###Q
#Q##
sdit@sdit-PC:~/spoorthi$ ./a.out
Enter the number of queens: 2
Solution does not exist
sdit@sdit-PC:~/spoorthi$