【发布时间】:2014-05-08 15:09:10
【问题描述】:
我要计算这个递归函数的最坏情况,时间复杂度。
list 是 m*n 个片段的列表。
matrix 是一个 mxn 的矩阵,用来填充这个和平。
Backtrack(list, matrix):
if(matrix is complete) //O(1)
return true
from the list of m*n pieces, make a list of candidatePieces to put in the matrix. // O(m*n)
for every candidatePiece // Worst case of n*m calls
Put that piece in the matrix // O(1)
if(Backtrack(list, matrix) is true)
return true
我猜这个公式是这样的:
T(n*m) = T(n*m - 1) + O(n*m) + O(1) = T(n*m - 1) + O(n*m)
这是正确的吗?
我不能使用主定理,我可以使用其他方法来获得封闭公式吗?
【问题讨论】:
-
你有没有从矩阵中取出东西?候选棋子永远不进去吗?
matrix is complete什么时候返回true? -
我问的原因是,
for every candidatePiece循环如果从未真正回溯,似乎毫无意义,这似乎是 Ashlynd 所假设的。
标签: time-complexity proof