Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 安德鲁的时间复杂性';s算法(复杂外壳)_Algorithm_Time Complexity_Convex Hull - Fatal编程技术网

Algorithm 安德鲁的时间复杂性';s算法(复杂外壳)

Algorithm 安德鲁的时间复杂性';s算法(复杂外壳),algorithm,time-complexity,convex-hull,Algorithm,Time Complexity,Convex Hull,根据,如果所有点都已排序,安德鲁算法将以线性时间运行。我们将以排序点为例 但是,在伪代码中,它表示: 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

根据,如果所有点都已排序,安德鲁算法将以线性时间运行。我们将以排序点为例

但是,在伪代码中,它表示:

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循环和一个while循环嵌套在for循环中。根据我的逻辑推理,如果在一个循环中有一个循环,它就不可能具有线性时间复杂度

我在哪里犯错? 谢谢

编辑:通过分析代码,我得出以下结论

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)大,因为i增加到n


我真的不知道如何正确计算…

你确实有线性复杂性,因为:

For(i=1…n)赋予复杂性一个n因子,因此直到现在O(n)

在嵌套的while循环中,您有一个条件(L size>=2&&它还将检查您是否做了逆时针旋转(应该在恒定时间内完成)。因此,这可能需要将复杂度扩展到n的因子(这将产生二次复杂度O(n*n))


但现在的问题是嵌套while循环的主体最多可以执行N次,因为这里有从L弹出的元素;除了每一个i有一次外,你不会推动L中的元素。因此,在算法执行过程中,push(append)语句将执行N次,因此POP(remove last element)最多可以执行N次,而不管它嵌套在封闭的for循环中。因此,复杂度仍然是O(n)=线性复杂度。

非常好的答案。它是通过摊销分析得出的。真是优雅的解释!我一看就知道了。。。谢谢Javatar!:)