1. Sort a given set of elements using the Quick sort 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> void swap(int *x,int *y) { int temp; temp=*x; *x=*y; *y=temp; } int partition(int a[20], int l, int r) { int i,j; int p; p=a[l]; i=l+1; j=r; while(i<=j) { delay(100); while(a[i]< =p) i++; while(a[j]>p) j- -; ____________________________________________________________ _____________ S T J I T Ranebennur Design & Analysis of Algorithms Lab Dept. of CSE
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
1. Sort a given set of elements using the Quick sort 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.
_________________________________________________________________________S T J I T Ranebennur Design & Analysis of Algorithms Lab Dept. of CSE
s=partition(a,l,r);qsort(a,l,s-1);qsort(a,s+1,r);
}}
void main(){
int a[20],i,j,n;clock_t begin,end;clrscr();printf("\n Quick Sort");printf("\nEnter The Value of n\n");scanf("%d",&n);printf("Elements Are: ");for(i=1;i<=n;i++)
printf("\t%d",a[i] =rand()%100);begin=clock();qsort(a,1,n);end=clock();printf("\nSorted Array is");for(i=1;i<=n;i++)printf("\t%d",a[i]);printf("\nBegin Time=%d\n",begin);printf("End Time is=%d\n",end);printf("Total No. of clock ticks=%d\n",(end-begin));printf("Total Time Required=%f",((end-begin)/CLK_TCK));getch();
} Out Put :-
Quick SortEnter The Value of n 5Elements Are17 95 15 48 26Sorted Array is15 17 26 48 95Begin Time=50End Time is=61Total No. of clock ticks=11Total Time Required=0.604396
_________________________________________________________________________S T J I T Ranebennur Design & Analysis of Algorithms Lab Dept. of CSE
2. Using OpenMP, implement a parallelized Merge Sort 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, 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<stdlib.h.>#include<time.h>#include<omp.h>void merge(int a[30], int l, int mid, int r){
_________________________________________________________________________S T J I T Ranebennur Design & Analysis of Algorithms Lab Dept. of CSE
void mergesort(int a[30], int l, int r){
int mid;if(l<r){ mid=(l+r)/2;
#pragma omp parallel sections {
#pragma omp sectionmergesort(a, l, mid);
#pragma omp section mergesort(a,mid+1,r);}
merge(a, l, mid, r);} }void main(){
int a[30],n,i;double starttime,endtime:printf("Enter the number of elements:\t");scanf("%d",&n);printf("The elements before sorting are:\n");
for(i=1;i<=n;i++) { printf(“%5d”,a[i]=rand()%100); } starttime=omp_get_wtime(); mergesort(a,1,n); endtime=omp_get_wtime(); printf("\nThe sorted elements are:\n"); for(i=1;i<=n;i++) { printf("%d\t",a[i]); } printf("\nThe time taken is %lf\n",(double)(endtime-starttime));
}
Out Put :-Enter the number of elements: 5The elements before sorting are 3 12 5 90 10The sorted elements are: 3 5 10 12 90The time taken is 0.102453
_________________________________________________________________________S T J I T Ranebennur Design & Analysis of Algorithms Lab Dept. of CSE
3.a. Obtain the topological ordering of vertices in a given digraph.
void topo(int n, int indegree[10], int a[10][10]){ int i,j; for(i=1;i<=n;i++) { if(indegree[i] ==0) { indegree[i]= -1; temp[++k]=i; for(j=1;j<=n;j++) {
void main(){ int a[10][10],i,j,n; clrscr(); printf(" Enter the no of vertices:\n"); scanf("%d",&n); printf(" Enter the adjacency matrix:\n"); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { scanf("%d",&a[i][j]); } } warshall(a,n); printf("The Transitive Closure of given graph is :\n");
for(i=1;i<=n;i++) {
for(j=1;j<=n;j++)
_________________________________________________________________________S T J I T Ranebennur Design & Analysis of Algorithms Lab Dept. of CSE
{ printf("%d\t",a[i][j]); }
printf("\n");}
getch();}
Out Put :-
Enter the number .of vertices 4Enter the adjacency matrix:0 1 0 00 0 0 10 0 0 01 0 1 0
The Transitive Closure of given graph is :1 1 1 1 1 1 1 10 0 0 01 1 1 1
_________________________________________________________________________S T J I T Ranebennur Design & Analysis of Algorithms Lab Dept. of CSE
BA
C D
4. Implement 0/1 Knapsack problem using Dynamic Programming
int n,i,j,capacity;int wt[20],ve[20],v[20][20],w;clrscr();printf("Enter the No. of items\n:");scanf("%d",&n);printf("-------------------Weight Value--------------------\n\n");for(i=1;i<=n;i++){
scanf("%d",&wt[i]);scanf("%d",&ve[i]);
}printf("Enter the Capacity of Knapsack\n");scanf("%d",&capacity);for(i=0;i<=n;i++){
for(j=0;j<=capacity;j++){
if(i==0||j==0){
v[i][j] =0;}else if(wt[i]>j){
v[i][j] =v[i-1][j]; } else
{ v[i][j] =max(v[i-1][j],v[i-1][j-wt[i]]+ve[i]);
}printf("%4d",v[i][j]);
}printf("\n");
}
_________________________________________________________________________S T J I T Ranebennur Design & Analysis of Algorithms Lab Dept. of CSE
w=capacity;printf("the item in the knapsack\n");for(i=n;i>0;i--){
if(v[i][w] ==v[i-1][w])continue;
else{
w=w-wt[i]; printf("%3d",wt[i]);
}}printf("\n Total Profit=%d",v[n][capacity]);getch();
}
Out Put :-1)Enter the No. of items: 2-------------------Weight Value--------------------2 103 20Enter the Capacity of Knapsack:30 0 0 00 0 10 100 0 10 20
int i,q[10],u; int f=1,r=1; visited[source] =1; q[r] =source; while(f<=r) { u=q[f]; f++; for(i=1;i<=n;i++) if(a[u][i] ==1 && visited[i] ==0) {
r++;q[r] =i;visited[i] =1;
} }}
void main(){ int n,a[10][10],i,j,source; clrscr(); printf("Enter the number of nodes : "); scanf("%d",&n); printf("Enter the adjacency matrix\n"); for(i=1;i<=n;i++) for(j=1;j<=n;j++)
scanf("%d",&a[i][j]); printf("\nEnter the source : "); scanf("%d",&source);
for(i=1;i<=n;i++) visited[i] =0;
_________________________________________________________________________S T J I T Ranebennur Design & Analysis of Algorithms Lab Dept. of CSE
int i; visited[source] =1; for(i=1;i<=n;i++) if(a[source][i] ==1 && visited[i] ==0)
dfs(n,a,i);}
void main(){ int i,j,source; int n,a[10][10];
clrscr();printf("\nEnter 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",&a[i][j]);printf("\nEnter the source vertex : ");
scanf("%d",&source);for(i=1;i<=n;i++)
visited[i] =0;dfs(n,a,source);for(i=1;i<=n;i++)
{ if(visited[i] ==0) printf("\nThe node %d is not reachable",i); else printf("\nThe node %d is reachable",i); } getch();}
_________________________________________________________________________S T J I T Ranebennur Design & Analysis of Algorithms Lab Dept. of CSE
Out Put :-
1.Enter the number of nodes : 3Enter the adjacency Matrix 0 1 1 0 0 01 1 0Enter The Source Vertex: 3
The node 1 is ReachableThe node 2 is ReachableThe node 3 is Reachable
2.Enter the number of nodes : 4Enter the adjacency Matrix 0 1 1 1 0 0 0 10 0 0 10 0 0 0Enter The Source Vertex:1
The node 1 is ReachableThe node 2 is ReachableThe node 3 is ReachableThe node 4 is Reachable
_________________________________________________________________________S T J I T Ranebennur Design & Analysis of Algorithms Lab Dept. of CSE
1
3 4
2
1
2 3
8. 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. for example, if S={ 1,2,5,6,8} and d=9 there are two solutions { 1,2,6} and {1,8}. A suitable message is to be displayed if the given problem instance does not have a solution.
#include<stdio.h>#include<conio.h>#define MAX 10int s[MAX],x[MAX];int d;
void sumofsub(int p,int k,int r){
int i; x[k] =1; if((p+s[k])==d) { for(i=1;i<=k;i++)
int i,n,sum=0; clrscr(); printf("\nEnter max number : "); scanf("%d",&n); printf("\nEnter the set in increasing order : \n"); for(i=1;i<=n;i++) scanf("%d",&s[i]);
printf("\nEnter the max subset value : ");
_________________________________________________________________________S T J I T Ranebennur Design & Analysis of Algorithms Lab Dept. of CSE
1)Enter the max number 5Enter the set in increasing order:1 2 5 6 8
Enter the max subset value: 9
1 2 61 8
2)Enter the max number 5Enter the set in increasing order:3 4 5 6 9
Enter the max subset value: 2
No subset possible
_________________________________________________________________________S T J I T Ranebennur Design & Analysis of Algorithms Lab Dept. of CSE
9. Implement any scheme to find the optimal solution for the Traveling Salesperson problem and then solve the same problem instance using any approximation algorithm and determine the error in the approximation.
}printf("\n The Minimum Cost of Spanning Tree is %d\n", mincost);
}void main(){
clrscr();
_________________________________________________________________________S T J I T Ranebennur Design & Analysis of Algorithms Lab Dept. of CSE
printf("Enter the no.of vertices :\n");scanf("%d",&n);printf("Enter the cost of 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;for(i=2;i<=n;i++)
visited[i] =0;prims( n,cost);getch();
}
Out Put :-
Enter the number of verteces : 4
2 5
4 1
36
Enter the cost of adjacency matrix0 2 4 32 0 5 14 5 0 63 1 6 0
1 Edge (1,2) =22 Edge (2,4) =13 Edge (1,3) =4The minimum cost of spanning tree is 7
2
4 1
_________________________________________________________________________S T J I T Ranebennur Design & Analysis of Algorithms Lab Dept. of CSE
43
1 2
3 4
1 2
11. Implement All-Pairs Shortest Paths Problem using Floyd's algorithm. Parallelize this algorithm, implement it using OpenMP and determine the speed-up achieved.
int i,j,n,cost[10][10];double starttime, end time;printf("Enter the no.of nodes : ");scanf("%d",&n);printf("\nEnter the adjacency cost matrix\n");for(i=1;i<=n;i++)
for(j=1;j<=n;j++)scanf("%d",&cost[i][j]);
starttime=omp_get_wtime();floyd(n, cost);endtime=omp_get_wtime();printf("\n\n All pair shortest path matrix\n");for(i=1;i<=n;i++){
_________________________________________________________________________S T J I T Ranebennur Design & Analysis of Algorithms Lab Dept. of CSE
for(j=1;j<=n;j++)printf("%d\t",cost[i][j]);
printf("\n");}printf("\n\nThe time taken is %l0.9f\n",(double)(endtime-starttime));return 0;
}
Out Put :-Enter the no.of nodes : 4
4 3
5 6 1
2
Enter the adjacency cost matrix0 4 5 999999 0 3 6999 999 0 21 999 999 0