[This page is auto-generated; please prefer to read the article’s PDF Form.]
In the cycle-searching part of method floyd(), every time we progress R1 and R2 by 1 and 2 steps respectively. Suppose we instead progress them by p and q steps with p > q, and call the corresponding references Rp and Rq. Will these two references ever meet inside the cycle?
They will meet if there exists s ≥ 1 such that eps and eqs are equal and so belong to the cycle. That is, ps ≥ l and qs ≥ l, which is equivalent to (since p > q) qs ≥ l. So:
| (6) |
Due to (1), indices of eps and eqs are same iff:
It is in fact possible to find s ≥ 1 which satisfies both (6) and (7). An example is s = mn where m is a positive integer large enough such that mn ≥ l∕q. So we can conclude that Rp and Rq will always meet after some number of iterations, for any p and q with p > q.
Note that for p = 2 and q = 1, equations (6) and (7) are equivalent to (2) and (3) respectively.
■
[1] D. E. Knuth. The Art of Computer Programming, Vol 2, Third Edition. Addison-Wesley (1997).
[2] Wikipedia. Cycle Detection. https://en.wikipedia.org/wiki/
Cycle_detection.
Copyright © 2021 Nitin Verma. All rights reserved.