The Tortoise, the Hare, and the Cyclical Linked List
medium.com
medium.com
https://en.wikipedia.org/wiki/Cycle_detection#Floyd's_tortoi...
The serious ones do. At least, I found it to be commonly known.
Ex: 1 2 3 1 1 3 1 => 1
Hints
1- It can be done in one pass, linear time, constant space
2- It doesn't have to be numbers
3- The algorithm it is literally just 2 statements in a loop
Edit: I think I figured it out!
It serves both the job of making those curious who are not in the know (and hopefully get their clicks), while also telling the rest exactly what it's about (and save our time).
As a counter example where both k and k-1 are prime consider k=3. Then:
3 x + b = x (mod 2)
will fail to have a solution for odd b.
I prefer to think about this another way.
I'm also confused by your parent's comment "I suspect this would work for any prime for the tortoise, not just 2". In the post, 2 is the speed of the hare, and the tortoise's speed is 1.
Anyway, what you want is for the hare to meet the tortoise. In the general case, tortoise and hare both have arbitrary speeds t and h and enter the cycle at random positions.
The cycle is m nodes long. What you need is for (h - t), the amount by which the hare catches up to the tortoise, to be coprime to m. Since m could be anything, the only solution is h - t = 1. But you could have a hare going 6 nodes per timestep and a tortoise going 5.
This change adds a performance penalty with the extra equality checks, but can reduce the total number of steps in certain contexts (e.g. lists with long cycles that appear at an early node can benefit from a much faster hare).
> This change adds a performance penalty with the extra equality checks, but can reduce the total number of steps in certain contexts
The post seems to think that most of the value in this approach comes from the fact that it's easy to identify the beginning of the cycle after you've identified that the cycle exists. The method relies on the tortoise and hare finishing a round on the same node. Does that generalize to the case where the hare just passes the tortoise at some point?
I don't think there is any way to generalize the algorithm in the article to work on the method I described. However I was able to find an alternate algorithm to find the initial node of the cycle. It also has linear time complexity and constant storage, but it requires that the pointers in each node be mutable. It is also worse than Floyd's algorithm in almost every way. The algebra gets a little annoying, so I've just provided the informal algorithm below.
0) Find the meeting point as described above.
1) Find the cycle length.
2) Find the distance to the meeting point k+x (where k is the length of the linear portion and x is the number of nodes from the start of the cycle to the meeting point).
2a) If the hare speed is double the tortoise speed and the tortoise speed is less than the cycle length, this can be found directly.
2b) Otherwise, we can send a probe from the head until it reaches the meeting point.
This unfortunately this isn't enough information to find the initial node of the cycle, so more drastic measures are needed.3) Reverse all of the nodes of the cycle (We can actually do this during step 1 if we want to be more efficient, although some care needs to be taken (e.g. that step 2b is performed first).)
4) Send a probe from the head and count the steps until it reaches the meeting point node. This will have gone k + m - x steps.
5) Now we use some algebra to get x in terms of k and known values. We can substitute this into our equation that gives a known value for k+x (from step 3) to find k.
6) Reverse all the nodes of the cycle so they are back to how they originated.
7) Advance k times to reach the first node of the cycle; return this node.
The additional steps have a linear order complexity with respect to the number of nodes of the linked list. And like the OP algorithm, only a constant amount of storage is necessary, although reversing the list will require a couple more pointers to nodes. I'm not sure if there are many cases where this would perform better, but if they do exist they seem like they would be extremely rare.
> However, as I mentioned, this solution is spatially inefficient,
Typically you'd need to store N pointers, and the list would be of size N(size_of(pointer) + size_of(node)) - which would normally be much larger than the pointers? (otherwise use an array, perhaps?)
What are some real world cases where long linked list of very light nodes are best checked for cycles with this algorithm?
[1] https://aphyr.com/posts/341-hexing-the-technical-interview
Jokes on you, I've got 128 gigs of heap.