[This page is auto-generated; please prefer to read the article’s PDF Form.]
We have R1 at et. After l steps, it will be at index (due to (1)):
|
Also, say another reference R is initialized to point to e0. It will be at index l after l steps. So, to find l, we can iteratively step R1 and R till they meet, while counting the steps. This step count will be l.
The above approach can be made more efficient. Due to (4), (t − n) can be expressed as:
| (5) |
Clearly, 0 ≤ (t − n) ≤ l. Now consider a reference R at element et−n and R1 which is already at et. For case (a) and (b) above, R will require n and l mod n steps respectively to reach el. Now consider equation (4): adding n and l mod n to t for case (a) and (b) respectively will make it l + n, and el+n is simply el (due to (1)).
Thus, for case (a), both R and R1 after taking n steps, must point to el. For case (b), they must point to el after taking l mod n steps. So we can find l as follows.
Initialize R at et−n. This can be done by stepping R from e0 (t−n) times. R1 is already at et. Now, iteratively step R and R1 till they meet (they will meet at el), while counting the steps. Say, the step count comes out to be x. Adding x to (t−n) will give us the total steps taken by R to reach el from e0, which must be l.
The “Find l” part in method floyd() implements this approach.
The other approach mentioned in the beginning of this section steps both R and R1 l times. This approach steps R l times, but steps R1 only x times where x is n (case (a)) or l mod n (case (b)).
Copyright © 2021 Nitin Verma. All rights reserved.