by tunesmith · 4y ago · 0 comments
The problem then becomes that I can't introduce NOT gates anywhere in a cycle, because then the bit will continue flipping and I'll get an infinite processing loop.
So it seems my only hope is external processes that continually walk the graph and keep track of where it visited to try and detect loops, and I don't like how that scales...
If the turtle and hare ever meet, you have a cycle. Otherwise, if the hare reaches the end of the list, you don't have a cycle.
Another notable advantage of Brent's algorithm is that it automatically finds the cycle length, rather than (in Floyd's case) any multiple of the cycle length.
https://en.wikipedia.org/wiki/Cycle_detection#Brent's_algori...
Cycle detection in (currently) 44 languages. Most (all?) use Brent's algorithm. They're operating on an iterated function but converting most of these to detect cycles in a linked list would be straightforward.
To be clear, http://trout.me.uk/gc/recycler-overview.pdf
There's also a paper with the full algorithm in that directory.
It's how Nim's ORC collector works.
(note that yes, I know, this is for collection, not cycle detection in general, but it's also brilliant and works)
Step through the list by one element per iteration with one of them, and with the other, step every other iteration.
If the two pointers are ever again equal you've found a cycle.
If you hit end of list, no cycle.
Or to save the comparisons make the thing the first node points to have a stop flag of some sort.
Head -> Node1 -> Node2 -> Node3
^ |
+-----------------+
If it's a circular list then your option would work, but not all cycles will produce circular lists.