design and analysis of algorithms lab

38
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

Upload: rajendrayalaburgi

Post on 20-Dec-2015

13 views

Category:

Documents


0 download

DESCRIPTION

4th sem VTU

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.

#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 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){

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

#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: 5Enter the adjacency matrix:0 1 1 0 00 0 1 0 00 0 0 1 10 0 0 0 10 0 0 0 0

The topological ordering is:1 2 4 5 3

2.Enter the number of vertices: 5Enter the adjacency matrix:0 1 1 0 00 0 1 0 00 0 0 1 00 0 0 0 10 0 1 0 0

Topological ordering is not possible

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

A

E

C

B

D

E

D

C

B

A

3.b. Compute the transitive closure of a given directed graph using Warshall’s 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 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

#include<stdio.h>#include<conio.h>int max(int a,int b){

if(a>b)return a;

elsereturn 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 103 20Enter the Capacity of Knapsack:30 0 0 00 0 10 100 0 10 20

The item in the knapsack: 3Total Profit: 20

2)Enter the No. of items: 4-------------------Weight Value--------------------1 402 423 253 12Enter the Capacity of Knapsack: 50 0 0 0 0 00 40 40 40 40 400 40 42 82 82 820 40 42 82 82 820 40 42 82 82 82

The item in the knapsack: 2 1Total 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 Dijkstra’s 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=999for(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

2

3 5 4

6

Enter the cost of adjacency matrix0 2 0 00 0 0 35 0 0 04 0 6 0

Enter the source vertex : 2

Shortest path from 2 to 1= 7Shortest 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

1 2

43

6. Find minimum cost spanning tree of a given undirected graph using Kruskal’s 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 : 42 5

4 1

36

Enter the cost of adjacency matrix0 2 4 32 0 5 14 5 0 63 1 6 0

1 Edge (2,4) =12 Edge (1,2) =23 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

1 2

43

1 2

3 4

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 : 4Enter the adjacency matrix0 0 1 11 0 0 00 0 0 10 1 0 0Enter the source : 1

The node 1 is reachableThe node 2 is reachableThe node 3 is reachableThe node 4 is reachable

2.Enter the number of nodes 4Enter the adjacency matrix0 1 1 00 0 0 00 0 0 00 1 0 0Enter the source 1

The node 1 is reachableThe node 2 is reachableThe node 3 is reachableThe node 4 is not reachable

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

1A

3C

2

4

1

3

2

4

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 : 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++)

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

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

v1..v2..wt : 0 2 5 5

v1..v2..wt : 0 3 3 3 7

v1..v2..wt : 1 2 7 4

v1..v2..wt : 1 3 4 1

v1..v2..wt : 2 3 1

possible paths & costs:

0 1 2 3 0 =130 1 3 2 0 =120 2 1 3 0 =190 2 3 1 0 =120 3 2 1 0 =130 3 1 2 0 =19shortest pathcost 12

01320

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

0

23

1

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

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.

#include<stdio.h>#include<omp.h>#define INFINITY 999int min(int a,int b){

if(a<b)return a;

elsereturn 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

4 3

5 6 1

2

Enter the adjacency cost matrix0 4 5 999999 0 3 6999 999 0 21 999 999 0

All pair shortest path matrix0 4 5 76 0 3 53 7 0 21 5 6 0

The time taken is 0.000204640

12. Implement N Queen’s problem using back tracking.

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

43

21

#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 XX X X QQ X X XX X Q X______2_____X X Q XQ X X XX X X QX Q X X

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