0% found this document useful (0 votes)
53 views

Design and Analysis of Algorithms Lab

1. The document describes implementing Kruskal's algorithm to find the minimum spanning tree of an undirected graph. It includes code to initialize parent arrays, find minimum cost edges between vertices, update the parent vertex and print the minimum spanning tree. 2. It then provides code to implement Dijkstra's algorithm to find the shortest paths from a source vertex to all other vertices in a weighted connected graph. It initializes distance arrays, finds the minimum distance vertex, updates distances and prints the shortest paths. 3. Finally, it shows code for Warshall's algorithm to compute the transitive closure of a directed graph by iterating through vertices and updating the adjacency matrix.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Design and Analysis of Algorithms Lab

1. The document describes implementing Kruskal's algorithm to find the minimum spanning tree of an undirected graph. It includes code to initialize parent arrays, find minimum cost edges between vertices, update the parent vertex and print the minimum spanning tree. 2. It then provides code to implement Dijkstra's algorithm to find the shortest paths from a source vertex to all other vertices in a weighted connected graph. It initializes distance arrays, finds the minimum distance vertex, updates distances and prints the shortest paths. 3. Finally, it shows code for Warshall's algorithm to compute the transitive closure of a directed graph by iterating through vertices and updating the adjacency matrix.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 29

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- -;
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

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)
{
int b[30],i,j,k;
i=l; j=mid+1;
k=l;
while(i<=mid && j<= r)
{
if(a[i]<a[j])
{
b[k] =a[i];
k++; i++;
}
else
{
b[k]=a[j];
k++; j++;
}
}
while(i<=mid)
{
b[k]=a[i];
k++; i++;
}
while(j<= r)
{
b[k]=a[j];
k++; j++;
}
for(i=l; i<= r; i++)
{
a[i]=b[i];
}
}
_________________________________________________________________________
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 section
mergesort(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: 5
The elements before sorting are
The sorted elements are:
The time taken is 0.102453

3 12 5 90 10
3 5 10 12 90

_________________________________________________________________________
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.


#include<stdio.h>
#include<conio.h>
int temp[10],k=0;
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++)
{
if(a[i][j] ==1 && indegree[j]! = -1)
indegree[j]--;
}
i=0;
}
}
}
void main()
{
int i, j, n, indegree[10], a[10][10];
clrscr();
printf("Enter the number of vertices: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
indegree[i] =0;
printf("Enter the adjacency matrix\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&a[i][j]);
if(a[i][j] ==1)
indegree[j]++;
}
topo(n,indegree,a);
_________________________________________________________________________
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

The topological ordering is:


1
2
4
5
3
2.
Enter the number of vertices: 5
Enter the adjacency matrix:
0
1
1
0
0
0
0
1
0
0
0
0
0
1
0
0
0
0
0
1
0
0
1
0
0

Topological ordering is not possible

_________________________________________________________________________
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();
}

Out Put :Enter the number .of vertices 4


Enter the adjacency matrix:
0
1
0
0
0
0
0
1
0
0
0
0
1
0
1
0
The Transitive Closure of given graph is :
1
1
1
1
1
1
1
1
0
0
0
0
1
1
1
1

_________________________________________________________________________
S T J I T Ranebennur
Design & Analysis of Algorithms Lab
Dept. of CSE

4. Implement 0/1 Knapsack problem using Dynamic Programming


#include<stdio.h>
#include<conio.h>
int max(int a,int b)
{
if(a>b)
return a;
else
return b;
}
void main()
{
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
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

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


other vertices using Dijkstras algorithm.
#include<stdio.h>
#include<conio.h>
void dijkstras(int cost[10][10], int dist[10], int n, int v)
{
int i,w,ne=2,visited[10],min;
for(i=1;i<=n;i++)
{
visited [i] =0;
dist[i] =cost[v][i];
}
visited[v] =1;
dist[v] =0;
ne=2;
while(ne<n)
{
min=999
for(i=1;i<=n;i++)
{
if(dist[i]<min && visited[i]==0)
{
min=dist[i];
v=i;
}
}
visited [v] =1;
ne++;
for(w=1;w<=n;w++)
{
if(dist[v]+cost[v][w]<dist[w] && visited[w] ==0)
dist[w] =dist[v]+cost[v][w];
}
}
}
void main()
{
int n,cost[10][10],source, i,j,dist[10];
clrscr();
_________________________________________________________________________
S T J I T Ranebennur
Design & Analysis of Algorithms Lab
Dept. of CSE

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;
}
printf("Enter the source vertex : \n");
scanf("%d",& source);
dijkstras(cost, dist, n, source);
for(i=1;i<=n;i++)
if(source! =i)
printf("Shortest path from %d to %d= %d\n", source, i, dist[i]);
getch();
}
Out Put :Enter the number of vertices : 4
1

3
5
3

4
6

Enter the cost of adjacency matrix


0
2
0
0
0
0
0
3
5
0
0
0
4
0
6
0
Enter the source vertex : 2
Shortest path from 2 to 1= 7
Shortest path from 2 to 3= 9
Shortest path from 2 to 4= 3

_________________________________________________________________________
S T J I T Ranebennur
Design & Analysis of Algorithms Lab
Dept. of CSE

6. Find minimum cost spanning tree of a given undirected graph using


Kruskals algorithm.
#include<stdio.h>
#include<conio.h>
int parent[10], mincost=0, cost[10][10], n, i, j, ne, a, b, min, u, v;
void kruskal(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)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
}
}
while(parent[u])
u=parent[u];
while(parent[v])
v=parent[v];
if(v! =u)
{
printf("%d Edge (%d,%d) =%d\n",ne++,a,b,min);
mincost+=min;
parent[v] =u;
}
cost[a][b] =cost[b][a] =999;
}
printf("\n The minimum cost of spanning tree is %d\n",mincost);
}

_________________________________________________________________________
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

The node 1 is reachable


The node 2 is reachable
The node 3 is reachable
The node 4 is reachable
2.
Enter the number of nodes 4
Enter the adjacency matrix
0
1
1
0
0
0
0
0
0
0
0
0
0
1
0
0
Enter the source 1

1
A

3
C

The node 1 is reachable


The node 2 is reachable
The node 3 is reachable
The node 4 is not reachable
_________________________________________________________________________
S T J I T Ranebennur
Design & Analysis of Algorithms Lab
Dept. of CSE

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

Out Put :1.


Enter the number of nodes : 3
Enter the adjacency Matrix
0
1
1
0
0
0
1
1
0
Enter The Source Vertex: 3

The node 1 is Reachable


The node 2 is Reachable
The node 3 is Reachable
2.
Enter the number of nodes : 4
Enter the adjacency Matrix
0
1
1
1
0
0
0
1
0
0
0
1
0
0
0
0
Enter The Source Vertex:1
The node 1
The node 2
The node 3
The node 4

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

possible paths & costs:


0
1
0
1
0
2
0
2
0
3
0
3
shortest path
cost 12

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

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
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

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<omp.h>
#define INFINITY 999
int min(int a,int b)
{
if(a<b)
return a;
else
return b;
}
void floyd(int n, int cost[10][10])
{
int i,j,k;
#pragma omp parallel for private(i,j,k) shared(cost)
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
cost[i][j] =min(cost[i][j], cost[i][k]+ cost[k][j]);
}
int main()
{
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
1

4
3

6
1

Enter the adjacency cost matrix


0
4
5
999
999 0
3
6
999 999 0
2
1
999 999 0
All pair shortest path matrix
0
4
5
7
6
0
3
5
3
7
0
2
1
5
6
0
The time taken is 0.000204640

12. Implement N Queens problem using back tracking.


_________________________________________________________________________
S T J I T Ranebennur
Design & Analysis of Algorithms Lab
Dept. of CSE

#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

while(m[k]< =n && place(m,k) ==0)


{
m[k] =m[k]+1;
}
if(m[k]< =n)
{
if(k==n)
{
count++;
display(m,n);
getch();
}
else
k++,m[k] =0;
}
else
k--;
}
}
void main()
{
int n;
clrscr();
printf("Enter the number of queens\n");
scanf("%d",&n);
if(n==2||n==3)
{
printf("no solutions\n");
getch();
exit(0);
}
else
nqueens(n);
}
Out Put :Enter the number of queens 4
______1_____
X
Q
X
X
X
X
X
Q
Q
X
X
X
X
X
Q
X
______2_____
X
X
Q
X
Q
X
X
X
X
X
X
Q
X
Q
X
X
_________________________________________________________________________
S T J I T Ranebennur
Design & Analysis of Algorithms Lab
Dept. of CSE

You might also like