1

I was going through Floyd's Cycle Finding algorithm and had a doubt.

Do we increase the fast pointer by 2 only?

Is there any other value that should be the best match for this?

the_unknown_spirit
  • 2,518
  • 7
  • 34
  • 56

2 Answers2

1

Suppose both pointers wind up in a cycle. Relative to each other, the fast pointer is moving 1 unit closer to the slow pointer on each iteration. This means that at some point the pointers must overlap. This is a handy Property.

I suppose you could do it with a 3 speed and 2 speed pointer, but it wouldn't go any faster and the code would be more complex (have to write out checks for three pointers each step instead of just 2).

cephalopodMD
  • 169
  • 9
0

If you increase the value of the fast pointer by more than two you might "skip" the value in which both pointers overlap and end up having to iterate more times.

By having the fast points be incremented by two you guarantee that you'll be maximising your efficiency on the worst case scenario.

hbejgel
  • 4,674
  • 2
  • 17
  • 27