2

I get an interview question about the Floyd's cycle-finding algorithm :

When will Floyd's cycle-finding algorithm fail ?

I mean, is there rule for finding the step between the fast and slow pointers ?

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
WoooHaaaa
  • 19,732
  • 32
  • 90
  • 138
  • 3
    Perhaps when it's badly implemented? –  Apr 06 '13 at 05:39
  • As described by the Wiki, when there is no cycle, it will fail to detect one (thats a giant *duh*, to be sure, but none the less important). – WhozCraig Apr 06 '13 at 05:54
  • @NPE Were the interview question posed as "Assuming there exists a cycle within a sequence, under what conditions could Floyd's algorithm fail to find it?" I would concur. The *linked* algorithm fails under the guise that eventually the rabbit runs out of real-estate and never accounts for no-cycle. Were you or I to implement it, we would consider that a success-by-null. if `f(x)` in the linked algorithm returns null when passed null, then it will *not* fail, but that artifact appears left out (or I didn't see it, quite possible). – WhozCraig Apr 06 '13 at 06:00

3 Answers3

2

Under reasonable assumptions, it won't fail. It will either find a cycle or conclude that there isn't one.

The only failure scenarios I can think of are along the following lines:

  • there's a bug in the implementation;
  • the structure that's being traversed gets modified while the algorithm is in progress.
NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • Is there any rule for finding the step between the faster and slower pointers ? – WoooHaaaa Apr 06 '13 at 05:49
  • @MrROY: Slow moves by one step, fast moves by two steps. What does this have to do with the hypothetical failure of the algorithm (which is what your question appears to be about)? – NPE Apr 06 '13 at 05:53
  • +1 A dynamic list could most-definitely cause the general algorithm to fail. – WhozCraig Apr 06 '13 at 06:03
  • These are scenarios where every algorithm would fail. – SChepurin Apr 06 '13 at 06:26
  • 1
    @SChepurin: This is exactly what I mean when I say *Under reasonable assumptions, it won't fail*. I think the whole question is cryptic at best, especially after it's been edited. – NPE Apr 06 '13 at 06:27
  • @NPE - Yes. The only reason i see, is to test his/her way of thinking when trying to answer such a "cryptic" question. – SChepurin Apr 06 '13 at 07:07
  • It fails with slow pointer moving by one and faster pointer moving by 3 steps. And for the most weird reason, I cannot figure out why. – Divij Sehgal Jun 21 '17 at 19:47
  • Note: DivijSehgal's comment has been asked (and answered) here - [Why does Floyd's cycle finding algorithm fail for certain pointer increment speeds?](https://stackoverflow.com/questions/44685102/why-does-floyds-cycle-finding-algorithm-fail-for-certain-pointer-increment-spee) (and it has been noted that the pointer speeds of 1 and 2 is a fundamental part of the algorithm, as far as I'm concerned). – Bernhard Barker Jun 22 '17 at 01:15
1

There may not be any possible failure situations for Floyd's cycle finding algorithm.

The only possible failure scenario occurs when it is computationally difficult to find the next node in a dynamically changing linked list.

Deepu
  • 7,592
  • 4
  • 25
  • 47
0

okay, I'm solving this problem now and the same question occurred to me too.
The Floyd algorithm states that the fast pointer can be m times faster than the slow pointer. people generally take m as 2 i.e., fast advances by 2 steps while slow advances by only 1 step in every iteration. This particular value works for all structures and lengths of loop.
However, if we were to find the start of the cycle and we take m>2, then the algorithm fails in cases where there exists a loop of length m-1. In this case the cycle will be detected, but it fails on the part of finding the initial loop node.
This is something I've noticed but not sure how and why is it like that. Some insight will be helpful. thanks!

  • This is not an answer, but likely a comment. An answer should answer the question. – U. Windl Dec 19 '21 at 16:36
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/30626480) – bmkorkut Dec 20 '21 at 13:16