Docsity

Ada lab programs, Study notes of Algorithms and Programming

Toggle sidebar
bg1
Lab1: Sort a given set of elements using the Quicksort method and determine the time required to sort theelements.
Repeat the experiment for different values of n and plot a graph of time taken versus n. The elements can be read
from a file or can be generated using the random number generator.
(using file)
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void quicksort(int a[], int l, int h);
void swap(int *a, int *b)
{
int t;
t=*a;
*a=*b;
*b=t;
}
int main()
{
int n, i, a[500], ele;
FILE *fp;
clock_t st, et;
float dur;
srand(time(NULL));
fp=fopen("teju.txt", "w");
printf("\nEnter the number of elements:\t");
scanf("%d", &n);
for(i=0;i<n;i++)
{
ele=rand()%1000;
fprintf(fp, "%d\t", ele);
}
fclose(fp);
fp=fopen("teju.txt", "r");
printf("\n elements are:\n");
for(i=0;i<n;i++)
{
fscanf(fp, "%d", &a[i]);
printf("%d\t", a[i]);
}
fclose(fp);
st=clock();
quicksort(a, 0, n-1);
et=clock();
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21

Partial preview of the text

Download Ada lab programs and more Study notes Algorithms and Programming in PDF only on Docsity!

Lab1 : Sort a given set of elements using the Quicksort method and determine the time required to sort theelements. Repeat the experiment for different values of n and plot a graph of time taken versus n. The elements can be read from a file or can be generated using the random number generator.

(using file) #include<stdio.h> #include<stdlib.h> #include<time.h> void quicksort(int a[], int l, int h);

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

int main() { int n, i, a[500], ele; FILE *fp; clock_t st, et; float dur; srand(time(NULL));

fp=fopen("teju.txt", "w"); printf("\nEnter the number of elements:\t"); scanf("%d", &n); for(i=0;i<n;i++) { ele=rand()%1000; fprintf(fp, "%d\t", ele); } fclose(fp);

fp=fopen("teju.txt", "r"); printf("\n elements are:\n"); for(i=0;i<n;i++) { fscanf(fp, "%d", &a[i]); printf("%d\t", a[i]); } fclose(fp);

st=clock(); quicksort(a, 0, n-1); et=clock();

dur=(double)(et-st)/CLOCKS_PER_SEC;

printf("\n Sorted array is:\n"); for(i=0;i<n;i++) printf("%d\t", a[i]);

printf("\n The duration is: %f\t", dur); return 0; }

(or)

#include<stdio.h> #include<stdlib.h> #include<time.h> void quicksort(int a[], int l, int h);

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

int main() { int n, i, a[500]; clock_t st,et; float dur; srand(time(NULL));

printf("\nEnter the number of elements:\t"); scanf("%d",&n);

printf(โ€œ\nGenerated elements are:\nโ€); for(i=0; i<n; i++) { a[i]=rand()%1000; printf("%d\t",a[i]); }

st=clock(); quicksort(a, 0, n-1); et=clock();

dur=(double)(et-st)/CLOCKS_PER_SEC;

printf("\n Sorted array is:\n");

Lab2 : Using OpenMP implement a parallised Mergesort algorithm to sort a given set of elements and determine the time required to sort the elements. Repeat the experiment for different values of n and plot a graph of 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<omp.h> #define NUM_THREADS 4 int a[500], b[500]; void mergesort(int a[], int low, int high); void merge(int a[], int low, int mid, int high);

void mergesort(int a[], int low, int high) { int mid; if(low>=high) return; mid=(low+high)/2;

#pragma omp parallel sections { #pragma omp section mergesort(a, low, mid); #pragma omp section mergesort(a, mid+1, high); #pragma omp section merge(a, low, mid, high); } }

void merge(int a[],int low, int mid, int high) { int i, j, k, h; i=low; j=mid+1; h=low;

while((i<=mid)&&(j<=high)) { if(a[i]<=a[j]) b[h++]=a[i++]; else b[h++]=a[j++]; }

while(i<=mid) b[h++]=a[i++]; while(j<=high) b[h++]=a[j++]; for(k=low;k<=high;k++)

a[k]=b[k]; }

void main() { int i, n, ch=1; double start, end, dur; omp_set_num_threads(NUM_THREADS); while(ch) { srand(time(NULL)); printf("\n Enter the number of elements:\t"); scanf("%d", &n);

printf("\n generated array elements:\n"); for(i=0;i<n;i++) { a[i]=rand()%1000; printf("%d\t", a[i]); }

start=omp_get_wtime(); mergesort(a,0,n-1); end=omp_get_wtime(); dur=(end-start);

printf("\n Sorted elements are:\n"); for(i=0;i<n;i++) { printf("%d\t",a[i]); }

printf("\n Total duration is:%f",dur); printf("\n DO u want to continue?(0/1)"); scanf("%d",&ch); } }

Output:

Enter the number of elements: 5

generated array elements: 797 516 971 501 834 Sorted elements are: 501 516 797 834 971 Total duration is:0. DO u want to continue?(0/1)

Lab3a : Obtain Topological ordering of vertices in given digraph

#include<stdio.h> #include<stdlib.h> int topology(); int in_degree(); int i,j,n,a[10][10]; int indegree[10];

int main() { printf("\n Enter the number of nodes:\t"); scanf("%d",&n); printf("\n Enter the adjacency matrix:\n"); for(i=0;i<n;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]);

topology(); return 0; }

int in_degree() { int s; for(j=0;j<n;j++) { s=0; for(i=0;i<n;i++) { s=s+a[i][j]; } indegree[j]=s; } return 0; }

int topology() {

int top=-1, s[10],t[10], u, v, k=0,m; in_degree(); for(i=0;i<n;i++) { if(indegree[i]==0) { top++; s[top]=i; }

while(top!=-1) { u=s[top]; top--; t[k]=u; k++; for(v=0;v<n;v++) { if(a[u][v]==1) { indegree[v]--; if(indegree[v]==0) { top++; s[top]=v; } } } }

printf("\nTopological sorted array is:\n"); for(m=0;m<n;m++) printf("%d\t",t[m]); return 0; }

Output:

Enter the number of nodes: 5

Enter the adjacency matrix: 0 0 1 0 0 0 0 1 0 0 0 0 0 1 1 0 0 0 0 1 0 0 0 0 0

Topological sorted array is: 1 0 2 3 4

Transitive closure is: 1 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1

Lab 4 : Implement 0/1 knapsack problem using dynamic programming.

#include<stdio.h> int p[50],w[50],v[50][50],x[50],W,n;

int max(int a,int b) { if(a>b) return a; else return b; }

int knapsack(int n,int W) { int i,j; for(i=1;i<=n;i++) { for(j=1;j<=W;j++) { if(j>=w[i]) v[i][j]=max(v[i-1][j],p[i]+v[i-1][j-w[i]]); else v[i][j]=v[i-1][j]; } } }

void display(int n,int W) { int k=W,m=n,i,j; printf("\nSOlution table:\n"); for(i=0;i<=n;i++) { for(j=0;j<=W;j++) { printf("%d\t",v[i][j]); } printf("\n"); }

for(i=1;i<=n;i++) x[i]=0;

printf("\nSolution subset is:\n"); for(i=n;(i>0&&W>0);i--) { if(v[i][W]!=v[i-1][W]) {

Solution table: 0 0 0 0 0 0 0 0 12 12 12 12 0 10 12 22 22 22 0 10 12 22 30 32 0 10 15 25 30 37

Solution subset is: 4 2 1 Solution vector is: 1 1 0 1 Total profit=

Lab5 : From a given vertex in a weighted connected graph, find the shortest paths to other vertices using Dikstraโ€™s algorithm.

#include<stdio.h> int u, v, n, source, min; int visited[20], w[20][20], p[20], dist[20];

int min_distance() { int i,j; min=999; for(i=1;i<=n;i++) { if((dist[i]<min)&&(visited[i]==0)) { min=dist[i]; u=i; } } return u; }

void shortest_path() { int i,j; for(i=1;i<=n;i++) { j=i; printf("The shorest path from node %d to %d is ",source, i); while(j!=source) { printf("%d<---",j); j=p[j]; } printf("%d\n", j); printf("The shortest distance from node %d to %d = %d\n", source, i, dist[i]); } }

int main() { int i, j; printf("Enter the no of nodes\n"); scanf("%d", &n); printf("Enter the weight matrix\n"); for(i=1;i<=n;i++) for(j=1;j<=n;j++) scanf("%d", &w[i][j]);

The shortest distance from node 1 to 3 = 5 The shorest path from node 1 to 4 is 4<--- The shortest distance from node 1 to 4 = 4

Lab 6 : Find Minimum Cost spanning tree(MST) of a given undirected graph using Kruskal algorithm.

#include<stdio.h> #include<stdlib.h>

int find(int i); int uni(int i, int j); int parent[10]; int main() { int i, j, n, min, ne=1, u, v, a, b, cost[10][10], mincost=0; printf("\nEnter the value of n:\t"); scanf("%d", &n); printf("\nEnter Weighted 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; } } 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; } } } 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("\nMinimum cost=%d", mincost);

lab 7a : Print all the nodes reachable from a given starting node in a digraph using BFS method.

#include<stdio.h> #include<stdlib.h> void bfs(int v); int a[20][20], reach[20], k=1, f=-1, r=-1, v, n, q[20], visited[20];

int main() { int i, j, ch=1; printf("\n Enter the number of vertices:\t"); scanf("%d", &n);

printf("\nEnter the adjacency matrix:\n"); for(i=1;i<=n;i++) for(j=1;j<=n;j++) scanf("%d", &a[i][j]);

while(ch) { k=1; for(i=1;i<=n;i++) reach[i]=0;

printf("\n Enter the starting vertex:\t"); scanf("%d", &v); bfs(v); if(k<n) printf("\nNodes are not reachable"); else { printf("\nNodes reachable from starting vertex %d is:\n",v); for(i=1;i<k;i++) printf("%d\t", visited[i]); } printf("\nDo you want to continue(1/0):\t"); scanf("%d", &ch); } }

void bfs(int v) { int i; reach[v]=1; r++; q[r]=v; while(f!=r) { f++;

v=q[f]; for(i=1;i<=n;i++) { if((a[v][i]==1)&&(reach[i]==0)) { reach[i]=1; visited[k++]=i; r++; q[r]=i; } } } }

Output:

Enter the number of vertices: 4

Enter the adjacency matrix: 0 1 1 1 0 0 0 1 0 0 0 0 0 0 1 0

Enter the starting vertex: 1

Nodes reachable from starting vertex 1 is: 2 3 4 Do you want to continue(1/0): 1

Enter the starting vertex: 2

Nodes are not reachable Do you want to continue(1/0): 0