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
0
votes
1 answer

Understanding polyline in AI

While using a computer display, polylines are used. You want to use an algorithm which will reduce points in the polyline. The polyline should be decimated within a specified tolerance. Which of the following algorithm would you use? A) Flood fill…
0
votes
0 answers

Why in Floyd's Algorithm for cycle detection if we take fast as head.next than the solution becomes wrong?

So I was solving leetcode's question where we have to return the node where the tail connects, but the solution came wrong when I took fast = head->next and came right when I took fast = head. I understood how the latter was correct but I didn't…
0
votes
2 answers

Cycle Detection Algorithm

Say I have a function f: f(0) = 0 f(i) = (i - 1) % 4 f(0..12): 0 0 1 2 3 0 1 2 3 0 1 2 3 I want to find the cycle start and the cycle length, which are 1 and 4, respectively. The tortoise and hare algorithm works with iterated functions, but I…
SuprDewd
  • 269
  • 6
  • 12
0
votes
1 answer

Is it possible to remove duplicates from an unsorted array in O(n) time, O(1) space complexity, using the Floyd's tortoise and hare algorithm?

Is it possible to remove duplicates from an unsorted array in O(n) time, O(1) space complexity, using the Floyd's tortoise and hare algorithm? Consider the array [3,1,3,4,2]. After removing duplicates, the function "remove_dups" must return…
user11502296
0
votes
0 answers

How do I fix the error my code is giving while printing the linked list after removing loop/cycle?

My code is about detecting a loop / cycle in a linked list, and then removing the error. However, only the first element of the linked list is getting printed after running the code to remove the loop / cycle. I have already tried merging the…
0
votes
1 answer

Writing a function to detect a cycle in a linked list (Floyd's alg)... Logic looks correct but can't find the bug

I am trying to detect a cycle in a linked list I created (I'm practicing for interviews questions). I understand the logic involved in Floyd's tortoise-hare algorithm but the function is always returning false... Here's my linked list: class…
0
votes
1 answer

Use Floyd-Warshall algorithm to find negative-weight circles

To judge whether a graph contains negative-circles, after running the Floyd-Warshall algorithm, can I deal with the problem only by scanning the diagonal elements of the matrix to find whether it has negative elements. I dont't know how to prove…
NiaBie
  • 55
  • 1
  • 9
0
votes
1 answer

Floyd's Algorithm - SIGTSTP error

I am working on a problem to find the number of nodes present in loop (if any) in the given linked list. Below is the function which accepts head of the node, checks for loop using Floyds cycle algorithm and if found, gives the number of nodes in…
0
votes
1 answer

Can you please explain how this below snippet for finding a loop in linked list works?

This code is for finding the loop in a single linked list and i have learned about it from http://blog.ostermiller.org/find-loop-singly-linked-list but could not get my head around why the code has been written the way it has been written. This…
0
votes
1 answer

Floyd's cycle-finding algorithm proof (Detecting loops in linked lists)

The algorithm uses a fast and a slow pointer to detect a loop in a linked list. My question is, how do I prove that the distance between the point where the two pointers collide in the loop and the start of the loop is equal to the distance between…
YSA
  • 799
  • 1
  • 12
  • 30
0
votes
1 answer

Detecting a loop in linked list without Floyds cycle detection algorithm

In an interview I was asked to detect the loop node in the linked list and count the number of nodes in the loop. As i was unaware of the flyod algorithm, I tried to figure out my own approach. The idea is in this scenario, the address of two nodes…
user3505249
0
votes
2 answers

How the loops are removed from link list

For instance if the link list is like : 1->2->3->4->5->6->7->8->9->4 We can find the loop using Floyd Cycle Algorithm but how to remove the loop in such cases?.
Vipul
  • 566
  • 5
  • 29
0
votes
1 answer

Is my implementation of Floyd's cycle detection algorithm incorrect?

I have the following code for detecting a cycle in a linked list: public Class Node { Object data; Node next = null; } boolean containCycle() { boolean retVal = true; Node head = this; Node slower = head; Node faster =…
Enzo
  • 969
  • 1
  • 8
  • 23
-2
votes
5 answers

How to find the total number of items in linked list?

I have a linked list which is cyclic and I want to find out the total number of elements in this list. How to achieve this?
JavaUser
  • 25,542
  • 46
  • 113
  • 139
1 2
3