You are on page 1of 39

ADA lab programs

1. Sort a given set of elements using 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 the file or can be generated using the random
number generator.
#include<stdio.h>
int partition (int a[],int low,int high)
{
int key,i,j,temp;
key=a[low];
i=low+1;
j=high;
while(1)
{
while((key>=a[i])&&(i<high))
i++;
while((key<a[j])&&(j>=low))
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 s;
if(low<high)
{
s=partition(a,low,high);
quicksort(a,low,s-1);
quicksort(a,s+1,high);
}
}
void main()
{
int a[20],i,n,k;
float t1,t2;
clrscr();
printf("\n Enter the size of array");
scanf("%d",&n);
for(i=0;i<n;i++)
a[i]=random()%100;
t1=clock();
quicksort(a,0,n-1);
t2=clock();
printf("\n Sorted elements are \n");
for(i=0;i<n;i++)
printf("%d \n",a[i]);
printf("Time taken for sorting is %f seconds",(t2-t1)/CLK_TCK);
}

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<omp.h>
#include<time.h>
void mergesort(int a[100],int low,int high);
void merge(int a[100],int low,int mid,int high);
int total=0;
void main()
{
int i,n,a[100];
clock_t start=0,end=0;
printf("Enter the no.of elements\n");
scanf("%d",&n);
printf("\n Enter the elements to be generated\n");
for(i=0;i<n;i++)
{
a[i]=rand()%100;

printf("%d ",a[i]);
}
start=clock();
mergesort(a,0,n-1);
end=clock();
printf("The sorted elements are\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("The time taken for sorting process is : %lf", (end-start)/
(double)CLOCKS_PER_SEC);
printf("The total no. of threads are %d ",total);
}
void mergesort(int a[100],int low,int high)
{
int i,j,mid;
int nthreads,tid;
for(i=1;i<=10000;i++)
for(j=1;j<=1000;j++);
#pragma omp parallel shared(a,low,mid,high,nthreads,tid)
{
if(low<high)
{
#pragma omp sections nowait
4

{
mid=(low+high)/2;
mergesort(a,low,mid);
mergesort(a,mid+1,high);
merge(a,low,mid,high);
}
}
tid=omp_get_thread_num();
if(tid==0)
{
nthreads=omp_get_num_threads();
if(total<nthreads)
total=nthreads;
printf("\n the total threads are %d ",nthreads);
}
}
}
void merge(int a[100],int low,int mid,int high)
{
int i=low,j=mid+1,k=low,c[20];
while(i<=mid && j<=high)
{
if(a[i]<a[j])
5

c[k++]=a[i++];
else
c[k++]=a[j++];
}
while(i<=mid) c[k++]=a[i++];
while(j<=high) c[k++]=a[j++];
for(i=low;i<=high;i++)
a[i]=c[i];
}

3a. Obtain the Topological ordering of vertices in a given digraph.


#include<stdio.h>
void main()
{
int a[10][10],i,flag[10],j,n; clrscr();
printf("\nEnter the no of nodes:");
scanf("%d",&n);
for(i=1;i<=n;i++)
flag[i]=0;
printf("\n\nEnter the adjacency matrix:");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&a[i][j]);
if(a[i][j]==1)
flag[j]=flag[j]+1;
}
printf("\n\nTopological sorting is\n");
topology(a,n,flag);
}
topology(int a[10][10],int n, int flag[10])
{
7

int i,j;
for(i=1;i<=n;i++)
{
if(flag[i]==0)
{
printf("%d ",i);
flag[i]=-1;
for(j=1;j<=n;j++)
{
if(a[i][j]==1)
flag[j]=flag[j]-1;
}
i=0;
}
}
}

3b. Compute the transitive closure of a given directed graph using


Warshalls algorithm.
#include<stdio.h>
void main()
{
int a[10][10],i,j,n;
printf("\nEnter the no of nodes:");
scanf("%d",&n);
printf("\nEnter the adjacent matrix:");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
warshalls(a,n);
printf("\n\nThe path matrix after applying transitive closure is\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
}
warshalls(int a[10][10],int n)
9

{
int i,j,k;
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] ;
}

10

4. Implement a 0/1 Knapsack problem using Dynamic programming.


#include<stdio.h>
int v[10][10];
int max(int a,int b)
{
return((a>b)?a:b);
}
int knapsack(int n,int m,int p[][],int w[][])
{
int i,j;
for(i=0;i<=n;i++)
{
for(j=0;j<=m;j++)
{
if(i==0||j==0)
v[i][j]=0;
else if(j-w[i]>=0)
v[i][j]=max(v[i-1][j], p[i]+v[i-1][j-w[i]]);
else
v[i][j]=v[i-1][j];
}
}
11

return v[n][m];
}
void optimalsubset(int n,int m,int v[][],int w[][])
{
int i,j;
i=n;
j=m;
while((i!=0)&&(j!=0))
{
if(v[i][j]!=v[i-1][j])
{
printf("\n Item %d \n",i);
j=j-w[i];
}
}
}

void main()
{
int n,m,p[10][10],w[10][10],i,j;
printf("\n Enter the number of items:");
scanf("%d",&n);
printf("\n Enter the weights of each item:");
12

for(i=1;i<=n;i++)
scanf("%d",&w[i]);
printf("\n Enter the values of each item:");
for(i=1;i<=n;i++)
scanf("%d",&p[i]);
printf("\n Enter knapsack capacity:");
scanf("%d",&m);
value=knapsack(n,m,p,w);
printf("\n Solution
programming\n");

of

the

knapsack

problem

by

dynamic

for(i=0;i<=n;i++)
{
for(j=0;j<=m;j++)
printf("%d\t",v[i][j]);
printf("\n");
}
printf("\n The maximal value is %d ",value);
printf("\n The items of optimal subset are \n");
optimalsubset(n,m,v,w);
}

13

5. From a given vertex in a weighted connected graph, find shortest


paths to other vertices using Dijkstras algorithm.
#include<stdio.h>
void dijkstras(int [][10],int,int);
void main()
{
int a[10][10],source,n,i,j;
printf("\nEnter the no of nodes:");
scanf("%d",&n);
printf("\nEnter the cost adjacency matrix:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d", &a[i][j]);
printf("\nEnter the source node:");
scanf("%d",&source);
dijkstras(a,n,source);
}
void dijkstras(int a[10][10],int n,int source)
{
int d[10],s[10],i,minm,u,v,j;
for(i=1;i<=n;i++)
{
14

s[i]=0;
d[i]=a[source][i];
}
s[source]=1;
for(i=1;i<=n;i++)
{
minm=999;
for(j=1;j<=n;j++)
if(s[j]==0)
if(d[j]<minm)
{
minm=d[j];
u=j;
}
s[u]=1;
for(v=1;v<=n;v++)
if(s[v]==0)
if(d[v]>(d[u]+a[u][v]))
{
d[v]=d[u]+a[u][v];
}
}
printf("\nShortest distance from source is \n");
15

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

printf("%d ==> %d in %d\n",source,i,d[i]);


}

16

6. Find Minimum Cost Spanning Tree of a given undirected graph


using Kruskals algorithm.

#include<stdio.h>
#include<stdlib.h>
struct edge
{
Int begv,endv,cost;
};
typedef struct edge E;
int getparent(int parent[20],int v)
{
while(parent[v]!=1)
V=parent[v];
return v;
}
void kruskal(int a[][],int n)
{
E e[200],temp;
Int count=1,i,j,parent[20],sum=0,parenti,parentj,nedges=0;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
17

if((a[i][j]!=0)&&(a[i][j]!=999))
{
e[count].begv=i;
e[count].endv=j;
e[count].cost=a[i][j];
cost++;
}
count=count-1;
for(i=1;i<=count-1;i++)
for(j=1;j<=count-1;j++)
if(e[j+1].cost<e[j].cost)
{
temp.begv=e[j+1].begv;
temp.endv=e[j+1].endv;
temp.cost=e[j+1].cost;
e[j+1].begv=e[j].begv;
e[j+1].endv=e[j].endv;
e[j+1].cost=e[j].cost;
e[j].begv=temp.begv;
e[j].endv=temp.endv;
e[j].cost=temp.cost;
}
for(i=1;i<=n;i++)
18

parent[i]=-1;

for(i=1;i<=count;i++)
{
parenti=getparent(parent,e[i].begv);
parentj=getparent(parent,e[i].endv);
if(parenti!=parentj)
{
printf(%d%d=%d\n,e[i].begv,e[i].endv,e[i].cost);
sum=sum+e[i].cost;
parent[parentj]=parenti;
nedges++;
}
}
if(nedges==n-1)
printf(Cost is %d,sum);
else
exit(0);
}

void main()
{
int n,a[10][10],I,j;
19

printf("\n Enter the number of vertices");


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

20

7a. Print all the nodes reachable from a given starting node in a
digraph using BFS method.
#include<stdio.h>
void bfs(int n,int a[10][10],int source,int s[10])
{
int i,f=0,r=-1,q[10],v;
printf("%d -->",source);
s[source]=1;
q[++r]=source;
while(f<=r)
{
v=q[f++];
for(i=1;i<=n;i++)
if(s[i]==0&&a[v][i])
{
q[++r]=i;
printf("%d -->",i);
s[i]=1;
}
}
}
void main()
21

{
int n,a[10][10],i,j,source,s[10];
printf("\n Enter the number of nodes in the graph");
scanf("%d",&n);
printf("\n Enter the adjacency matrix for the graph");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
printf("\n Enter the source node");
scanf("%d",&source);
for(i=1;i<=n;i++)
s[i]=0;
printf("\n The BFS traversal is");
bfs(n,a,source,s);
printf("\n the nodes reachable are \n");
for(i=1;i<=n;i++)
if(s[i])
printf("%d\n",i);
}

22

7b. Check whether a given graph is connected or not using DFS


method.
#include<stdio.h>
int s[10];
void dfs(int a[][10],int n, int v);
void main()
{
int n, a[10][10],i,j,flag;
printf("\nenter num of nodes:");
scanf("%d",&n);
printf("\nenter adj matrix\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
s[j]=0;
dfs(a,n,i);
}
flag=0;
for(i=1;i<=n;i++)
{
23

if(s[i]==0) flag=1;
}
if(flag==0) printf("\ngraph connected");
else printf("\ngraph not connected");
}

void dfs(int a[10][10],int n,int v)


{
int k;
s[v]=1;
for(k=1;k<=n;k++)
{
if(a[v][k]==1 && visited[k]==0)
dfs(a,n,k);
}
}

24

8. Find a subset of a given set S={s1,s2,s3,,sn} of n positive


integers whose sum is equal to given positive integer d. A suitable
message is to be displayed if the given problem instance doesnt
have a solution.
#include<stdio.h>
int total=0,n,i,p,r,x[10],s[10],count=0;
void subset(int k,int sum,int rem)
{
x[k]=1;
if(s[k]+sum==total)
{
count++;
printf("\n");
for(i=1;i<=k;i++)
{
If(x[i]==1)
printf("%d\t",s[i]);
}
}
else if(s[k]+s[k+1]+sum<=total)
subset(k+1,sum+s[k],rem-s[k]);
if( (sum+rem-s[k]>=total) && (sum+s[k+1]<=total) )
{
x[k]=0;
subset(k+1,sum,rem-s[k]);
}
}
void main()
{
int rem=0;
printf("Enter the number of elements in the set\n");
scanf("%d", &n);
printf("Enter the elements in increasing order\n");
for(i=1;i<=n;i++)
{
25

scanf("%d",&s[i]);
rem=rem+s[i];
x[i]=0;
}
printf("Enter Max subset value\n");
scanf("%d",&total);
if(total>rem)
printf("Subset not possible\n");
else
{
subset(1,0,r);
if(count==0)
printf("Solution does not exist\n");
else
printf("\nNumber of subsets is %d\n",count);
}
}

26

9. Implement any scheme to find the optimal solution for the


Travelling Salesperson Problem.
#include<stdio.h>
Int a[10][10],path[100],paths[10][10],dis[100],vis[100],visited[100],n,source,
solno=0;
float optimal(void);
float nearest(void);
void main()
{
int dist=0,i,j;
float p,q;
printf("\nEnter the no of cities\n");
scanf("%d",&n);
printf("\nEnter\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
printf("\nEnter the source node\n");
scanf("%d",&source);
visited[source]=1;
path[1]=source;
distance(a,path,dist,2,source,visited);
p=optimal();
q=nearest();
printf("\n\nResult=%f",(q/p));
}
Void distance(int a[10][10],int path[100],int dist,int no,int s,int visited[100])
{
int flag=0,i;
for(i=1;i<=n;i++)
{
if(!visited[i])
{
27

flag=1;
visited[i]=1;
path[no]=i;
distance(a,path,dist+a[s][i],no+1,i,visited);
visited[i]=0;
}
}
if(flag==0)
{
printf("\nSolution %d \n",solno);
for(i=1;i<=n;i++)
{
printf("%d->",path[i]);
paths[solno][i]=path[i];
}
printf("%d\t",source);
printf("Distance:: %d\n",dist+a[s][source]);
dis[solno]=dist+a[s][source];
solno++;
}
}
float optimal()
{
int minm=dis[0],solnno=0,i;
for(i=1;i<solno;i++)
if( dis[i]<minm)
{
minm=dis[i];
solnno=i;
}
printf("\nOptimal path is ");
for(i=1;i<=n;i++)
printf("%d->",paths[solnno][i]);
printf("%d",source);
printf("\nOptimal cost is %d",minm);
return minm;
}
28

float nearest()
{
int i,j,x,y,cost=0,minm=999,count=0 ;
printf("\n\nPath using nearest\n");
printf("%d ",source);
vis[source]=1;
while(count<n-1)
{
minm=999;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
if(vis[i] && !vis[j] && a[i][j]<minm)
{
minm=a[i][j];
x=i;
y=j;
}
}
cost=cost+a[x][y];
printf("->%d",y);
vis[y]=1;
count++;
}
printf("->%d",source);
cost=cost+a[y][source];
printf("\nMinimum cost is %d",cost);
return cost;
}

29

10. Find Minimum Cost Spanning Tree of a given undirected graph


using Prims algorithm.

#include<stdio.h>
void main()
{
int n,a[10][10],i,j,sum;
printf("Enter the no. of nodes\n");
scanf("%d",&n);
printf("Enter the cost adj. matrix\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
sum=prims(a,n,1);
if(sum>=999)
printf("spanning does not exist\n");
else
printf("cost of MST is %d",sum)
}

prims(int a[10][10],int n,int source)


{
int s[10],d[10],i,j,sum,u,v,minm,inter[10][10];
30

for(i=1;i<=n;i++)
{
s[i]=0;
d[i]=a[source][i];
iner[i]=0;
}
sum=0;
s[source]=1;
for(i=1;i<=n-1;i++)
{
minm=999;
for(j=1;j<=n;j++)
{
if(s[j]==0)
if(d[j]<minm)
{
minm=d[j];
u=j;
}
}
sum=sum+d[u];
s[u]=1;
for(v=1;v<=n;v++)
31

if(s[v]==0)if(d[v]>a[u][v])
{
d[v]=a[u][v];
inter[v]=u;
}
}
printf(Path of the MST is sourcedestination=cost:\n);
for(i=1;i<=n;i++)
if(inter[i]!=i)
printf(%d%d=%d\n,i,a[inter[i]][i]);
return sum;
}

32

11. Implement All-Pairs Shortest Paths Problem using Floyds


algorithm. Parallelize this algorithm, implement it using OpenMP and
determine the speed-up achieved.
Int total;
#include<stdio.h>
#include<omp.h>
#include<time.h>
int cost[10][10],a[10][10];
void floyds(int cost[10][10],int n);
int min(int a,int b)
{
return(a<b)?a:b;
}
int main()
{
int i,j,n,tot;
clock_t start=0,end=0;
printf("enter the no. of vertices\n");
scanf("%d",&n);
printf("\n the cost adj matrix is\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{

33

scanf("%d",&cost[i][j]);
a[i][j]=cost[i][j];
}
start=clock();
tot=floyds(cost,n);
end=clock();
printf("\nthe path matrix is \n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d ",cost[i][j]);
printf("\n");
}
printf("\n the time taken for sorting process is: %lf",(end-start)/
(double)CLOCKS_PER_SEC);
}

int floyds(int cost[10][10],int n)


{
int i,j,k;
int nthreads,tid;

for(i=1;i<=10000;i++)
34

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

#pragma omp parallel shared(i,j,k,cost,a,nthreads,tid)


{
#pragma omp parallel for num_threads(2)

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]);
tid=omp_get_thread_num();
if(tid==0)
{
nthreads=omp_get_num_threads();
if(total<nthreads)
total=nthreads;
printf("\n total threads are: %d ",nthreads);
}
}
return total;
}

35

12. Implement N Queens problem using Back Tracking.


#include<stdio.h>
#include<math.h>
Int place(int x[],int k);
Void nqueen (int n);
Void printsol(int x,int n);
void main()
{
int n;
printf("\nEnter the number of queens\n");
scanf("%d",&n);
if(n==1)
printf(Trivial solution,1x1 chess board, 1 queen placed.\n);
else if(n==2||n==3)
printf(No solution.\n);
else
nqueen(n);
}
Void nqueen(int n)
{
int k=1,x[10],i,count=0;
x[k]=0;
while(k!=0)
36

{
x[k]=x[k]+1;
while( x[k] <=n && place(x,k) ==0 )
x[k]=x[k]+1;
if(x[k]<=n)
{
if(k==n)
{
count++;
printf("\nSolution %d is\n",count);
for(i=1;i<=n;i++)
printf("%d ",x[i]);
printf("\nChessBoard Representation is\n");
printsol(x,n);
}
else
{
k=k+1;
x[k]=0;
}
}
else
k=k-1;
37

}
return;
}

Int place(int x[10],int k)


{
int i;
for(i=1;i<=k-1;i++)
{
if( x[i]==x[k] )
return 0;
if( abs(x[i]-x[k])== abs(i-k))
return 0;
}
return 1;
}

Void printsol(int x[10],int n)


{
char c[10][10],i,j;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
c[i][j]='X';
38

for(i=1;i<=n;i++)
c[i][x[i]]='Q';

for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%c ",c[i][j]);
printf("\n");
}
return;
}

39