ADA Small Prgs
ADA Small Prgs
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>
int find(int i) {
while(p[i]) i = p[i];
return i;
int main() {
printf("Matrix:\n");
while(edges < n - 1) {
min = INF;
min = c[i][j], a = i, b = j;
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>
v[s] = 1;
min = INF;
return cost;
int main() {
int a[10][10], n, s, i, j;
printf("Adjacency Matrix:\n");
return 0;
3A. Design and implement C/C++ Program to solve All-Pairs Shortest Paths problem using Floyd’s algorithm.
#include <stdio.h>
int main() {
int a[10][10], n, i, j, k;
scanf("%d", &a[i][j]);
3B. Design and implement C/C++ Program to find the transitive closure using Warshal’s algorithm.
#include <stdio.h>
int main() {
int a[10][10], n, i, j, k;
scanf("%d", &a[i][j]);
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>
int main() {
scanf("%d", &n);
scanf("%d", &c[i][j]);
scanf("%d", &s);
v[s] = 1;
min = INF;
v[u] = 1;
5. Design and implement C/C++ Program to obtain the Topological ordering of vertices in a given
digraph.
#include <stdio.h>
int main() {
scanf("%d", &a[i][j]);
in[j] += a[i][j];
if(in[j] == 0) {
in[j] = -1;
t[++k] = j;
break;
6. Design and implement C/C++ Program to solve 0/1 Knapsack problem using Dynamic Programming method.
#include<stdio.h>
int w[10],p[10],n;
if(i>n)return 0;
if(w[i]>m)return k(i+1,m);
return max(k(i+1,m),k(i+1,m-w[i])+p[i]);
int main(){
int m,i;
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)scanf("%d%d",&p[i],&w[i]);
printf("%d",k(1,m));
}
7. Design and implement C/C++ Program to solve discrete Knapsack and continuous Knapsack
problems using greedy approximation method.
#include<stdio.h>
int main(){
scanf("%d",&n);
for(i=0;i<n;i++)scanf("%d",&w[i]);
for(i=0;i<n;i++)scanf("%d",&p[i]);
scanf("%d",&m);
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if((double)p[i]/w[i]<(double)p[j]/w[j]){
int t=w[i];w[i]=w[j];w[j]=t;
t=p[i];p[i]=p[j];p[j]=t;
int cw=0;maxp=0;
for(i=0;i<n;i++){
if(cw+w[i]<=m){x[i]=1;cw+=w[i];maxp+=p[i];}
else{x[i]=(double)(m-cw)/w[i];maxp+=x[i]*p[i];break;}
printf("%.1f\n",maxp);
for(i=0;i<n;i++)printf("%.1f ",x[i]);
8. Design and implement C/C++ Program to find a subset of a given set S = {s1 , s2,…..,sn} of n positive integers
whose sum is equal to a given positive integer d.
#include<stdio.h>
int s[10],x[10],d;
x[k]=1;
if(p+s[k]==d){
printf("\n");
else if(p+s[k]+s[k+1]<=d)f(p+s[k],k+1,r-s[k]);
if(p+r-s[k]>=d&&p+s[k+1]<=d){
x[k]=0;
f(p,k+1,r-s[k]);
int main(){
int n,i,sum=0;
scanf("%d",&n);
for(i=1;i<=n;i++)scanf("%d",&s[i]);
scanf("%d",&d);
for(i=1;i<=n;i++)sum+=s[i];
if(sum<d||s[1]>d)printf("No subset\n");
else f(0,1,sum);
}
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>
int i, j, min, t;
min = i;
if(a[j]<a[min]) min = j;
int main() {
int *a = malloc(n*sizeof(int));
clock_t s=clock();
selSort(a,n);
clock_t e=clock();
printf("%f\n", (double)(e-s)/CLOCKS_PER_SEC);
free(a);
}
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; scanf("%d",&n);
if(n<=5000)return 1;
int *a=malloc(n*sizeof(int));
clock_t s=clock();
quickSort(a,0,n-1);
clock_t e=clock();
printf("%f\n",(double)(e-s)/CLOCKS_PER_SEC);
free(a);
}
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 n1 = m - l + 1, n2 = r - m, i = 0, j = 0, k = l;
i = j = 0;
while (i < n1 && j < n2) a[k++] = (L[i] < R[j]) ? L[i++] : R[j++];
free(L); free(R);
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(a, l, m);
mergeSort(a, m + 1, r);
merge(a, l, m, r);
int main() {
mergeSort(a, 0, n - 1);
free(a);
return 0;
12. Design and implement C/C++ Program for N Queen’s problem using Backtracking.
#include <stdio.h>
#include <stdlib.h>
return 1;
if(c==n) return 1;
for(int r=0;r<n;r++){
if(safe(b,n,r,c)){
b[r][c]=1;
if(solve(b,n,c+1)) return 1;
b[r][c]=0;
return 0;
int main(){
int **b=malloc(n*sizeof(int*));
for(i=0;i<n;i++) b[i]=calloc(n,sizeof(int));
if(solve(b,n,0)){
for(i=0;i<n;i++){
printf("\n");
for(i=0;i<n;i++) free(b[i]);
free(b);
return 0;