Questions tagged [floyd-cycle-finding]

For detecting the presence of cycle in linked list.

Also named as tortoise and the hare algorithm. The algorithm is named for Robert W. Floyd, who was credited with its invention by Donald Knuth.

It also finds out the start of the loop in the linked list.

44 questions
2
votes
3 answers

When will Floyd's cycle-finding algorithm fail?

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 ?
WoooHaaaa
  • 19,732
  • 32
  • 90
  • 138
2
votes
3 answers

Floyd loop detection algorithm with different step size

In the Floyd loop detection algorithm in linked list,we generally increment slow pointer by 1 unit and fast pointer by 2 unit. What are the other values that we can use for incrementing the slow and fast pointer and how do they change the…
1
vote
1 answer

Logic behind the method to identify cycle in a linked list

In the best method to detect a cycle in a linked list, we do the following: Use Floyd's Cycle-Finding Algorithm and identify a position within the cycle in a linked list. Count the size of the cycle in the linked list Position one pointer at the…
Aks
  • 5,188
  • 12
  • 60
  • 101
1
vote
0 answers

In sort Linked List question, I am getting runtime error when I initialize fast=head and it is running fine when I initialize fast=head->next

In sort Linked List question on leetcode, I am getting runtime error when I initialize fast=head and it is running fine when I initialize fast=head->next. What is the reason behind it? Can anyone explain. class Solution { public: ListNode*…
1
vote
1 answer

Happy number program using array, help me how to calculate Time Complexity?

I have written a Happy number program using an array, help me how to calculate Time Complexity? Any number will be called a happy number if, after repeatedly replacing it with a number equal to the sum of the square of all of its digits, leads us to…
user11502296
1
vote
1 answer

How to find a collision of first 56 bits for MD5(MD5(x)) for input data with the same prefix?

I have a code to find the collision of the first 56 bits of the hash function: md5(md5(x)) (using the Floyd algorithm to find cycles). The script returns two strings (hare, tortoise) for which a collision occurs. How to modify this script to return…
1
vote
2 answers

Cycle detection within a Hash in Ruby

Withing a hash I have a list of 'jobs', each with an id and a Parent. Jobs with a parent cannot be executed until their parent is. How would I detect a loop of dependencies? The data set is shown below: jobs = [ {:id => 1, :title => "a", :pid =>…
Cizzle
  • 27
  • 7
1
vote
2 answers

Why is the meeting point in a loop same number of steps as the start of the linked list?

There is this apparently standard approach to find if a linked list has a cycle and then return the node that is at the start of the cycle which is floy's algorithm with slow/fast pointers. The code and the logic is clear except 1 thing. The…
Jim
  • 3,845
  • 3
  • 22
  • 47
1
vote
2 answers

Why does Floyd's cycle finding algorithm fail for certain pointer increment speeds?

Consider the following linked list: 1->2->3->4->5->6->7->8->9->4->...->9->4..... The above list has a loop as follows: [4->5->6->7->8->9->4] Drawing the linked list on a whiteboard, I tried manually solving it for different pointer steps, to see…
1
vote
2 answers

Increasing fast pointer by 2 only in Floyd cycle finding algorithm

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
1
vote
1 answer

Skipping more than one node in Floyd's cycle finding Algorithm

Today I was reading Floyd's algorithm of detecting loop in a linked list. I was just wondering that won't it be better if we skip more than one node, (say 2) for faster loop detection? For example: fastptr=fastptr->next->next->next. Note that the…
Green goblin
  • 9,898
  • 13
  • 71
  • 100
0
votes
2 answers

Loop detection in LinkedList using C#

In the interview question, "Implement an algorithm which detects for presence of the loop.". For example, the linked list has a loop, like: 0--->1---->2---->3---->4---->5---->6 ▲ | | …
parsh
  • 746
  • 1
  • 10
  • 23
0
votes
1 answer

Why answers are different for below two codes implementing Floyd's algorithm?

Code#1 which uses a while True loop to check the intersection of elements/nodes. Output is 2, which is correct since it is the only duplicate: nums=[1,3,4,2,2] slow = nums[0] fast = nums[0] while True: slow = nums[slow] fast =…
0
votes
1 answer

Floyd's Algorithm to detect cycle in linked list proof

I've seen several proofs for Floyd's algorithm in several posts inside and outside stack overflow. All of them proves the second part of the algorithm that is, why the method to find the start of the cycle works. But none of the proofs I've seen…
Adeeb HS
  • 139
  • 7
0
votes
3 answers

Finding a Cycle in a Linked List

I'm currently working with Floyd's Cycle Finding Algorithm for a question that asks me to detect if a singly linked list contains a cycle.. I understand the Tortoise and Hare approach but am somewhat confused regarding its implementation. There have…