# ANALYSIS & DESIGN OF ALGORITHM LAB PROGRAMS

B.G.S.I.T IV CSE/ISE

1.Sort a given set of elements using the Quicksort method and determine the time required to sort the elements. Repeat the experiment for different values of n, the number of elements in the list to be sorted and 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<conio.h> #include<time.h> int partition(int a[],int low,int high) { int i,j,key,temp; key=a[low]; i=low+1; j=high; while(1) { while(i<high && key>=a[i ]) i++; while(key<a[j]) j--; if(i<j) { temp=a[i]; a[i]=a[j]; a[j]=temp; } else { temp=a[low]; a[low]=a[j]; a[j]=temp; return j; } } } void quicksort(int a[],int low,int high) { int j; if(low<high) { j=partition(a,low,high); quicksort(a,low,j-1); quicksort(a,j+1,high); } } void main() { int i,n,a[20]; clock_t st,end; clrscr(); printf("\n enter the valu of n"); scanf("%d",&n); printf("\n enter no to be sorted"); for(i=0;i<n;i++) scanf("%d",&a[i]); st=clock(); quicksort(a,0,n-1); end=clock(); printf("\n the sorted array is\n"); for(i=0;i<n;i++) printf("%d\n",a[i]); printf("\n time taken=%d",end-st/CLK_TCK); getch(); }

T IV CSE/ISE 2. end=clock().j.low.int high) { int mid.I. mergesort(a. clock_t st. i=low.int low. k=low. else c[k++]=a[j++]. merge(a.int high) { int c[10]. } } void mergesort(int a[]. mergesort(a.i.a[i]).h> #include<time.mid.i++) scanf("%d".h> void merge(int a[]. printf("\n time taken=%d".n-1). printf("enter %d elements\n". while(j<=high) c[k++]=a[j++].high). the number of elements in the list to be sorted and plot a graph of the time taken versus n. { while((i<=mid)&&(j<=high)) { if(a[i]<=a[j]) c[k++]=a[i++].i<n.int low.i<n. } while(i<=mid) c[k++]=a[i++].mid). mergesort(a. j=mid+1. st=clock(). for(i=0.k.end.int mid.ANALYSIS & DESIGN OF ALGORITHM LAB PROGRAMS B. getch(). } } } void main() { int a[10].high). #include<stdio. Using OpenMP.S. scanf("%d".&a[i]). implement a parallelized Merge Sort algorithm to sort a given set of elements and determine the time required to sort the elements. } //copy the lowest element from left part of A to C //copy the lowest element from right part of A to C //copy remaining elements from left part of A to C //copy remaining elements from right part of A to C //copy the elements from array C to array A //divide the array into equal parts //sort the left part of the array recursively // sort the right part of the array recursively //merge the left part and right part .n.0.i. The elements can be read from a file or can be generated using the random number generator. for(i=0.&n).i++) a[i]=c[i]. if(low<high) { { mid=(low+high)/2.i++) printf("%d\n".end-st/CLK_TCK). printf("enter number of elements to sort\n"). Repeat the experiment for different values of n. printf("\n sorted elements are\n"). clrscr().i<=high.low.G.mid+1.n). for(i=low.

k.j<n.i<n.h> #include<conio.k<n. for(i=0. printf("\n").j++) scanf("%d".i<n.j++) printf("%d\t". } } void main() { int n. printf("enter the cost adjecency matrix\n").j<n.int a[10][10].i<n.j++) p[i][j]=a[i][j].int a[10][10]) { int i.a).&a[i][j]). } void read_matrix(int n.i++) for(j=0.S. for(i=0.j.ANALYSIS & DESIGN OF ALGORITHM LAB PROGRAMS B. printf("transitive closure is as shown below\n"). warshall(n.a[10][10].k++) //find the transitive closure [path matrix] for(i=0. printf("enter the no of nodes\n").j.p). #include<stdio.int p[10][10]) { int i.p). //make a copy of adjacency matrix for(k=0. b.int a[10][10]) { int i.a[i][j]). } .j<n.p[10][10].G. scanf("%d".i<n.h> void warshall(int n.T IV CSE/ISE 3. for(i=0.i++) for(j=0. getch().I. write_matrix(n. } void write_matrix(int n.j. read_matrix(n.j<n.j++) if(p[i][j]==0 && (p[i][k]==1 && p[k][j]==1)) p[i][j]=1. Compute the transitive closure of a given directed graph using Warshall s algorithm. clrscr().i++) { for(j=0.a.&n).i++) for(j=0.

&a[i][j]). #include<stdio.G.v++) //find the nodes v which are adjacent to u { if(a[u][v]==1 && s[v]==0) { s[v]=1. //add v to 's' indicates that v is visited now q[++r]=v.i<n.i++) s[i]=0. printf("enter adjacency matrix\n").j++) scanf("%d". a.r. int u. int i.i<n.ANALYSIS & DESIGN OF ALGORITHM LAB PROGRAMS B.T IV CSE/ISE 7. no node is visited //insert 'source' into Q //add source to 's'[mark source as visited] while(f<=r) //as long as queue is not empty { u=q[f++].source. printf("enter number of vertice\n").i<n. printf("enter source\n "). f=r=0. s[source]=1. for(i=0.h> #include<process.v.flag.I.&n). Print all the nodes reachable from a given starting node in a digraph using BFS method.i++) for(j=0.S.h> void bfs(int n.i++) { if(s[i]==0) printf("%d is not reachable\n". //insert new vertex into Q for exploration } } } } void main() { int n. //initially.i).&source).j. //delete the next vertex to be explored for(v=0. clrscr(). for(i=0.i).s[10]. scanf("%d". bfs(n. } getch(). } .i.s). scanf("%d".int source.source.q[20].int a[10][10].j<n. for(i=0. else printf("%d is reachable\n".a[10][10].a.int s[]) { int f. q[r]=source.v<n.

i<=n. printf("\n the sorted path obtained is \n").&n).j<=n.n).i<=n.S. all_paths(cost.i++) for(j=1.ANALYSIS & DESIGN OF ALGORITHM LAB PROGRAMS B.j<=n. void all_paths(int cost[10][10].n.j<=n.k.j++) printf("\t %d".int n) { int i. } .a[i][k]+a[k][j]).int a[10][10].j. Implement All-Pairs Shortest Paths Problem using Floyd s algorithm.j.h> int cost[10][10].i<=n. clrscr(). #include<stdio.k<=n. } int mini(int a. else return b. Parallelize this algorithm.h> #include<stdlib.k++) for(i=1. } void main() { int i.j++) scanf("%d".i++) for(j=1. for(i=1.j++) a[i][j]=mini(a[i][j].j<=n.i<=n.a. for(k=1.T IV CSE/ISE 11. scanf("%d".I.a[10][10].h> #include<conio. implement it using OpenMP and determine the speed-up achieved.G.int b) { if(a<b) return a. for(i=1.j++) a[i][j]=cost[i][j]. printf("\n enter the no of vertices\n").i++) for(j=1. printf("\n"). for(i=1.a[i][j]). } getch().i++) { for(j=1. &cost[i][j]). printf("\n enter the adjacency matrix\n").