0% found this document useful (0 votes)
5 views11 pages

ADA Small Prgs

The document contains multiple C/C++ programs implementing various algorithms including Kruskal's and Prim's for Minimum Cost Spanning Trees, Floyd's for All-Pairs Shortest Paths, Dijkstra's for shortest paths in a weighted graph, and several sorting algorithms like Selection, Quick, and Merge Sort. It also includes solutions for the 0/1 Knapsack problem, the N Queen's problem using backtracking, and methods for finding transitive closure and topological ordering in graphs. Each program is designed to read input, perform the specified algorithm, and output the results, with some programs also measuring and displaying execution time.

Uploaded by

1jt23ai050
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views11 pages

ADA Small Prgs

The document contains multiple C/C++ programs implementing various algorithms including Kruskal's and Prim's for Minimum Cost Spanning Trees, Floyd's for All-Pairs Shortest Paths, Dijkstra's for shortest paths in a weighted graph, and several sorting algorithms like Selection, Quick, and Merge Sort. It also includes solutions for the 0/1 Knapsack problem, the N Queen's problem using backtracking, and methods for finding transitive closure and topological ordering in graphs. Each program is designed to read input, perform the specified algorithm, and output the results, with some programs also measuring and displaying execution time.

Uploaded by

1jt23ai050
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

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>

#define INF 999

int c[10][10], p[10];

int find(int i) {

while(p[i]) i = p[i];

return i;

int main() {

int n, i, j, a, b, u, v, min, cost = 0, edges = 0;

printf("n: "); scanf("%d", &n);

printf("Matrix:\n");

for(i = 1; i <= n; i++) for(j = 1; j <= n; j++) scanf("%d", &c[i][j]);

while(edges < n - 1) {

min = INF;

for(i = 1; i <= n; i++)

for(j = 1; j <= n; j++)

if(c[i][j] < min && find(i) != find(j))

min = c[i][j], a = i, b = j;

u = find(a); v = find(b); p[v] = u;

printf("%d - %d : %d\n", a, b, min);

cost += min; edges++;

c[a][b] = c[b][a] = INF;

printf("Total Cost = %d\n", cost);

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>

#define INF 999


int prim(int a[10][10], int n, int s) {

int v[10] = {0}, d[10], p[10], i, j, u, min, cost = 0;

for(i = 1; i <= n; i++) d[i] = a[s][i], p[i] = s;

v[s] = 1;

for(i = 1; i < n; i++) {

min = INF;

for(j = 1; j <= n; j++)

if(!v[j] && d[j] < min) min = d[u = j];

v[u] = 1; cost += d[u];

printf("%d -> %d, Cost = %d\n", p[u], u, cost);

for(j = 1; j <= n; j++)

if(!v[j] && a[u][j] < d[j]) d[j] = a[u][j], p[j] = u;

return cost;

int main() {

int a[10][10], n, s, i, j;

printf("n: "); scanf("%d", &n);

printf("Adjacency Matrix:\n");

for(i = 1; i <= n; i++) for(j = 1; j <= n; j++) scanf("%d", &a[i][j]);

printf("Source: "); scanf("%d", &s);

printf("Total Cost = %d\n", prim(a, n, s));

return 0;

3A. Design and implement C/C++ Program to solve All-Pairs Shortest Paths problem using Floyd’s algorithm.

#include <stdio.h>

#define INF 999

int min(int a, int b) { return a < b ? a : b; }

int main() {
int a[10][10], n, i, j, k;

printf("n: "); scanf("%d", &n);

for(i = 1; i <= n; i++)

for(j = 1; j <= n; j++)

scanf("%d", &a[i][j]);

for(k = 1; k <= n; k++)

for(i = 1; i <= n; i++)

for(j = 1; j <= n; j++)

a[i][j] = min(a[i][j], a[i][k] + a[k][j]);

for(i = 1; i <= n; i++, printf("\n"))

for(j = 1; j <= n; j++)

printf("%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;

printf("n: "); scanf("%d", &n);

for(i = 1; i <= n; i++)

for(j = 1; j <= n; j++)

scanf("%d", &a[i][j]);

for(k = 1; k <= n; k++)

for(i = 1; i <= n; i++)

for(j = 1; j <= n; j++)

a[i][j] = a[i][j] || (a[i][k] && a[k][j]);

for(i = 1; i <= n; i++, printf("\n"))

for(j = 1; j <= n; j++)


printf("%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>

#define INF 999

int main() {

int c[10][10], d[10], v[10] = {0}, n, i, j, s, u, min;

scanf("%d", &n);

for(i = 1; i <= n; i++)

for(j = 1; j <= n; j++)

scanf("%d", &c[i][j]);

scanf("%d", &s);

for(i = 1; i <= n; i++) d[i] = c[s][i];

v[s] = 1;

for(i = 1; i < n; i++) {

min = INF;

for(j = 1; j <= n; j++)

if(!v[j] && d[j] < min) min = d[j], u = j;

v[u] = 1;

for(j = 1; j <= n; j++)

if(!v[j] && d[u] + c[u][j] < d[j]) d[j] = d[u] + c[u][j];

for(i = 1; i <= n; i++) printf("%d ", d[i]);

5. Design and implement C/C++ Program to obtain the Topological ordering of vertices in a given
digraph.

#include <stdio.h>

int main() {

int a[10][10], in[10] = {0}, t[10], n, i, j, k = 0;


scanf("%d", &n);

for(i = 1; i <= n; i++)

for(j = 1; j <= n; j++) {

scanf("%d", &a[i][j]);

in[j] += a[i][j];

for(i = 1; i <= n; i++)

for(j = 1; j <= n; j++)

if(in[j] == 0) {

in[j] = -1;

t[++k] = j;

for(int x = 1; x <= n; x++) in[x] -= a[j][x];

break;

for(i = 1; i <= k; i++) printf("%d ", t[i]);

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;

int max(int a,int b){return a>b?a:b;}

int k(int i,int m){

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 p[50],w[50],n,m,i,j;double x[50],maxp;

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;

void f(int p,int k,int r){

x[k]=1;

if(p+s[k]==d){

for(int i=1;i<=k;i++)if(x[i])printf("%d ",s[i]);

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>

void selSort(int *a, int n) {

int i, j, min, t;

for(i=0; i<n-1; i++) {

min = i;

for(j=i+1; j<n; j++)

if(a[j]<a[min]) min = j;

t = a[min]; a[min] = a[i]; a[i] = t;

int main() {

int n; scanf("%d", &n);


if(n <= 5000) return 1;

int *a = malloc(n*sizeof(int));

for(int i=0; i<n; i++) a[i] = rand()%10000;

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>

void swap(int *a,int *b){int t=*a;*a=*b;*b=t;}

int partition(int a[],int l,int h){int p=a[h],i=l-1,j;for(j=l;j<h;j++)if(a[j]<p)swap(&a[++i],&a[j]);swap(&a[i+1],&a[h]);return


i+1;}

void quickSort(int a[],int l,int h){if(l<h){int p=partition(a,l,h);quickSort(a,l,p-1);quickSort(a,p+1,h);}}

int main(){

int n; scanf("%d",&n);

if(n<=5000)return 1;

int *a=malloc(n*sizeof(int));

for(int i=0;i<n;i++)a[rand()%100000]=rand()%100000; // Just to fill some data

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>

void merge(int a[], int l, int m, int r) {

int n1 = m - l + 1, n2 = r - m, i = 0, j = 0, k = l;

int *L = malloc(n1 * sizeof(int)), *R = malloc(n2 * sizeof(int));

for (; i < n1; i++) L[i] = a[l + i];

for (i = 0; i < n2; i++) R[i] = a[m + 1 + i];

i = j = 0;

while (i < n1 && j < n2) a[k++] = (L[i] < R[j]) ? L[i++] : R[j++];

while (i < n1) a[k++] = L[i++];

while (j < n2) a[k++] = R[j++];

free(L); free(R);

void mergeSort(int a[], int l, int 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() {

int n; scanf("%d", &n);

if (n <= 5000) return 1;

int *a = malloc(n * sizeof(int));


for (int i = 0; i < n; i++) a[i] = rand() % 100000;

clock_t start = clock();

mergeSort(a, 0, n - 1);

clock_t end = clock();

printf("%f\n", (double)(end - start) / CLOCKS_PER_SEC);

free(a);

return 0;

12. Design and implement C/C++ Program for N Queen’s problem using Backtracking.

#include <stdio.h>

#include <stdlib.h>

int safe(int **b,int n,int r,int c){

for(int i=0;i<c;i++) if(b[r][i]) return 0;

for(int i=r,j=c;i>=0&&j>=0;i--,j--) if(b[i][j]) return 0;

for(int i=r,j=c;i<n&&j>=0;i++,j--) if(b[i][j]) return 0;

return 1;

int solve(int **b,int n,int c){

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 n,i,j; scanf("%d",&n);

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++){

for(j=0;j<n;j++) printf(b[i][j]?"Q ":"# ");

printf("\n");

} else printf("No solution\n");

for(i=0;i<n;i++) free(b[i]);

free(b);

return 0;

You might also like