Assume the first while loop terminates after
iterations.
Let
be the index array graph of
. By
Corollary: All Index Array Graphs have A Cycle Continuing Forever,
has a cycle
. Without a loss of generality, assume
. Let
be the movement sequence of
. Now, let
be the smallest number such that
at iteration
(i.e., termination) of the first while loop. Finally, let
be the smallest number such that
. It is worth noting that
, thus
is the length of
.
We know that the first while loop
terminates at iteration . Since
at iteration
, we know
. Therefore, we can say that upon termination of the first while loop,
has travelled
nodes into
.
For an alternative but equivalent statement of this fact, assume
completes
full cycles around
by iteration
. Then,
. Hence, we can also say that upon termination of the first while loop,
has travelled
nodes into
.
These two equivalent statements brings us to the following result.
Therefore, travelling
nodes into
is equivalent to completing
complete cycles around
and travelling another
nodes.
Now consider the second while loop. On iteration 0,
and
. Now consider iteration
. On iteration
,
.
Given
is in
,
is just
complete cycles around
from
. Therefore,
. On iteration
, we also trivially know
. Hence,
so the second while loop terminates, and upon termination,
.