Help needed when connecting these two concepts

The recently introduced "Ring Queue" concept, as I am more familiar with the interlace and hang algorithm for detecting a linked list, I am wondering if the principle of the ring queue has some kind of connection with the aforementioned loop detection algorithm in the Linked List as they both crawl around loop, then two pointers meet.

+4
source share
3 answers

A circular buffer is a data structure, and Floyd algorthm is ... an algorithm, so there are restrictions on any analogy.

But I will try:

+-------------------+-----------------------------------+---------------------------+ | | Circular buffer | Floyd algorithm | +-------------------+-----------------------------------+---------------------------+ | Tortoise | Start pointer | Slow pointer | | Hare | End pointer | Fast pointer | | Act I | Tortoise sleeps, hare walks | Tortoise walks, hare runs | | Act II | Hold hands; walk together forever | No act II | | Ends Romantically | Yes | Only if a cycle exists | +-------------------+-----------------------------------+---------------------------+ 
  • Act I: A round buffered turtle begins a dream story, unlike Floyd's algorithm, where it also moves (albeit slowly).
  • Climax: If the hare meets the turtle, the cycle is "found." This is guaranteed in a circular buffer, even though the turtle has slept (the buffer is circular, so all points in it are part of the loop). This is not like Floyd's algorithm, in which a meeting may not occur, since the linked list may not have a loop. In addition, the loop (if present) may not include a starting point, so a sleeping turtle would not be suitable for its plot.
  • Act II / Ending: When a hare encounters a (sleeping) turtle in a circular buffer, it wakes up, and then they unite, bypassing the cycle at all times. In the Floyd algorithm, the meeting of the two is the end of the story, although the story may also end up with the hare reaches the finish line (meeting someone else?).
+5
source

I think you only see patches here.

A circular buffer is just a data structure. The Turtle and Hare algorithm is also applicable to things that are not just circular queues and even in cases where the “pointers” are implicit (for example, searching for fixed points for functions).

0
source

I'm not sure if they are directly related.

In the linked list discovery algorithm, we try to detect the possibility of a loop in a linked list by choosing a scheme that causes two pointers to collide if there is a list.

In a circular buffer, a pointer collision means that the buffer is full or empty.

My assumption of the only connection that we can make here is that circular data structures can be detected by certain conditions, with only two pointers moving locally, and not a more “global” algorithm. For example, a loop search in a linked list can also be done through DFS.

0
source

Source: https://habr.com/ru/post/1339870/


All Articles