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

analysis and design algorithm Manual

The document provides a series of C/C++ programs for various algorithms including Kruskal's and Prim's for Minimum Cost Spanning Trees, Floyd's algorithm for All-Pairs Shortest Paths, Warshall's algorithm for transitive closure, Dijkstra's algorithm for shortest paths, Topological ordering for directed graphs, and a Dynamic Programming approach for the 0/1 Knapsack problem. Each section includes code snippets, input prompts, and sample outputs demonstrating the functionality of the algorithms. The document serves as a practical guide for implementing these algorithms in programming.

Uploaded by

achaithana35
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

analysis and design algorithm Manual

The document provides a series of C/C++ programs for various algorithms including Kruskal's and Prim's for Minimum Cost Spanning Trees, Floyd's algorithm for All-Pairs Shortest Paths, Warshall's algorithm for transitive closure, Dijkstra's algorithm for shortest paths, Topological ordering for directed graphs, and a Dynamic Programming approach for the 0/1 Knapsack problem. Each section includes code snippets, input prompts, and sample outputs demonstrating the functionality of the algorithms. The document serves as a practical guide for implementing these algorithms in programming.

Uploaded by

achaithana35
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

ANALYSIS & DESIGN OF ALGORITHM BCSL404

1. Design and implement C/C++ Program to find Minimum Cost Spanning Tree of a given
connected undirected graph using Kruskal's algorithm.

Code:

#include<stdio.h>

#include<conio.h>

int parent[10];

int main()

int mincost=0,cost[10][10],min,ne,a,b,n,i,j,u,v;

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

scanf("%d",&n);

printf("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]=999;

ne=1;

while(ne<n)

for(min=999,i=1;i<=n;i++)

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

Dept. of Computer Science & Engg, STJIT, Ranebennur. Page 1


ANALYSIS & DESIGN OF ALGORITHM BCSL404

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("The maximum cost of spanning tree is :%d",mincost);

getch();

Dept. of Computer Science & Engg, STJIT, Ranebennur. Page 2


ANALYSIS & DESIGN OF ALGORITHM BCSL404

Output:

Enter the number of vertices:

Enter the cost matrix:

0 1 7 11

1093

7905

11 3 5 0

1 Edge(1,2)=1

2 Edge(2,4)=3

3 Edge(3,4)=5

The maximum cost of spanning tree is :9

Dept. of Computer Science & Engg, STJIT, Ranebennur. Page 3


ANALYSIS & DESIGN OF ALGORITHM BCSL404

2. Design and implement C/C++ Program to find Minimum Cost Spanning Tree of a given
connected undirected graph using Prim's algorithm.

Code:

#include<stdio.h>

#include<conio.h>

int main()

int mincost=0,cost[10][10],visited[10],min,ne,a,b,n,i,j,u,v;

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

scanf("%d",&n);

printf("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]=999;

visited[1]=1;

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

visited[i]=0;

ne=1;

Dept. of Computer Science & Engg, STJIT, Ranebennur. Page 4


ANALYSIS & DESIGN OF ALGORITHM BCSL404

while(ne<n)

for(min=999,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("The minimum cost for spanning tree is : %d",mincost);

getch();

Dept. of Computer Science & Engg, STJIT, Ranebennur. Page 5


ANALYSIS & DESIGN OF ALGORITHM BCSL404

Output:

Enter the number of vertices:

Enter the cost matrix:

0219

2 0 12 18

1 12 0 5

9850

1 Edge(1,3)=1

2 Edge(1,2)=2

3 Edge(3,4)=5

The minimum cost for spanning tree is : 8

Dept. of Computer Science & Engg, STJIT, Ranebennur. Page 6


ANALYSIS & DESIGN OF ALGORITHM BCSL404

3. a. Design and implement C/C++ Program to solve All-Pairs Shortest Paths problem using
Floyd's algorithm.
b. Design and implement C/C++ Program to find the transitive closure using Warshal's
algorithm.

Code 3a:

#include<stdio.h>

#define INFINITY 999

int min(int i,int j)

if(i<j) return i;

else return j;

int floyd(int n,int p[10][10])

int i,j,k;

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

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

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

p[i][j]=min(p[i][j],p[i][k]+p[k][j]);

int main()

int i,j,n,a[10][10];

printf("Enter the no of nodes:\n");

scanf("%d",&n);

printf("Enter the adj matrix:\n");

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

Dept. of Computer Science & Engg, STJIT, Ranebennur. Page 7


ANALYSIS & DESIGN OF ALGORITHM BCSL404

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

scanf("%d",&a[i][j]);

floyd(n,a);

printf("The distance matrix is:\n");

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

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

printf("%d\t",a[i][j]);

printf("\n");}

return 0;

Dept. of Computer Science & Engg, STJIT, Ranebennur. Page 8


ANALYSIS & DESIGN OF ALGORITHM BCSL404

Output:

Enter the no of nodes:

Enter the adj matrix:

0 15 8 10 999

15 0 4 999 999

8 4 0 999 12

10 999 999 0 7

999 999 12 7 0

The distance matrix is:

0 12 8 10 17

12 0 4 22 16

8 4 0 18 12

10 22 18 0 7

17 16 12 7 0

Dept. of Computer Science & Engg, STJIT, Ranebennur. Page 9


ANALYSIS & DESIGN OF ALGORITHM BCSL404

Code 3b:

#include<stdio.h>

#include<conio.h>

void warshall(int c[10][10],int n);

void main()

int i,j,cost[10][10],n;

printf("Enter the no of vertices:\n");

scanf("%d",&n);

printf("Enter the matrix:\n");

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

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

scanf("%d",&cost[i][j]);

warshall(cost,n);

printf("Transitive Closure is:\n");

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

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

printf("%d\t",cost[i][j]);

printf("\n");

getch();

Dept. of Computer Science & Engg, STJIT, Ranebennur. Page 10


ANALYSIS & DESIGN OF ALGORITHM BCSL404

void warshall(int c[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++)

c[i][j]=(c[i][j]||c[i][k]&&c[k][j]);

Dept. of Computer Science & Engg, STJIT, Ranebennur. Page 11


ANALYSIS & DESIGN OF ALGORITHM BCSL404

Output:

Enter the no of vertices:

Enter the matrix:

0100

0001

0000

1010

Transitive Closure is:

1 1 1 1

1 1 1 1

0 0 0 0

1 1 1 1

Dept. of Computer Science & Engg, STJIT, Ranebennur. Page 12


ANALYSIS & DESIGN OF ALGORITHM BCSL404

4. Design and implement C/C++ Program to find shortest paths from a given vertex in a
weighted connected graph to other vertices using Dijkstra's algorithm.

Code:

#include<stdio.h>

#include<conio.h>

void dijkstras(int cost[10][10],int dist[10],int n,int v)

int i,u,w,flag[10],min,count;

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

flag[i]=0;

dist[i]=cost[v][i];

flag[v]=1;

dist[v]=1;

count=2;

while(count<n)

for(i=1,min=999;i<=n;i++)

if(dist[i]<min && !flag[i])

min=dist[i];

v=i;

flag[v]=i;

count++;

Dept. of Computer Science & Engg, STJIT, Ranebennur. Page 13


ANALYSIS & DESIGN OF ALGORITHM BCSL404

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

if(dist[v]+cost[v][w]<dist[w]&&!flag[w])

dist[w]=dist[v]+cost[v][w];

void main()

int i,j,source,n,dist[10],cost[10][10];

printf("Enter the no of vertices:\n");

scanf("%d",&n);

printf("Enter the 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:\n");

scanf("%d",&source);

dijkstras(cost,dist,n,source);

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

Dept. of Computer Science & Engg, STJIT, Ranebennur. Page 14


ANALYSIS & DESIGN OF ALGORITHM BCSL404

if(source!=i)

printf("%d-->%d::%d\n",source,i,dist[i]);

getch();

Output:

Enter the no of vertices:

Enter the matrix:

0200

0035

0000

4030

Enter the source:

4-->1::4

4-->2::6

4-->3::3

Dept. of Computer Science & Engg, STJIT, Ranebennur. Page 15


ANALYSIS & DESIGN OF ALGORITHM BCSL404

5. Design and implement C/C++ Program to obtain the Topological ordering of vertices in a
given digraph.

Code:

#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,a[10][10],indegree[10];

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

Dept. of Computer Science & Engg, STJIT, Ranebennur. Page 16


ANALYSIS & DESIGN OF ALGORITHM BCSL404

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

if(k!=n)

printf("Topological ordering is not possible\n");

else

printf("The Topological ordering is\n");

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

printf("%d\t",temp[i]);

getch();

Dept. of Computer Science & Engg, STJIT, Ranebennur. Page 17


ANALYSIS & DESIGN OF ALGORITHM BCSL404

Output:

Enter the number of vertices:

Enter the Adjacency matrix:

00100

00100

00011

00000

00000

The Topological ordering is

1 2 3 4 5

Dept. of Computer Science & Engg, STJIT, Ranebennur. Page 18


ANALYSIS & DESIGN OF ALGORITHM BCSL404

6. Design and implement C/C++ Program to solve 0/1 Knapsack problem using Dynamic
Programming method.

Code:

#include<stdio.h>

#include<conio.h>

int max(int a,int b)

if(a>b) return a;

else return b;

void main()

int i,j,n,capacity;

int w,wt[20],ve[20],v[20][20];

printf("Enter the number of items:\n");

scanf("%d",&n);

printf("\n-----\nWeight values\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++)

Dept. of Computer Science & Engg, STJIT, Ranebennur. Page 19


ANALYSIS & DESIGN OF ALGORITHM BCSL404

if(i==0 || j==0)

v[i][j]=0;

else if(j-wt[i]>=0)

v[i][j]=max(v[i-1][j],v[i-1][j-wt[i]]+ve[i]);

else

v[i][j]=v[i-1][j];

printf("%4d",v[i][j]);

printf("\n");

w=capacity;

printf("The items in the Knapsack are :\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("\nTotal profit: %d",v[n][capacity]);

Dept. of Computer Science & Engg, STJIT, Ranebennur. Page 20


ANALYSIS & DESIGN OF ALGORITHM BCSL404

getch();

Output:

Enter the number of items:

-----

Weight values

-----

10

20

Enter the Capacity of Knapsack :

0 0 0 0

0 0 10 10

0 0 10 20

The items in the Knapsack are :

Total profit: 20

Dept. of Computer Science & Engg, STJIT, Ranebennur. Page 21


ANALYSIS & DESIGN OF ALGORITHM BCSL404

7. Design and implement C/C++ Program to solve discrete Knapsack and continuous
Knapsack problems using greedy approximation method.

Code:

#include<stdio.h>

int main()

float weight[50],profit[50],ratio[50],Totalvalue,temp,capacity,amount;

int n,i,j;

printf("Enter the number of items: ");

scanf("%d",&n);

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

printf("Enter Weight and Profit for item[%d]:\n",i);

scanf("%f %f",&weight[i],&profit[i]);

printf("Enter the capacity of knapscak:\n");

scanf("%f",&capacity);

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

ratio[i]=profit[i]/weight[i];

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

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

if(ratio[i]<ratio[j])

temp=ratio[i];

ratio[j]=ratio[i];

ratio[i]=temp;

temp=weight[j];

weight[j]=weight[i];

Dept. of Computer Science & Engg, STJIT, Ranebennur. Page 22


ANALYSIS & DESIGN OF ALGORITHM BCSL404

weight[i]=temp;

temp=profit[j];

profit[j]=profit[i];

profit[i]=temp;

printf("Knapscaks problem using Greedy algorithm:\n");

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

if(weight[i]>capacity)

break;

else

Totalvalue=Totalvalue+profit[i];

capacity=capacity-weight[i];

if(i<n)

Totalvalue=Totalvalue + (ratio[i]*capacity);

printf("\nThe maximum value is : %f\n",Totalvalue);

return 0;

Dept. of Computer Science & Engg, STJIT, Ranebennur. Page 23


ANALYSIS & DESIGN OF ALGORITHM BCSL404

Output:

Enter the number of items: 4

Enter Weight and Profit for item[0]:

12

Enter Weight and Profit for item[1]:

10

Enter Weight and Profit for item[2]:

20

Enter Weight and Profit for item[3]:

15

Enter the capacity of knapscak:

Knapscaks problem using Greedy algo:

The maximum value is : 37.000000

Dept. of Computer Science & Engg, STJIT, Ranebennur. Page 24


ANALYSIS & DESIGN OF ALGORITHM BCSL404

8. Design and implement C/C++ Program to find a subset of a given set S = {sl , s2,.....,sn}
of n positive integers whose sum is equal to a given positive integer d.

Code:

#include<stdio.h>

#include<conio.h>

#define max 10

int s[max],x[max],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\t",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]);

Dept. of Computer Science & Engg, STJIT, Ranebennur. Page 25


ANALYSIS & DESIGN OF ALGORITHM BCSL404

void main()

int i,n,sum=0;

printf("Enter the max no:\n");

scanf("%d",&n);

printf("Enter the subset in increasing order:\n");

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

scanf("%d",&s[i]);

printf("Enter the max subset:\t");

scanf("%d",&d);

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

sum=sum+s[i];

if(sum<d||s[1]>d)

printf("No possible subset\n");

else

sumofsub(0,1,sum);

getch();

Dept. of Computer Science & Engg, STJIT, Ranebennur. Page 26


ANALYSIS & DESIGN OF ALGORITHM BCSL404

Output:

Enter the max no:

Enter the subset in increasing order:

12568

Enter the max subset: 9

1 2 6

1 8

Enter the max no:

Enter the subset in increasing order:

12568

Enter the max subset: 0

No possible subset

Dept. of Computer Science & Engg, STJIT, Ranebennur. Page 27


ANALYSIS & DESIGN OF ALGORITHM BCSL404

9. Design and implement C/C++ Program to sort a given set of n integer elements using
Selection Sort method and compute its time complexity. Run the program for varied
values of n> 5000 and record the time taken to sort. 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.

Code:

#include <stdio.h>

void swap(int *a, int *b) {

int temp = *a;

*a = *b;

*b = temp;

void selectionSort(int array[], int size) {

for (int step = 0; step < size - 1; step++) {

int min_idx = step;

for (int i = step + 1; i < size; i++)

if (array[i] < array[min_idx])

min_idx = i;

swap(&array[min_idx], &array[step]);

void printArray(int array[], int size) {

for (int i = 0; i < size; ++i) {

Dept. of Computer Science & Engg, STJIT, Ranebennur. Page 28


ANALYSIS & DESIGN OF ALGORITHM BCSL404

printf("%d ", array[i]);

printf("\n");

int main() {

int data[] = {20, 12, 10, 15, 2};

int size = sizeof(data) / sizeof(data[0]);

selectionSort(data, size);

printf("Sorted array in Acsending Order:\n");

printArray(data, size);

Output:

Sorted array in Acsending Order:

2 10 12 15 20

Dept. of Computer Science & Engg, STJIT, Ranebennur. Page 29


ANALYSIS & DESIGN OF ALGORITHM BCSL404

10. Design and implement C/C++ Program to sort a given set of n integer elements using
Quick Sort method and compute its time complexity. Run the program for varied values
of n> 5000 and record the time taken to sort. 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.

Code:

#include<stdio.h>

#include<conio.h>

#include<time.h>

void swap(int *x,int *y)

int temp = *x;

*x = *y;

*y = temp;

int partition(int a[],int l,int r)

int i,j;

int p;

p=a[l];

i=l+1;

j=r;

while(i<=j)

while(a[i]<=p)

i++;

while(a[j]>=p)

Dept. of Computer Science & Engg, STJIT, Ranebennur. Page 30


ANALYSIS & DESIGN OF ALGORITHM BCSL404

j++;

swap(&a[i],&a[j]);

swap(&a[i],&a[j]);

swap(&a[l],&a[j]);

return j;

void qsort(int a[],int l,int r)

int s;

if(l<r)

s=partition(a,l,r);

qsort(a,l,s-1);

qsort(a,s+1,r);

void main()

int a[8000],i,j,n;

clock_t begin,end;

printf("\nQuick Sort");

printf("\n\n");

printf("Enter the value of n: ");

scanf("%d",&n);

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

Dept. of Computer Science & Engg, STJIT, Ranebennur. Page 31


ANALYSIS & DESIGN OF ALGORITHM BCSL404

scanf("The array elements are: \t%d",a[i]=rand()%100);

printf("Elements are: ");

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

printf("\t%d",a[i]=rand()%100);

begin=clock();

qsort(a,1,n-1);

end=clock();

printf("\nSorted Array is: ");

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

printf("\t%d",a[i]);

printf("\n\n");

printf("Begin time: %d\n",begin);

printf("End time: %d\n",end);

printf("Total no of clock ticks: %d\n",(end-begin));

printf("Total time request = %f",((end-begin)/(CLK_TCK)));

getch();

Dept. of Computer Science & Engg, STJIT, Ranebennur. Page 32


ANALYSIS & DESIGN OF ALGORITHM BCSL404

Output:

Quick Sort

Enter the value of n: 5

Elements are: 24 78 58 62 64

Sorted Array is: 24 58 62 64 78

Begin time: 1419

End time: 1419

Total no of clock ticks: 0

Total time request = 0.000000

Dept. of Computer Science & Engg, STJIT, Ranebennur. Page 33


ANALYSIS & DESIGN OF ALGORITHM BCSL404

11. Design and implement C/C++ Program to sort a given set of n integer elements using
Merge Sort method and compute its time complexity. Run the program for varied values
of n> 5000, and record the time taken to sort. 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.

Code:

#include<stdio.h>

#include<stdlib.h>

#include<time.h>

#include<omp.h>

void merge(int a[],int low,int mid,int high)

int b[100],i,j,k;

i=low;

j=mid+1;

k=low;

while(i<=mid && j<=high)

if(a[i]<a[j])

b[k]=a[i];

k++;

i++;

else

b[k]=a[j];

Dept. of Computer Science & Engg, STJIT, Ranebennur. Page 34


ANALYSIS & DESIGN OF ALGORITHM BCSL404

k++;

j++;

while(i<=mid)

b[k]=a[i];

k++;

i++;

while(j<=high)

b[k]=a[j];

k++;

j++;

for(i=low;i<=high;i++)

a[i]=b[i];

void mergesort(int a[],int low,int high)

int mid;

if(low<high)

mid=(low+high)/2;

Dept. of Computer Science & Engg, STJIT, Ranebennur. Page 35


ANALYSIS & DESIGN OF ALGORITHM BCSL404

#pragma omp parallel sections

#pragma omp section

mergesort(a,low,mid);

#pragma omp section

mergesort(a,mid+1,high);

merge(a,low,mid,high);

void main()

int a[100],i,n;

clock_t starttime,endtime;

printf("Enter the no of elements:\n");

scanf("%d",&n);

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

a[i]=rand()%100;

printf("The element are:\n");

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

printf("%d\t",a[i]);

starttime=clock();

mergesort(a,0,n-1);

endtime=clock();

Dept. of Computer Science & Engg, STJIT, Ranebennur. Page 36


ANALYSIS & DESIGN OF ALGORITHM BCSL404

printf("\nSorted array is: \n");

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

printf("%d\t",a[i]);

printf("\nTime required = %1f\n",(double)(endtime-starttime));

Output:

Enter the no of elements:

The element are:

41 67 34 0 69

Sorted array is:

0 34 41 67 69

Time required = 0.000000

Dept. of Computer Science & Engg, STJIT, Ranebennur. Page 37


ANALYSIS & DESIGN OF ALGORITHM BCSL404

12. Design and implement C/C++ Program for N Queen's problem using Backtracking.

Code:

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

char s[30][30],count=0;

void display(int m[30],int n)

int i,j;

printf("\n_____%d_____\n",++count);

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

for(j=0;j<n;j++)

s[i][j]='X';

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

s[i][m[i]]='Q';

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

for(j=0;j<n;j++)

printf("%2c",s[i][j]);

printf("\n\n");

printf("\n\n\n");

getch();

int place(int m[30],int k)

Dept. of Computer Science & Engg, STJIT, Ranebennur. Page 38


ANALYSIS & DESIGN OF ALGORITHM BCSL404

int i;

for(i=0;i<k;i++)

if(m[k]==m[i]||(abs(m[i]-m[k])==abs(i-k)))

return 0;

return 1;

int main()

int n,k=0,i;

int m[30];

printf("Enter the number of queens:\n");

scanf("%d",&n);

if(n==2||n==3)

printf("No solutions\n");

exit(0);

n--;

for(m[0]=0;k>=0;m[k]++)

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

m[k]++;

if(m[k]<=n)

if(k==n)

Dept. of Computer Science & Engg, STJIT, Ranebennur. Page 39


ANALYSIS & DESIGN OF ALGORITHM BCSL404

display(m,n+1);

else k++,m[k]=-1;

else

k--;

Output:

Enter the number of queens:

_____1_____

XQXX

XXXQ

QXXX

XXQX

_____2_____

XXQX

QXXX

XXXQ

XQXX

Dept. of Computer Science & Engg, STJIT, Ranebennur. Page 40

You might also like