0
$\begingroup$

I have the following algorithm

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 am having trouble understanding the time complexity of the following algorithm.It has time complexity: $O(n^n)$, As NQueen function is recursively called n times.But is there is any tighter bound possible for this program? what about best case, and worst case time complexity?

Can someone help me understand the time complexity of the algorithm?

$\endgroup$
  • $\begingroup$ Welcome to Computer Science! Your question is a very basic one. Since you did not include much of an attempt to solve it on your own, we have little to work with. Let me direct you towards our reference questions which cover your problem in detail. Please work through the related questions listed there, try to solve your problem again and edit to include your attempts along with the specific problems you encountered. Your question may then be reopened. Good luck! $\endgroup$ – Raphael Apr 12 '14 at 21:38
  • $\begingroup$ Also, please get rid of the source code and replace it with ideas, pseudo code and arguments of correctness. See here and here for related meta discussions. $\endgroup$ – Raphael Apr 12 '14 at 21:39
  • $\begingroup$ @Raphael: totally disagree with you.This question is not a 'very' basic one (at least to me). If this is very 'basic' then what about this and this.If these question can get more than 2 answers and also up votes then why is my question so unlucky? $\endgroup$ – user16713 Apr 14 '14 at 6:11
  • $\begingroup$ Did you read my comments in full? They explain why your question got closed. Both questions you link do contain some independent thought that matches the scope of the question. You, on the other hand, dump a huge amount of code and ask for an analysis without giving an attempt of your own. ("I think it has time complexity $O(n^n)$" does not count without a reason; most algorithms met in courses/practice have runtime $O(n^n)$ so it could be just a guess.) $\endgroup$ – Raphael Apr 14 '14 at 8:02