Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.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 Floyd&x27的运行时复杂性;s周期检测_Algorithm_Time Complexity - Fatal编程技术网

Algorithm Floyd&x27的运行时复杂性;s周期检测

Algorithm Floyd&x27的运行时复杂性;s周期检测,algorithm,time-complexity,Algorithm,Time Complexity,根据一些在线来源,我提到了弗洛伊德的循环检测算法的运行时复杂性为O(n)。 说 运行时复杂性应为=m+rp*l+k 该值如何为O(n)?如果存在n节点,则在快速指针与慢速指针相遇或找到列表结束之前,慢速指针的移动不会超过n步。这意味着你要做O(n)的工作,使慢指针前进,使快指针前进两倍,也就是O(n)。因此,整个算法是O(n)。如果列表有n个节点,那么在中,为什么保证慢指针的移动不超过n步?如果有一个循环,那么一旦两个指针都在该循环中,快速指针在每次迭代中“追赶”一步。“追赶”所需的距离最多是循

根据一些在线来源,我提到了弗洛伊德的循环检测算法的运行时复杂性为O(n)。 说

运行时复杂性应为=m+rp*l+k
该值如何为O(n)?

如果存在
n
节点,则在快速指针与慢速指针相遇或找到列表结束之前,慢速指针的移动不会超过
n
步。这意味着你要做O(n)的工作,使慢指针前进,使快指针前进两倍,也就是O(n)。因此,整个算法是O(n)。

如果列表有n个节点,那么在中,为什么保证慢指针的移动不超过n步?如果有一个循环,那么一旦两个指针都在该循环中,快速指针在每次迭代中“追赶”一步。“追赶”所需的距离最多是循环的长度,因此慢指针不能在快指针追赶之前在循环的第二圈开始。你能提供一些证据证明慢指针确实会在循环完成之前追赶吗?快指针,这就是证据。慢指针需要的迭代次数与完成一圈所需的周期相同。快速指针只需要那么多的迭代就可以赶上进度。“每一步,快速指针和慢速指针之间的距离将增加1。”这就是我一直在寻找的直觉,谢谢!
p = slow pointer
q = fast pointer
m = the distance from start of linked list to first loop node
k = the distance of the meeting point of fast and slow nodes from the first loop node 
l = length of loop
rp = number of loop rotations by p before meeting q.
rq = number of loop rotations by q before meeting p.