ADA Lab Programs (WWW - Vtuloop.com)
ADA Lab Programs (WWW - Vtuloop.com)
Algorithms
Subject Code : 10CSL47
m
.co
Lab Manual
op
lo
tu
.v
w
w
w
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>
{
tu
int i, j, key, k;
.v
w
if(low>=high)
w
return;
w
void main()
{
int n, a[1000],k;
clock_t st,et;
double ts;
clrscr();
printf("\n Enter How many Numbers: ");
scanf("%d", &n);
printf("\nThe Random Numbers are:\n");
for(k=1; k<=n; k++)
{
a[k]=rand();
printf("%d\t",a[k]);
}
st=clock();
QuickSort(a, 1, n);
et=clock();
ts=(double)(et-st)/CLOCKS_PER_SEC;
printf("\nSorted Numbers are: \n ");
for(k=1; k<=n; k++)
printf("%d\t", a[k]);
m
.co
getch();
lo
}
tu
Output:
.v
w
w
w
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 <conio.h>
#include<time.h>
void Merge(int a[], int low, int mid, int high)
{
int i, j, k, b[20];
i=low; j=mid+1; k=low;
while ( i<=mid && j<=high )
{ m
if( a[i] <= a[j] )
.co
b[k++] = a[i++] ;
op
else
lo
b[k++] = a[j++] ;
tu
}
.v
w
printf("%d\t", a[k]);
op
getch();
tu
}
.v
Output:
w
w
w
3. a. Obtain the Topological ordering of vertices in a given digraph.
#include<stdio.h>
#include<conio.h>
int a[10][10],n,indegre[10];
void find_indegre()
{ int j,i,sum;
for(j=0;j<n;j++)
{
sum=0;
for(i=0;i<n;i++)
sum+=a[i][j];
indegre[j]=sum;
}
}
void topology()
{ m
int i,u,v,t[10],s[10],top=-1,k=0;
.co
find_indegre();
op
for(i=0;i<n;i++)
lo
{
tu
.v
if(indegre[i]==0) s[++top]=i;
w
}
w
while(top!=-1)
w
{
u=s[top--];
t[k++]=u;
for(v=0;v<n;v++)
{
if(a[u][v]==1)
{
indegre[v]--;
if(indegre[v]==0) s[++top]=v;
}
}
}
printf("The topological Sequence is:\n");
for(i=0;i<n;i++)
printf("%d ",t[i]);
}
void main()
{
int i,j;
clrscr();
printf("Enter number of jobs:");
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
topology();
getch();
}
Output:
m
.co
op
lo
tu
.v
w
w
w
3. b. Compute the transitive closure of a given directed graph using
Warshall's algorithm.
# include <stdio.h>
# include <conio.h>
int n,a[10][10],p[10][10];
void path()
{
int i,j,k;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
p[i][j]=a[i][j];
for(k=0;k<n;k++)
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(p[i][k]==1&&p[k][j]==1) p[i][j]=1;
}
m
void main()
.co
{
op
int i,j;
lo
clrscr();
tu
.v
scanf("%d",&n);
w
w
#include<stdio.h>
#include<conio.h>
int w[10],p[10],v[10][10],n,i,j,cap,x[10]={0};
int max(int i,int j)
{
return ((i>j)?i:j);
}
int knap(int i,int j)
{
int value;
if(v[i][j]<0)
{
if(j<w[i])
value=knap(i-1,j);
else m
value=max(knap(i-1,j),p[i]+knap(i-1,j-w[i]));
.co
v[i][j]=value;
op
}
lo
return(v[i][j]);
tu
.v
}
w
void main()
w
{
w
int profit,count=0;
clrscr();
printf("\nEnter the number of elements\n");
scanf("%d",&n);
printf("Enter the profit and weights of the elements\n");
for(i=1;i<=n;i++)
{
printf("For item no %d\n",i);
scanf("%d%d",&p[i],&w[i]);
}
printf("\nEnter the capacity \n");
scanf("%d",&cap);
for(i=0;i<=n;i++)
for(j=0;j<=cap;j++)
if((i==0)||(j==0))
v[i][j]=0;
else
v[i][j]=-1;
profit=knap(n,cap);
i=n;
j=cap;
while(j!=0&&i!=0)
{
if(v[i][j]!=v[i-1][j])
{
x[i]=1;
j=j-w[i];
i--;
}
else
i--;
}
printf("Items included are\n");
m
.co
printf("Sl.no\tweight\tprofit\n");
op
for(i=1;i<=n;i++)
lo
if(x[i])
tu
printf("%d\t%d\t%d\n",++count,w[i],p[i]);
.v
getch();
w
Output:
5. From a given vertex in a weighted connected graph, find shortest
paths to other vertices using Dijkstra's algorithm.
#include<stdio.h>
#include<conio.h>
#define infinity 999
void dij(int n,int v,int cost[10][10],int dist[100])
{
int i,u,count,w,flag[10],min;
for(i=1;i<=n;i++)
flag[i]=0,dist[i]=cost[v][i];
count=2;
while(count<=n)
{
min=99;
for(w=1;w<=n;w++)
if(dist[w]<min && !flag[w])
m
min=dist[w],u=w;
.co
flag[u]=1;
op
count++;
lo
for(w=1;w<=n;w++)
tu
.v
dist[w]=dist[u]+cost[u][w];
w
w
}
}
void main()
{
int n,v,i,j,cost[10][10],dist[10];
clrscr();
printf("\n Enter the number of nodes:");
scanf("%d",&n);
printf("\n Enter the cost 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]=infinity;
}
printf("\n Enter the source matrix:");
scanf("%d",&v);
dij(n,v,cost,dist);
printf("\n Shortest path:\n");
for(i=1;i<=n;i++)
if(i!=v)
printf("%d->%d,cost=%d\n",v,i,dist[i]);
getch();
}
Output:
m
.co
op
lo
tu
.v
w
w
w
6. Find Minimum Cost Spanning Tree of a given undirected graph
using Kruskal's algorithm.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int i,j,k,a,b,u,v,n,ne=1;
int min,mincost=0,cost[9][9],parent[9];
int find(int);
int uni(int,int);
void main()
{
clrscr();
printf("\n\n\tImplementation of Kruskal's algorithm\n\n");
printf("\nEnter the no. of vertices\n");
scanf("%d",&n);
printf("\nEnter the cost adjacency matrix\n");
m
for(i=1;i<=n;i++)
.co
{
op
for(j=1;j<=n;j++)
lo
{
tu
.v
scanf("%d",&cost[i][j]);
w
if(cost[i][j]==0)
w
w
cost[i][j]=999;
}
}
printf("\nThe edges of Minimum Cost Spanning Tree are\n\n");
while(ne<n)
{
for(i=1,min=999;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("\n\tMinimum cost = %d\n",mincost);
getch();
}
int find(int i)
{
while(parent[i])
i=parent[i];
m
.co
return i;
op
}
lo
{
.v
if(i!=j)
w
w
{
w
parent[j]=i;
return 1;
}
return 0;
}
Output:
7. a. Print all the nodes reachable from a given starting node in a
digraph using BFS method.
#include<stdio.h>
#include<conio.h>
int a[20][20],q[20],visited[20],n,i,j,f=0,r=-1;
void bfs(int v)
{
for(i=1;i<=n;i++)
if(a[v][i] && !visited[i])
q[++r]=i;
if(f<=r)
{
visited[q[f]]=1;
bfs(q[f++]);
}
}
m
void main()
.co
{
op
int v;
lo
clrscr();
tu
.v
scanf("%d",&n);
w
w
for(i=1;i<=n;i++)
{
q[i]=0;
visited[i]=0;
}
printf("\n Enter graph data in matrix form:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
printf("\n Enter the starting vertex:");
scanf("%d",&v);
bfs(v);
printf("\n The node which are reachable are:\n");
for(i=1;i<=n;i++)
if(visited[i])
printf("%d\t",i);
getch();
}
Output:
m
.co
op
lo
tu
.v
w
w
w
7. b. Check whether a given graph is connected or not using DFS
method.
#include<stdio.h>
#include<conio.h>
int a[20][20],reach[20],n;
void dfs(int v)
{
int i;
reach[v]=1;
for(i=1;i<=n;i++)
if(a[v][i] && !reach[i])
{
printf("\n %d->%d",v,i);
dfs(i);
}
}
m
void main()
.co
{
op
int i,j,count=0;
lo
clrscr();
tu
.v
scanf("%d",&n);
w
w
for(i=1;i<=n;i++)
{
reach[i]=0;
for(j=1;j<=n;j++)
a[i][j]=0;
}
printf("\n Enter the adjacency matrix:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
dfs(1);
printf("\n");
for(i=1;i<=n;i++)
{
if(reach[i])
count++;
}
if(count==n)
printf("\n Graph is connected");
else
printf("\n Graph is not connected");
getch();
}
Output:
m
.co
op
lo
tu
.v
w
w
w
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
doesn't have a solution.
#include<stdio.h>
#include<conio.h>
int s[10] , x[10],d ;
void sumofsub ( int , int , int ) ;
void main ()
{
int n , sum = 0 ;
int i ;
clrscr () ;
printf ( " \n Enter the size of the set : " ) ;
scanf ( "%d" , &n ) ;
m
.co
Output:
m
.co
op
lo
tu
.v
w
w
w
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.
m
.co
op
lo
tu
.v
w
w
w
10. Find Minimum Cost Spanning Tree of a given undirected graph
using Prim’s algorithm.
#include<stdio.h>
#include<conio.h>
int a,b,u,v,n,i,j,ne=1;
int visited[10]={0},min,mincost=0,cost[10][10];
void main()
{
clrscr();
printf("\n Enter 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",&cost[i][j]);
m
if(cost[i][j]==0)
.co
cost[i][j]=999;
op
}
lo
visited[1]=1;
tu
.v
printf("\n");
w
while(ne<n)
w
w
{
for(i=1,min=999;i<=n;i++)
for(j=1;j<=n;j++)
if(cost[i][j]<min)
if(visited[i]!=0)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
if(visited[u]==0 || visited[v]==0)
{
printf("\n Edge %d:(%d %d) cost:%d",ne++,a,b,min);
mincost+=min;
visited[b]=1;
}
cost[a][b]=cost[b][a]=999;
}
printf("\n Minimun cost=%d",mincost);
getch();
}
Output:
m
.co
op
lo
tu
.v
w
w
w
11. Implement All-Pairs Shortest Paths Problem using Floyd's
algorithm. Parallelize this algorithm, implement it using OpenMP
and determine the speed-up achieved.
#include<stdio.h>
#include<conio.h>
int min(int,int);
void floyds(int p[10][10],int n)
{
int i,j,k;
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(i==j)
p[i][j]=0;
else
p[i][j]=min(p[i][j],p[i][k]+p[k][j]);
m
}
.co
{
lo
tu
if(a<b)
.v
return(a);
w
w
else
w
return(b);
}
void main()
{
int p[10][10],w,n,e,u,v,i,j;;
clrscr();
printf("\n Enter the number of vertices:");
scanf("%d",&n);
printf("\n Enter the number of edges:\n");
scanf("%d",&e);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
p[i][j]=999;
}
for(i=1;i<=e;i++)
{
printf("\n Enter the end vertices of edge%d with its weight \n",i);
scanf("%d%d%d",&u,&v,&w);
p[u][v]=w;
}
printf("\n Matrix of input data:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d \t",p[i][j]);
printf("\n");
}
floyds(p,n);
printf("\n Transitive closure:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d \t",p[i][j]);
m
.co
printf("\n");
op
}
lo
for(i=1;i<=n;i++)
.v
for(j=1;j<=n;j++)
w
w
{
w
if(i!=j)
printf("\n <%d,%d>=%d",i,j,p[i][j]);
}
getch();
}
Output:
w
w
w
.v
tu
lo
op
.co
m
12. Implement N Queen's problem using Back Tracking.
#include<stdio.h>
#include<conio.h>
#include<math.h>
int a[30],count=0;
int place(int pos)
{
int i;
for(i=1;i<pos;i++)
{
if((a[i]==a[pos])||((abs(a[i]-a[pos])==abs(i-pos))))
return 0;
}
return 1;
}
void print_sol(int n) m
{
.co
int i,j;
op
count++;
lo
printf("\n\nSolution #%d:\n",count);
tu
for(i=1;i<=n;i++)
.v
w
{
w
for(j=1;j<=n;j++)
w
{
if(a[i]==j)
printf("Q\t");
else
printf("*\t");
}
printf("\n");
}
}
void queen(int n)
{
int k=1;
a[k]=0;
while(k!=0)
{
a[k]=a[k]+1;
while((a[k]<=n)&&!place(k))
a[k]++;
if(a[k]<=n)
{
if(k==n)
print_sol(n);
else
{
k++;
a[k]=0;
}
}
else
k--;
}
}
void main()
m
.co
{
op
int i,n;
lo
clrscr();
tu
scanf("%d",&n);
w
w
queen(n);
w
printf("\nTotal solutions=%d",count);
getch();
}
Output: