0

So my question is that in The tortoise and rabbit/heir algorithm to detect the circular linked list, why is it necessary to increment the second faster pointer only by 2 ?? I am not able to figure that out neither did i find any answer to it here.

Incrementing the first slow pointer by 1 makes sense so that we are iterating over all the elements which we will compare with a second pointer BUT WHY THE FASTER POINTER NEEDS TO BE INCREMENTED ONLY BY 2. Why can we increment it by 3 or 4 or more ????

And is there a way to calculate what should be the no. of hops(if not 2) of faster pointer in relation to the number of elements in list ???

Vince
  • 41
  • 1
  • 7
  • 1
    possible duplicate of [Why increase pointer by two while finding loop in linked list, why not 3,4,5?](http://stackoverflow.com/questions/5130246/why-increase-pointer-by-two-while-finding-loop-in-linked-list-why-not-3-4-5) – templatetypedef Jun 15 '12 at 00:23
  • Also, please don't use VERY LOUD UPPER CASE LETTERS IN YOUR ANSWER or REPEATED PUNCUTUATION!!! It reads as if you are shouting at us. For emphasis, it's better to use *italics* or **bold** to call attention. – templatetypedef Jun 15 '12 at 00:24
  • The question needs to be corrected. Faster pointer is used in detecting cycle in a linked list, not determining whether a list is circular or not. The difference is that in a circular list, the last node always points back to head. Where as a cycle can be pointing to any intermediate node – fkl Jun 18 '12 at 07:15

2 Answers2

0

You can use 3, 4 or whatever you want but is the fastest way with 2 because let's say you are in the loop with a step of 2 first time :( D is the pointer that is going 2 steps and S is the normal pointer.
Case 1)

step 1 :x - D - x - S - x - x ...
step 2 :x - x - x - D - S - x ...
step 3 :x - x - x - x - x - SD ( match)

Case 2)

step 1: x - D - S - x this is the step 2 from case 1 and we can see that this is step 2 and we also have a match

but if you have let's say a 3 step (D should be the pointer that is going 3 steps at a time.)

Case 1)

   step 1 : x - D - x - x - S ...
   step 2 : x - x - x - x - D - S ...
   step 3 : x - x - x - x - x - x - S - D ... ( D is passing S and it needs at least another loop to meet S)

Case 2)

step 1 : x - D - x - S -..
step 2 : x - x - x - x - SD ( match)

case 3)

step 1: x - D - S - ..
step 2: x - x - x - S - D ... ( D is passing S and it needs at least another loop to meet S)

And because of that 2 is better than 3, 4 or whatever you want because all the numbers larger than 2 have a chance of 1/(N-1) to meet for the first time.
Another way to explain is this: For D and S and a step of N if the distance between S and D is N - 1 we have a match ( D goes N steps and S 1 step and than D = S), other way D is passing S so we have a 1/(N-1) chance to meet S and D the first time, and because of that if N = 2 we have 1 / 1 = 100% , if N = 3 we have 1/2 = 50%.
Hope it helped.

Mark
  • 1,100
  • 9
  • 17
  • 2 is not necessarily the "fastest" step size. How quickly the pointers will meet depends not just on the step size but also on the length of the loop. It may just so happen that with step size=4 and loop length=3 the "fast" pointer will become equal the "slow" pointer just after one iteration. But, of course, longer steps take extra time to do. – Alexey Frunze Jun 15 '12 at 11:44
0

If you use 3 or 4 as step size, you need to add checks for all special cases when advancing by 3 might result in a null at first or second node.

Also you have a possibility of any of 3 positions from the slow coinciding with the faster one in case of a cycle.

It is just more code, more conditions to handle, error prone and appears less elegant to use more than 2 without any performance gains in terms of Big O

fkl
  • 5,412
  • 4
  • 28
  • 68