0% found this document useful (0 votes)
25 views47 pages

Daa File

The document discusses implementing various algorithms related to design and analysis of algorithms. It includes implementing quicksort, merge sort, topological sorting, transitive closure, knapsack problem and Dijkstra's algorithm.

Uploaded by

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

Daa File

The document discusses implementing various algorithms related to design and analysis of algorithms. It includes implementing quicksort, merge sort, topological sorting, transitive closure, knapsack problem and Dijkstra's algorithm.

Uploaded by

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

Practical file

Design and Analysis of Algorithms


Submitted in partial fulfilment of the requirement for the
award of the degree
of
Technolog
y in
COMPUTER SCIENCE AND ENGINEERING (ARTIFICIAL
INTELLIGENCE / MACHINE LEARNING)
To
Shivani
Sharma
(Skill Assistant Professor, CSE Department)

By
Neeraj(22BTC35140)

Department of Skill Faculty of Engineering &


Technology Shri Vishwakarma Skill University
Dudhola, Palwal
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 <stdlib.h>
#include <sys/time.h>
#include <time.h>
void fnGenRandInput(int [],
int); void fnDispArray( int [],
int);
int fnPartition(int [], int , int );
void fnQuickSort(int [], int , int
); void fnSwap(int*, int*);
void fnSwap(int *a, int *b)
{
int t = *a; *a = *b; *b = t;
}
int main( int argc, char **argv)
{

FILE *fp;
struct timeval tv;
double
dStart,dEnd;
int iaArr[500000],iNum,iPos,iKey,i,iChoice;

for(;;)
{

printf(“name=Neeraj(22BTC35140)\n”); printf("\
n1.Plot the Graph\n2.QuickSort\n3.Exit");
printf("\nEnter your choice\n");
scanf("%d",&iChoice);

switch(iChoice)
{
case 1:
fp = fopen("QuickPlot.dat","w");

for(i=100;i<100000;i+=100)
{
fnGenRandInput(iaArr,i);

gettimeofday(&tv,NULL);
dStart = tv.tv_sec + (tv.tv_usec/1000000.0);

fnQuickSort(iaArr,0,i-1);

gettimeofday(&tv,NULL);
dEnd = tv.tv_sec + (tv.tv_usec/1000000.0);

fprintf(fp,"%d\t%lf\n",i,dEnd-dStart);

}
fclose(fp);
printf("\nData File generated and stored in file < QuickPlot.dat >.\n Use a
plotting utility\n");
break;

case 2:
printf("\nEnter the number of elements to sort\
n"); scanf("%d",&iNum);
printf("\nUnsorted Array\
n");
fnGenRandInput(iaArr,iNum)
; fnDispArray(iaArr,iNum);
fnQuickSort(iaArr,0,iNum-1);
printf("\nSorted Array\n");
break;

case 3:
exit(0);
}
}

return 0;
}
int fnPartition(int a[], int l, int r)
{
int i,j,temp;
int p;

p = a[l];
i = l;
j = r+1;
do
{
do { i++; } while (a[i] < p);
do { j--; } while (a[j] > p);

fnSwap(&a[i], &a[j]);
}
while (i<j);

fnSwap(&a[i], &a[j]);
fnSwap(&a[l], &a[j]);

return j;
}
void fnQuickSort(int a[], int l, int r)
{
int s;

if (l < r)
{
s = fnPartition(a, l, r);
fnQuickSort(a, l, s-1);
fnQuickSort(a, s+1,
r);
}
}
void fnGenRandInput(int X[], int n)
{
int i;
srand(time(NULL));
for(i=0;i<n;i++)
{
X[i] = rand()%10000;
}

}
void fnDispArray( int X[], int n)
{
int i;

for(i=0;i<n;i++)
printf(" %5d \n",X[i]);

Output:
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<omp.h>
#include<stdio.h>
#include<time.h>
void merge(int a[],int low,int mid,int high)
{
int c[10000],i,j,k;
i=low;
j=mid+1;
k=low;
{
while((i<=mid)&&(j<=high))
{
if(a[i]<=a[j])
c[k++]=a[i++];
else c[k+
+]=a[j++];
}
while(i<=mid)
c[k++]=a[i++];
while(j<=high)
c[k++]=a[j++];
for(i=low;i<=high;i++)
a[i]=c[i];
}
}
void mergesort(int a[],int low,int high)
{
int mid;
if(low<high)
{
#pragma omp parallel sections num_threads(5)
{
mid=(low+high)/2;
#pragma omp section
mergesort(a,low,mid);
#pragma omp section
mergesort(a,mid+1,high);
#pragma omp section
merge(a,low,mid,high);
}
}
}
int main()
{
int a[10000],i,n;
clock_t st,end;

printf(“name=Neeraj(22BTC35140)\n”);
printf("enter number of elements to sort\
n"); scanf("%d",&n);
for(i=0;i<n;i++)
a[i]=rand()%100;
printf("the elements are\
n",n); for(i=0;i<n;i++)
printf("%d\n",a[i]);
st=clock();
mergesort(a,0,n-1);
end=clock();
printf("\n sorted elements are\
n"); for(i=0;i<n;i++)
printf("%d\n",a[i]);
printf("\n time taken=%f \n",(end-st)/(double)CLOCKS_PER_SEC);
}

Output:

3. Obtain the Topological ordering of vertices in a given digraph.


#include<stdio.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()
{
int i,u,v,t[10],s[10],top=-1,k=0;
find_indegre(); for(i=0;i<n;i+
+)
{
if(indegre[i]==0)
s[++top]=i;
}
while(top!=-1)
{
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(“name=Neeraj(22BTC35140)\n”);
printf("The topological Sequence is:\
n"); for(i=0;i<n;i++)
printf("%d ",t[i]);
}
void main()
{
int i,j;
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();
}

Output:
4. Compute the transitive closure of a given directed graph using
Warshall's algorithm
#include<stdio.h>
#include<math.h>
int max(int, int);
void warshal(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+
+)
p[i][j] = max(p[i][j], p[i][k] && p[k][j]);
}
int max(int a, int b) {
;
if (a > b)
return
(a);
else
return (b);
}
void main() {
int p[10][10] = { 0 }, n, e, u, v, i, j;
printf(“name=Neeraj(22BTC35140)\n”);
printf("\n Enter the number of vertices:");
scanf("%d", &n);
printf("\n Enter the number of
edges:"); scanf("%d", &e);
for (i = 1; i <= e; i++) {
//printf("\n Enter the end vertices of edge %d:",
i); scanf("%d%d", &u, &v);
p[u][v] = 1;
}
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");
}
warshal(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]);
printf("\n");
}
}

Output:
5. Implement 0/1 Knapsack problem using Dynamic Programming.
}#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
value=max(knap(i-1,j),p[i]+knap(i-1,j-w[i])); v[i]
[j]=value;
}
return(v[i][j]);
}
void main() {
int profit,count=0;
clrscr();
printf(“name=Neeraj(22BTC35140)\n”);
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--;
}
els
e
i--;
}
printf("Items included are\n");
printf("Sl.no\tweight\tprofit\n");
for (i=1;i<=n;i++)
if(x[i]) printf("%d\t%d\t%d\n",+
+count,w[i],p[i]);
printf("Total profit = %d\
n",profit); getch();
}

OUTPUT:
6. From a given vertex in a weighted connected graph, find shortest paths
to other vertices using Dijkstra's algorithm.
#include<stdio.h>
#define INFINITY
9999
#define MAX 10

void dijkstra(int G[MAX][MAX],int n,int startnode);

int main()
{
int G[MAX][MAX],i,j,n,u;
printf("Enter no. of
vertices:"); scanf("%d",&n);
printf("\nEnter the adjacency matrix:\
n"); for(i=0;i<n;i++)
for(j=0;j<n;j++) scanf("%d",&G[i]
[j]); printf("\nEnter the starting
node:"); scanf("%d",&u);
dijkstra(G,n,u);
return 0;
}

void dijkstra(int G[MAX][MAX],int n,int startnode)


{

int cost[MAX][MAX],distance[MAX],pred[MAX];
int visited[MAX],count,mindistance,nextnode,i,j;
//pred[] stores the predecessor of each node
//count gives the number of nodes seen so far
//create the cost matrix
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(G[i][j]==0)
cost[i][j]=INFINITY;
else cost[i][j]=G[i]
[j];
//initialize pred[],distance[] and
visited[] for(i=0;i<n;i++)
{
distance[i]=cost[startnode][i];
pred[i]=startnode;
visited[i]=0;
}
distance[startnode]=0
; visited[startnode]=1;
count=1;
while(count<n-1)
{
mindistance=INFINITY;
//nextnode gives the node at minimum
distance for(i=0;i<n;i++)
if(distance[i]<mindistance&&!visited[i])
{
mindistance=distance[i];
nextnode=i;
}
//check if a better path exists through
nextnode visited[nextnode]=1;
for(i=0;i<n;i++)
if(!visited[i]) if(mindistance+cost[nextnode]
[i]<distance[i])
{
distance[i]=mindistance+cost[nextnode][i];
pred[i]=nextnode;
}
count++;
}
printf(“name=Neeraj(22BTC35140)\n”);
//print the path and distance of each
node for(i=0;i<n;i++)
if(i!=startnode)
{
printf("\nDistance of node%d=
%d",i,distance[i]); printf("\nPath=%d",i);
j=i;
do
{
j=pred[j];
printf("<-%d",j);
}while(j!=startnode);
}
}

output:
7. Find Minimum Cost Spanning Tree of a given undirected graph
using Kruskal's algorithm.
#include<stdio.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()
{
printf(“name=Neeraj(22BTC35140)\n”);
printf("\n\tImplementation of Kruskal's algorithm\
n"); printf("\nEnter the no. of vertices:");
scanf("%d",&n);
printf("\nEnter the cost 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("The edges of Minimum Cost Spanning Tree are\
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("%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);
}
int find(int i)
{
while(parent[i])
i=parent[i];
return i;
}
int uni(int i,int j)
{
if(i!=j)
{
parent[j]=i;
return 1;
}
return 0;
}

output:
8. i) Print all the nodes reachable from a given starting node in a
digraph using BFS method.
#include<stdio.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++]);
}
}
void main()
{
int v; printf(“name=Neeraj(22BTC35140)\
n”); printf("\n Enter the number of
vertices:"); scanf("%d",&n);
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);
}

Output:
ii) Check whether a given graph is connected or not using DFS method.
#include<stdio.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);

int main() {
int i, j, count = 0; printf(“name=Neeraj(22BTC35140)\
n”);
printf("\n Enter number of

vertices:"); scanf("%d", &n);

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


return 0;

}
Output:

9. 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. 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>
int s[10],d,n,set[10],count=0;
void display(int);
int flag=0;
void
main()
{
int subset(int,int);
int i;
printf(“name=Neeraj(22BTC35140)\n”);
printf("Enter the number of elements in set\
n"); scanf("%d",&n);
printf("Enter the set values\
n"); for(i=0;i<n;++i)
scanf("%d",&s[i]);
printf("Enter the sum\
n"); scanf("%d",&d);
printf("The progrm output is\
n"); subset(0,0);
if(flag==0)
printf("there is no solution");
}
int subset(int sum,int i)
{
if(sum==d)
{
flag=1;
display(count);
return;
}
if(sum>d||i>=n)
return;
else
{
set[count]=s[i];

count++;
subset(sum+s[i],i+1);
count--;
subset(sum,i+1);
}
}
void display(int count)
{
int i;
printf("{");
for(i=0;i<count;i++)
printf("%d",set[i]);
printf("}");
}

Output:

10. 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>
int a[10][10],n,visit[10];
int cost_opt=0,cost_apr=0;
int least_apr(int c);
int least_opt(int c);

void mincost_opt(int city)


{
int i,ncity;
visit[city]=1;
printf("%d-->",city);
ncity=least_opt(city);
if(ncity==999)
{
ncity=1;
printf("%d",ncity);
cost_opt+=a[city][ncity];
return;
}
mincost_opt(ncity);
}
void mincost_apr(int city)
{
int i,ncity;
visit[city]=1;
printf(“name=Neeraj(22BTC35140)\n”);
printf("%d-->",city);
ncity=least_apr(city);
if(ncity==999)
{
ncity=1;
printf("%d",ncity);
cost_apr+=a[city][ncity];
return;
}
mincost_apr(ncity);
}

int least_opt(int c)
{
int i,nc=999;
int min=999,kmin=999;
for(i=1;i<=n;i++)
{
if((a[c][i]!=0)&&(visit[i]==0))
if(a[c][i]<min)
{
min=a[i][1]+a[c][i];
kmin=a[c][i];
nc=i;
}
}
if(min!=999)
cost_opt+=kmin;
return nc;
}

int least_apr(int c)
{
int i,nc=999;
int min=999,kmin=999;
for(i=1;i<=n;i++)
{
if((a[c][i]!=0)&&(visit[i]==0))
if(a[c][i]<kmin)
{
min=a[i][1]+a[c][i];
kmin=a[c][i];
nc=i;
}
}
if(min!=999)
cost_apr+=kmin;
return nc;
}
void main()
{
int i,j;
printf("Enter No. of cities:\
n"); scanf("%d",&n);

printf("Enter the cost matrix\


n"); for(i=1;i<=n;i++)
{
printf("Enter elements of row:%d\n",i
); for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
visit[i]=0;
}
printf("The cost list is \
n"); for(i=1;i<=n;i++)
{
printf("\n\n");
for(j=1;j<=n;j++)
printf("\t%d",a[i][j]);
}
printf("\n\n Optimal Solution :\
n"); printf("\n The path is :\n");
mincost_opt(1);
printf("\n Minimum
cost:");
printf("%d",cost_opt);

printf("\n\n Approximated Solution :\


n"); for(i=1;i<=n;i++)
visit[i]=0;
printf("\n The path is :\
n"); mincost_apr(1);
printf("\nMinimum cost:");
printf("%d",cost_apr);
printf("\n\nError in approximation is approximated solution/optimal solution=
%f", (float)cost_apr/cost_opt);
}

Output:
11. Find Minimum Cost Spanning Tree of a given undirected graph
using Prim’s algorithm.
#include<stdio.h>
int n,cost[10][10],temp,nears[10];
void readv();
void primsalg();
void readv()
{
int i,j;
printf(“name=Neeraj(22BTC35140)\n”);
printf("\n Enter the No of nodes or
vertices:"); scanf("%d",&n);
printf("\n Enter the Cost Adjacency matrix of the given
graph:"); for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if((cost[i][j]==0) && (i!=j))
{
cost[i][j]=999;
}
}
}
}

void primsalg()
{
int k,l,min,a,t[10][10],u,i,j,mincost=0;
min=999;
for(i=1;i<=n;i++) //To Find the Minimum Edge E(k,l)
{
for(u=1;u<=n;u++)
{
if(i!=u)
{
if(cost[i][u]<min)
{
min=cost[i][u];
k=i;
l=u;
}
}
}
} t[1]
[1]=k;
t[1][2]=l;
printf("\n The Minimum Cost Spanning tree
is..."); printf("\n(%d,%d)-->%d",k,l,min);
for(i=1;i<=n;i++)
{
if(i!=k)
{
if(cost[i][l]<cost[i][k])
{
nears[i]=l;
}
else
{
nears[i]=k;
}
}
}
nears[k]=nears[l]=0
; mincost=min;
for(i=2;i<=n-1;i++)
{
j = findnextindex(cost,nears);
t[i][1]=j;
t[i][2]=nears[j];
printf("\n(%d,%d)-->%d",t[i][1],t[i][2],cost[j][nears[j]]);
mincost=mincost+cost[j][nears[j]];
nears[j]=0;
for(k=1;k<=n;k++)
{
if(nears[k]!=0 && cost[k][nears[k]]>cost[k][j])
{
nears[k]=j;
}
}
}
printf("\n The Required Mincost of the Spanning Tree is:%d",mincost);
}
int findnextindex(int cost[10][10],int nears[10])
{
int min=999,a,k,p;
for(a=1;a<=n;a++)
{
p=nears[a];
if(p!=0)
{
if(cost[a][p]<min)
{
min=cost[a][p];
k=a;
}
}
}
return k;
}
void main()
{
readv();
primsalg();
}

output:
12. Implement All-Pairs Shortest Paths Problem using Floyd's algorithm.
#include<stdio.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]);
}
int min(int a,int b) {
if(a<b)
return(a); else
return(b);
}
void main() {
int p[10][10],w,n,e,u,v,i,j;
;
printf(“name=Neeraj(22BTC35140)\n”);

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]);
printf("\n");
}
printf("\n The shortest paths are:\
n"); for (i=1;i<=n;i++)
for (j=1;j<=n;j++) {
if(i!=j)
printf("\n <%d,%d>=%d",i,j,p[i][j]);
}

Output:
13. 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)
{ int i,j;
count++;
printf(“name=Neeraj(22BTC35140)\n”);
printf("\n\nSolution #%d:\n",count);
for (i=1;i<=n;i++) {
for (j=1;j<=n;j++) {
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;
}
}
els
e
k--;
}
}
void main() {
int i,n;
clrscr();
printf("Enter the number of Queens\
n"); scanf("%d",&n);
queen(n);
printf("\nTotal solutions=%d",count);
getch();
}

output:

You might also like