Design and Analysis of Algorithms Lab
Design and Analysis of Algorithms Lab
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- -;
swap(&a[i],&a[j]);
}
swap(&a[i],&a[j]);
swap(&a[l],&a[j]);
return j;
}
void qsort(int a[20],int l,int r)
{
int s;
if(l<r)
{
_________________________________________________________________________
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 Sort
Enter The Value of n 5
Elements Are
17 95 15 48 26
Sorted Array is
15 17 26 48 95
Begin Time=50
End Time is=61
Total No. of clock ticks=11
Total Time Required=0.604396
_________________________________________________________________________
S T J I T Ranebennur
Design & Analysis of Algorithms Lab
Dept. of CSE
3 12 5 90 10
3 5 10 12 90
_________________________________________________________________________
S T J I T Ranebennur
Design & Analysis of Algorithms Lab
Dept. of CSE
if(k!=n)
printf("\nTopological ordering is not possible\n");
else
{
printf("The topological ordering is \n");
for(i=1;i<=k;i++)
printf("%d\t",temp[i]);
}
getch();
}
Out Put :1.
Enter the number of vertices: 5
Enter the adjacency matrix:
0
1
1
0
0
0
0
1
0
0
0
0
0
1
1
0
0
0
0
1
0
0
0
0
0
_________________________________________________________________________
S T J I T Ranebennur
Design & Analysis of Algorithms Lab
Dept. of CSE
3.b. Compute the transitive closure of a given directed graph using Warshalls
algorithm.
#include<stdio.h>
#include<conio.h>
void warshall(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(p[i][j]==0 && p[i][k]==1 && p[k][j]==1)
{
p[i][j] =1;
}
}
}
}
}
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();
}
_________________________________________________________________________
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
10
3
20
Enter the Capacity of Knapsack:3
0
0
0
0
0
0
10
10
0
0
10
20
The item in the knapsack: 3
Total Profit: 20
2)
Enter the No. of items: 4
-------------------Weight Value-------------------1
40
2
42
3
25
3
12
Enter the Capacity of Knapsack: 5
0
0
0
0
0
0
0
40
40
40
40
40
0
40
42
82
82
82
0
40
42
82
82
82
0
40
42
82
82
82
The item in the knapsack: 2
Total Profit: 82
_________________________________________________________________________
S T J I T Ranebennur
Design & Analysis of Algorithms Lab
Dept. of CSE
3
5
3
4
6
_________________________________________________________________________
S T J I T Ranebennur
Design & Analysis of Algorithms Lab
Dept. of CSE
_________________________________________________________________________
S T J I T Ranebennur
Design & Analysis of Algorithms Lab
Dept. of CSE
void main()
{
printf("Enter the number 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;
}
kruskal(n, cost);
getch();
}
Out Put :Enter the number of verteces : 4
1
2
2
5
4
1
3
4
6
Enter the cost of adjacency matrix
0
2
4
3
2
0
5
1
4
5
0
6
3
1
6
0
3
1 Edge (2,4) =1
2 Edge (1,2) =2
3 Edge (1,3) =4
The minimum cost of spanning tree is 7
2
4
3
1
4
_________________________________________________________________________
S T J I T Ranebennur
Design & Analysis of Algorithms Lab
Dept. of CSE
7.a. Print all the nodes reachable from a given starting node in a digraph using
breadth first search method.
#include<stdio.h>
#include<conio.h>
int visited[10];
void bfs(int n, int a[10][10], int source)
{
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
bfs(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();
}
Out Put :1.
Enter the number of nodes : 4
Enter the adjacency matrix
0
0
1
1
1
0
0
0
0
0
0
1
0
1
0
0
Enter the source : 1
1
A
3
C
7.b. Check weather a given graph is connected or not using DFS method.
#include<stdio.h>
#include<conio.h>
int visited[10];
void dfs(int n,int a[10][10],int source)
{
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
is Reachable
is Reachable
is Reachable
is Reachable
_________________________________________________________________________
S T J I T Ranebennur
Design & Analysis of Algorithms Lab
Dept. of CSE
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 10
int 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++)
if(x[i] ==1)
printf("%d",s[i]);
printf("\n");
}
else if (p+s[k]+s[k+1]<=d)
sumofsub(p+s[k],k+1,r-s[k]);
if((p+r-s[k]>=d) && (p+s[k+1]<=d))
{
x[k]=0;
sumofsub(p,k+1,r-s[k])
}
}
void main()
{
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
scanf("%d",&d);
for(i=1;i<=n;i++)
sum=sum+s[i];
if(sum<d || s[1]>d)
printf("\nNo subset possible");
else
sumofsub(0,1,sum);
getch();
}
Out Put :1)
Enter the max number 5
Enter the set in increasing order:
1
2
5
6
8
Enter the max subset value: 9
1
1
2
8
2)
Enter the max number 5
Enter the set in increasing order:
34569
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.
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int i,j, e,n,v1,v2,wt, G[10][10], list[10], d[30], p[30][20],r=0;
int fact(int num)
{
int i,f=1;
if (num! =0)
for(i=1;i<=num;i++)
f=f*i;
return f;
}
int min_dist(int dist[ ])
{
int min_index=0,minimum=dist[0];
for(i=0;i<fact(n-1);i++)
if(dist[i]<minimum)
{
minimum=dist[i];
min_index=i;
}
return min_index;
}
void perm(int list[], int k, int m)
{
int i, temp;
if(k==m)
{
for(i=0;i<=m;i++)
{
p[r][i+1] =list[i];
}
r++;
}
else
for(i=k;i<=m;i++)
{
temp=list[k];
list[k] =list[i];
list[i] =temp;
perm(list, k+1,m);
temp=list[k];
_________________________________________________________________________
S T J I T Ranebennur
Design & Analysis of Algorithms Lab
Dept. of CSE
list[k] =list[i];
list[i] =temp;
}
}
void sol()
{
int i,j;
perm(list,0,n-2);
for(i=0;i<fact(n-1);i++)
{
p[i][0] =0;
p[i][n] =0;
}
for(i=0;i<fact(n-1);i++)
{
d[i] =0;
for(j=0;j<n;j++)
d[i] =d[i]+G[p[i][j]][p[i][j+1]];
}
}
void print_data()
{
int i,j,pos;
printf("possible paths & costs\n");
for(i=0;i<fact(n-1);i++)
{
for(j=0;j<n+1;j++)
printf("%d\t",p[i][j] );
printf("=%d\n",d[i]);
}
printf("\nshortest path");
pos=min_dist(d);
printf("\ncost %d\n", d[pos]);
for (j=0;j<=n;j++)
printf("\n%d ",p[pos][j]);
}
void main()
{
clrscr();
printf("Enter number of nodes");
scanf("%d",&n);
printf("no. of edges");
scanf("%d",&e);
for(i=0;i<n;i++)
_________________________________________________________________________
S T J I T Ranebennur
Design & Analysis of Algorithms Lab
Dept. of CSE
for (j=0;j<n;j++)
G[i][j] =0;
for(i=0;i<e;i++)
{
printf("v1..v2..wt");
scanf("%d%d%d",&v1,&v2,&wt);
G[v2][v1] =G[v1][v2] =wt;
}
for(i=0;i<n-1;i++)
list[i] =i+1;
sol();
print_data();
getch();
}
Out Put :Enter number of nodes: 4
no. of edges :6
v1..v2..wt : 0 1
v1..v2..wt : 0 2
v1..v2..wt : 0 3
v1..v2..wt : 1 2
v1..v2..wt : 1 3
v1..v2..wt : 2 3
5
3
7
4
2
3
1
3
2
1
3
2
3
1
1
2
0
0
0
0
0
0
=13
=12
=19
=12
=13
=19
0
1
3
2
0
_________________________________________________________________________
S T J I T Ranebennur
Design & Analysis of Algorithms Lab
Dept. of CSE
10. Find minimum cost spanning tree of a given undirected graph using prims
algorithm.
#include<stdio.h>
#include<conio.h>
int mincost=0,cost[10][10],n,i,j,visited[10],ne,a,b,min,u,v;
void prims(int n, int cost[10][10])
{
ne=1;
while(ne<n)
{
min=999;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(cost[i][j]<min)
{
if(visited[i] ==0)
continue;
else
{
min=cost[i][j];
a=u=i;
b=v=j;
}
}
}
}
if(visited[u] ==1&&visited[v] ==0)
{
printf("%d Edge(%d%d) =%d\n", ne++,a ,b, min);
mincost+=min;
visited[v] =1;
}
cost[a][b] =cost[b][a] =999;
}
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
5
4
1
3
4
6
Enter the cost of adjacency matrix
0
2
4
3
2
0
5
1
4
5
0
6
3
1
6
0
1 Edge (1,2) =2
2 Edge (2,4) =1
3 Edge (1,3) =4
The minimum cost of spanning tree is 7
2
4
3
1
4
_________________________________________________________________________
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
1
4
3
6
1
#include<stdio.h>
#include<conio.h>
#include<math.h>
char q[30][30];
int count;
void display(int m[30],int n)
{
int i,j;
printf("\n______%d_____\n", count);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
q[i][j] ='X';
for(i=1;i<=n;i++)
q[i][m[i]]='Q';
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%2c",q[i][j]);
printf("\n\n");
}
printf("\n\n\n");
}
int place(int m[30], int k)
{
int i;
for(i=1;i<=k-1;i++)
{
if(m[i] ==m[k] || abs(i-k) ==abs(m[i]-m[k]))
return 0;
}
return 1;
}
void nqueens(int n)
{
int m[10],k=1;
m[k] =0;
while(k!=0)
{
m[k] =m[k]+1;
_________________________________________________________________________
S T J I T Ranebennur
Design & Analysis of Algorithms Lab
Dept. of CSE