根据Wikibooks,如果所有点已经排序,Andrew算法将以线性时间运行。我们将采取排序积分的情况。安德鲁算法(复杂船体)的时间复杂度
然而,在伪代码,它说:
for i = 1, 2, ..., n:
while L contains at least two points and the sequence of last two points
of L and the point P[i] does not make a counter-clockwise turn:
remove the last point from L
append P[i] to L
现在,在这里我们可以看到一个for循环和嵌套的内部for循环while循环。根据我的逻辑推理,如果循环内有一个循环,它就不能具有线性时间复杂度。
我在哪里犯错? 谢谢!
编辑:通过分析代码,我推断以下。
for i loop--------O(n)
while loop----O(i-2) worst case
remove----O(1)
append--------O(1)
现在,如果while循环的时间复杂度为O(n),则总体复杂度为O(n^2)。但因为它更小,整体的复杂性应该是O((i-2)* n),我认为它比O(n)大,因为我增加到n ...
我不太确定正确计算这个...
非常好的答案。它通过摊销分析得出结论。 –
真正优雅的解释!只要我读它就知道了......谢谢Javatar! :) – Leta