Floyd's cycle-finding algorithm
来源:互联网 发布:反监听软件 编辑:程序博客网 时间:2024/05/22 16:27
From Quora
How does Floyd's cycle-finding algorithm work?
I understand that tortoise and hare's meeting concludes the existence of loop, but how does moving tortoise to beginning of linked list while keeping the hare at meeting place, followed by moving both one step at a time make them meet at starting point of cycle?
A lot of good perspectives. The way I remember it is like Namrata's answer but, I think, a bit simpler without needing to explicitly manipulate any equations or worry about how many times the cycle is traversed.
We only need to consider two positions, s - the start of the cycle, and s+x, where x is the offset from the cycle start where the two pointers meet.
When the slow pointer traveled s+x, the fast one will have traveled an additional distances+x from the offset position x along the cycle. This extras+x distance brought it back to the same position along the cycle (offsetx).
That means if moving a distance s+x from that offset brings the pointer back to the offsetx, then moving x less from that position, ors, will result in the pointer being at the start of the cycle - positions = offset 0.
Therefore, starting the formerly-fast pointer from where they met (offset x along the cycle), and the formerly-slow pointer at the beginning, and moving both pointers at equal pace, afters iterations, the formerly-fast one will be at the cycle start s and, the formerly-slow one will also be at the cycle starts.
Fast pointer have to meet the slow one because their distance is s and the different of velocity is 1.
And when they meet, the slow pointer won't go further then (s+x) because s/1<(s+x)/1.(this circumstance will happen when the slow one get into the circle and the fast one is in the x)
We only need to consider two positions, s - the start of the cycle, and s+x, where x is the offset from the cycle start where the two pointers meet.
When the slow pointer traveled s+x, the fast one will have traveled an additional distances+x from the offset position x along the cycle. This extras+x distance brought it back to the same position along the cycle (offsetx).
That means if moving a distance s+x from that offset brings the pointer back to the offsetx, then moving x less from that position, ors, will result in the pointer being at the start of the cycle - positions = offset 0.
Therefore, starting the formerly-fast pointer from where they met (offset x along the cycle), and the formerly-slow pointer at the beginning, and moving both pointers at equal pace, afters iterations, the formerly-fast one will be at the cycle start s and, the formerly-slow one will also be at the cycle starts.
Fast pointer have to meet the slow one because their distance is s and the different of velocity is 1.
And when they meet, the slow pointer won't go further then (s+x) because s/1<(s+x)/1.(this circumstance will happen when the slow one get into the circle and the fast one is in the x)
0 0
- Floyd's cycle-finding algorithm
- Floyd's Cycle Detection Algorithm
- Detect Cycle in a linked list (Floyd’s Cycle Detection Algorithm)
- [2016/12/8]happy number:application of Floyd's Cycle Detection Algorithm
- floyd's cycle detect算法
- Floyd’s Algorithm 弗洛伊德算法
- zoj 3232 It's not Floyd Algorithm
- zoj3232 It's not Floyd Algorithm
- Manacher's algorithm for finding longest palindromic substring
- Kruskal’s Algorithm for finding Minimum Spanning Tree
- Floyd algorithm
- floyd algorithm
- Floyd Algorithm
- ZOJ 3232 It's not Floyd Algorithm ( 暴力水过 )
- Floyd判圈算法(龟兔赛跑算法, Floyd's cycle detection)及其证明
- Finding a Point on a Bézier Curve: De Casteljau's Algorithm(转)
- Finding a Point on a Bézier Curve: De Casteljau's Algorithm
- [4_1_fence6] Finding Shortest Cycle
- login与服务器交换数据
- Java调用webservice出现System.NullReferenceException: 未将对象引用设置到对象的实例。
- MySQL管理工具---phpAdmin
- 数模搭配上春运(My First Blog)
- vector的用法
- Floyd's cycle-finding algorithm
- 数据结构之双向循环链表
- 使用Android-Pdf-Viewer-Library开源框架
- iOS 限制textField字节长度
- 注意头文件规则,避免链接错误:重复定义(multiple defination)
- 图
- 【转】使用gdb和core dump迅速定位段错误
- 1011. World Cup Betting (20)
- C语言解决大数的阶层问题