18
#include<stdio.h>
#include<math.h>

void printboard(int n);
void fourQueen(int k,int n);
int place(int k,int i);
int x[100];

void NQueen(int k,int n)
{
  int i;
  for(i=1;i<=n;i++)
  {
    if(place(k,i)==1)
    {     x[k]=i;
            if(k==n)
            {
                printf("Solution\n");
                printboard(n);
            }
            else
                NQueen(k+1,n);
    }
  }
}

int place(int k,int i)
{
  int j;
  for(j=1;j<k;j++)
  {
    if((x[j]==i)||abs(x[j]-i)==abs(j-k))
        return 0;
  }
  return 1;
}

void printboard(int n)
{
  int i;
  for(i=1;i<=n;i++)
    printf("%d  ",x[i]);
}

void main()
{
    int n;
    printf("Enter Value of N:");
    scanf("%d",&n);
    NQueen(1,n);
}

I think it has time complexity: O(n^n), As NQueen function is recursively calling, but is there is any tighter bound possible for this program? what about best case, and worst case time complexity. I am also confused about the place() function which is O(k) and calling from NQueen().

Tanmoy Banerjee
  • 1,433
  • 3
  • 22
  • 31

7 Answers7

16

There are a lot of optimizations than can improve the time complexity of the algorithm.

There is more information in these links:

  1. https://sites.google.com/site/nqueensolver/home/algorithm-results

  2. https://sites.google.com/site/nqueensolver/home/algorithms/2backtracking-algorithm

Snapshot

ichramm
  • 6,437
  • 19
  • 30
14

For Your function T(n) = n*T(n-1) + O(n^2) which translates to O(N!) time complexity approximately.

Vikram Bhat
  • 6,106
  • 3
  • 20
  • 19
8

TIME COMPLEXITY OF N-QUEEN PROBLEM IS

> O(N!)

Explanation: If we add all this up and define the run time as T(N). Then T(N) = O(N2) + N*T(N-1). If you draw a recursion tree using this recurrence, the final term will be something like n3+ n!O(1). By the definition of Big O, this can be reduced to O(n!) running time.

Tom Aranda
  • 5,919
  • 11
  • 35
  • 51
Shyam Bhimani
  • 361
  • 3
  • 8
  • 1
    Please explain why. – Tom Aranda Dec 16 '17 at 05:39
  • If we add all this up and define the run time as T(N). Then T(N) = O(N2) + N*T(N-1). If you draw a recursion tree using this recurrence, the final term will be something like n3+ n!O(1). By the definition of Big O, this can be reduced to O(n!) running time. – Shyam Bhimani Dec 16 '17 at 05:48
7

O(n^n) is definitely an upper bound on solving n-queens using backtracking. I'm assuming that you are solving this by assigning a queen column-wise. However, consider this - when you assign a location of the queen in the first column, you have n options, after that, you only have n-1 options as you can't place the queen in the same row as the first queen, then n-2 and so on. Thus, the worst-case complexity is still upper bounded by O(n!).

Hope this answers your question even though I'm almost 4 years late!

Ketan Sahu
  • 127
  • 1
  • 11
Akshay
  • 71
  • 1
  • 3
4

Let us consider that our queen is a rook, meaning we need not take care of diagonal conflicts.

Time complexity in this case will be O(N!) in the worst case, supposed if we were on a hunt to check if any solution exists or not. Here is a simple explanation.

Let us take an example where N=4.

Supposed we are want to fill the 2-D matrix. X represents a vacant position while 1 represents a taken position.

In the starting, the answer matrix (which we need to fill) looks like,

X X X X
X X X X
X X X X
X X X X

Let us fill this row-wise, meaning will select one location in each row then move forward to the next row.

For the first row, since none is filled in the matrix as a whole, we have 4 options. For the second row, we have 3 options as one row has already been taken off. Similarly, for the third row, we have 2 options and for the final row, we are left with just 1 option.

Total options = 4*3*2*1 = 24 (4!)

Now, this was the case had our queen were a rook but since we have more constraints in case of a queen. Complexity should be less than O(N!) in terms of the actual number of operations.

Mohit Gupta
  • 73
  • 1
  • 9
2

The complexity is n^n and here is the explanation

Here n represent the number of of queens and will remain same for every function call. K is the row number and function will be called times till k reaches the n.There if n=8,we have n rows and n queens.

T(n)=n(n+t(max of k - 1))=n^max of k=n^n as the max of k is n.

Note:The function has two parameters.In loop, n is not decreasing ,For every function call it remains same.But for number of times the function is called it is decreasing so that recursion could terminate.

aman_41907
  • 179
  • 10
0

The complexity is (n+1)!n^n begin with T(i)=O(niT(i+1)), T(n)=n^3. So, T(1)=nT(2)=2n^2T(3)=...=(n-1)n^(n-1)!T(n)=(n+1)