简体   繁体   English

算法 - nQueens 的时间复杂度

[英]Algorithm - Timecomplexity of nQueens

I have this questions which may be basic to ask, but I am finding it hard to understand.我有这个问题可能很基本,但我发现很难理解。 How to determine time complexity of nQueens.如何确定 nQueens 的时间复杂度。 In some post it says its n, because in (4*4) matrix.在某些帖子中,它说它的 n,因为在 (4*4) 矩阵中。 each queen gets one row & as the queens moved down the rows the column option reduces (n * n-1 * n-2..,) which is fine.每个皇后得到一行,当皇后向下移动时,列选项会减少 (n * n-1 * n-2..,),这很好。 but in recursive algorithm we pass rowIndex and for each call we check if queen can be placed in a cell looping thru all columns & then call solveNQueens for rowIndex+1.但是在递归算法中,我们传递 rowIndex 并且对于每个调用,我们检查是否可以将 Queen 放置在循环遍历所有列的单元格中,然后为 rowIndex+1 调用solveNQueens。 In this case is time complexity still n!在这种情况下,时间复杂度仍然是 n!

bool solveNQueens(rowIndex, matrix)
{
  //recursion base case to exit

  //decision tree
  for (int i = 0; i<4; i++)
  {
    if(cellAvailable)
    {
      solveNQueens(rowIndex+1, matrix);
    }
  }
}

In fact O(n!) is an upper bound.实际上O(n!)是一个上限。 But as this article explains , the number of solutions is roughly (0.143n)^n .但正如本文所解释的,解决方案的数量大致为(0.143n)^n So that gives a super-exponential lower bound.所以这给出了一个超指数的下限。

Coming up with a better answer is likely to be an open research problem.提出更好的答案可能是一个开放的研究问题。 But I expect that the lower bound is in the right ballpark for what the algorithm takes to produce all answers.但我希望算法生成所有答案所需的下限在正确的范围内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM