ADA LAB MANUAL 2022 SCHEME (1)
ADA LAB MANUAL 2022 SCHEME (1)
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>
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()
{
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))
{
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 a,b,u,v,n,i,j,ne=1;
int visited[10]={0},min,mincost=0,cost[10][10];
void main()
{
clrscr();
printf("\n Enter the number of nodes:");
scanf("%d",&n);
printf("\n Enter the 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;
}
visited[1]=1;
printf("\n");
while(ne<n)
{
for(i=1,min=999;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]<min)
if(visited[i]!=0)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
if(visited[u]==0 || visited[v]==0)
{
printf("\n Edge %d:(%d %d) cost:%d",ne++,a,b,min);
mincost+=min;
visited[b]=1;
}
cost[a][b]=cost[b][a]=999;
}
3a. Design and implement C/C++ Program to solve All-Pairs Shortest Paths
problem using Floyd's algorithm.
#include<stdio.h>
#include<conio.h>
int min(int,int);
void floyds(int p[10][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++)
if(i==j)
p[i][j]=0;
else
p[i][j]=min(p[i][j],p[i][k]+p[k][j]);
}
int min(int a,int b)
{
if(a<b)
return(a);
else
return(b);
}
void main()
{
int p[10][10],w,n,e,u,v,i,j;
clrscr();
printf("\nEnter the number of vertices: ");
scanf("%d",&n);
printf("Enter the number of edges: ");
scanf("%d",&e);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
p[i][j]=999;
}
for(i=1;i<=e;i++)
{
printf("Enter the end vertices of edge %d with its weight : ",i);
scanf("%d%d%d",&u,&v,&w);
p[u][v]=w;
}
printf("Matrix of input data\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d\t",p[i][j]);
printf("\n");
}
floyds(p,n);
printf("\nTransitive closure \n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d\t",p[i][j]);
printf("\n");
}
printf("\nThe shortest paths are \n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
if(i!=j)
printf("\n%d,%d>=%d",i,j,p[i][j]);
}
getch();
}
3b. Design and implement C/C++ Program to find the transitive closure using
Warshal's algorithm
#include <stdio.h>
int main() {
int graph[nV][nV] = {{0, 3, INF, 5},
{2, 0, INF, 4},
{INF, 1, 0, INF},
{INF, INF, 2, 0}};
floydWarshall(graph);
}
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>
#define INF 999
void dijkstra(int c[10][10],int n,int s,int d[10])
{
int v[10],min,u,i,j;
for(i=1;i<=n;i++)
{
d[i]=c[s][i];
v[i]=0;
}
v[s]=1;
for(i=1;i<=n;i++)
{
min=INF;
for(j=1;j<=n;j++)
if(v[j]==0 && d[j]<min)
{
min=d[j];
u=j;
}
v[u]=1;
for(j=1;j<=n;j++)
if(v[j]==0 && (d[u]+c[u][j])<d[j])
d[j]=d[u]+c[u][j];
}
}
int main()
{
int c[10][10],d[10],i,j,s,sum,n;
printf("\nEnter 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",&c[i][j]);
printf("\nEnter the souce node:");
scanf("%d",&s);
dijkstra(c,n,s,d);
for(i=1;i<=n;i++)
printf("\nShortest distance from %d to %d is %d",s,i,d[i]);
return 0;
}
#include <stdlib.h>
#include <stdio.h>
void dfs(int u)
{
int v;
s[++top] = u;
visited[u] = 1;
res[k--] = s[top--];
}
void topological_sort()
{
int v;
for (v = 0; v < n; v++) {
if (visited[v] != 1) {
dfs(v);
}
}
int main()
{
int i, j;
k = n - 1;
topological_sort();
return 0;
}
6. Design and implement C/C++ Program to solve 0/1 Knapsack problem using
Dynamic Programming method.
#include <stdlib.h>
#include <stdio.h>
void knapsack()
{
int i, j, v[n+1][m+1];
for (i = 0; i <= n; i++)
{
for (j = 0; j <= m; j++)
{
if (i == 0 || j == 0)
{
v[i][j] = 0;
}
else if (j < w[i])
{
v[i][j] = v[i-1][j];
}
else
{
v[i][j] = max(v[i-1][j], p[i] + v[i-1][j-w[i]]);
}
}
}
printf("Maximum profit : %d\n", v[n][m]);
j = m;
for (i = n; i >= 0; i--)
{
if (v[i][j] != v[i-1][j])
{
sel[i] = 1;
j = j - w[i];
}
}
p = calloc(n, sizeof(int));
w = calloc(n, sizeof(int));
sel = calloc(n, sizeof(int));
#include<stdio.h>
int main()
{
float weight[50],profit[50],ratio[50],Totalvalue,temp,capacity,amount;
int n,i,j;
printf("Enter the number of items :");
scanf("%d",&n);
for (i = 0; i < n; i++)
{
printf("Enter Weight and Profit for item[%d] :\n",i);
scanf("%f %f", &weight[i], &profit[i]);
}
printf("Enter the capacity of knapsack :\n");
scanf("%f",&capacity);
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;
}
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 <stdlib.h>
#include <stdio.h>
int main()
{
int i;
w = calloc(n, sizeof(int));
sel = calloc(n, sizeof(int));
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 on an array
void selectionSort(int arr[], int n)
{
int i, j, min_idx;
for (i = 0; i < n-1; i++)
{
min_idx = i; // Assume the current element is the minimum
for (j = i+1; j < n; j++)
{
if (arr[j] < arr[min_idx])
{
min_idx = j; // Update min_idx if a smaller element is found
}
}
// Swap the found minimum element with the current element
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
// Function to generate an array of random numbers
void generateRandomNumbers(int arr[], int n)
{
for (int i = 0; i < n; i++)
{
arr[i] = rand() % 10000; // Generate random numbers between 0 and 9999
}
}
int main()
{
int n, arr[100],i;
printf("Enter number of elements: ");
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 <stdlib.h>
#include <stdio.h>
#include <time.h>
int *a, n;
int partition(int l, int r)
{
int i, j, p, temp;
i = l;
j = r + 1;
p = a[l];
while (i <= j)
{
do
{
i++;
} while (p >= a[i]);
do
{
j--;
} while (p < a[j]);
if (i < j)
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
temp = a[l];
a[l] = a[j];
a[j] = temp;
return j;
}
void print_array()
{
int i;
for (i = 0; i < n; i++)
{
printf("%d\t", a[i]);
}
printf("\n");
}
int main()
{
int i;
float start, end, complexity;
printf("Enter the value of n : ");
scanf("%d", &n);
a = calloc(n, sizeof(int));
for (i = 0; i < n; i++)
{
a[i] = rand() % 20;
}
printf("The array is : \n");
print_array();
start = clock();
q_sort(0, n - 1);
end = clock();
printf("The sorted array is : \n");
print_array();
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>
i = 0;
j = 0;
k = left;
i++;
k++;
}
free(L);
free(R);
}
int main()
{
int ni, arr[100];
printf("Enter the number of elements: ");
scanf("%d", &n);
if (n <= 5000)
{
printf("Please enter a value greater than 5000\n");
return 1; // Exit if the number of elements is not greater than 5000
}
printf("\nEnter the array:");
for(i=0;i<n;i++)
scanf(“%d”,&arr[i]);
generateRandomArray(arr, n);
// Repeat the sorting process multiple times to increase duration for timing
clock_t start = clock();
for (int i = 0; i < 1000; i++)
{
mergeSort(arr, 0, n - 1);
}
clock_t end = clock();
return 0;
}
12. Design and implement C/C++ Program for N Queen's problem using
Backtracking.
#include <stdlib.h>
#include <stdio.h>
int *x, q = 0, n;
int can_place(int p)
{
int i;
for (i = 0; i < q; i++)
{
if (x[i] == p || x[i] - i == p - q || x[i] + i == p + q)
{
return 0;
}
}
return 1;
}
void print_board()
{
int i, j;
printf("Solution : \n");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if (x[i] == j)
{
printf("Q ");
}
else
{
printf("_ ");
}
}
printf("\n");
}
}
void n_queens()
{
int p;
for (p = 0; p < n; p++)
{
if (can_place(p))
{
x[q] = p;
q++;
if (q == n)
{
print_board();
}
else
{
n_queens();
}
q--;
}
}
}
int main()
{
printf("Board size : ");
scanf("%d", &n);
x = calloc(n, sizeof(int));
n_queens();
return 0;
}