0% found this document useful (0 votes)
7 views

DS2 Lab team-9

The document presents an overview of Floyd's Cycle Detection Algorithm, also known as the Tortoise and Hare Algorithm, which is used to detect cycles in data structures like linked lists. It explains the algorithm's mechanics, including the use of two pointers moving at different speeds, and provides a simulation to illustrate its functionality. The document also discusses the algorithm's time and space complexity, comparing it to similar algorithms, highlighting its efficiency and suitability for memory-constrained environments.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

DS2 Lab team-9

The document presents an overview of Floyd's Cycle Detection Algorithm, also known as the Tortoise and Hare Algorithm, which is used to detect cycles in data structures like linked lists. It explains the algorithm's mechanics, including the use of two pointers moving at different speeds, and provides a simulation to illustrate its functionality. The document also discusses the algorithm's time and space complexity, comparing it to similar algorithms, highlighting its efficiency and suitability for memory-constrained environments.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Data Structure and Algorithms II Laboratory

CSE 2218 (E)

Presented by :
● Sanjida Nafin Riya - 011202065
● Azmain Elahi - 011222289
● Tanjid Ahmad Anik - 0112230651
● Md. Nasir Khan Naim - 0112230676

Presented to :
Iftekharul Abedeen
Lecturer,
CSE, United International University.
Our Presentation Topic

Floyd's Cycle Detection Algorithm


also known as Tortoise and Hare Algorithm
❏ Context of the Floyd’s Cycle
Detection Algorithm
Key areas we will ❏ Explanation of this algorithm
explore regarding this ❏ A simulation of this algorithm with
algorithm include - proper data
❏ Explanation of its Time & Space
complexity and comparison to
similar algorithms of similar class
Definition

Context of the Floyd’s


Cycle Detection Algorithm
Floyd's Cycle Detection Algorithm
is a technique to detect cycles in
sequences by using two pointers
moving at different speeds.
Why It's Used

This algorithm is used to efficiently


Context of the Floyd’s detect cycles in data structures, such
Cycle Detection Algorithm as linked lists or iterative processes,
with minimal space complexity.
Stability and Performance:
● Memory management
● Error detection
● Algorithm efficiency
Where It's Used

In Data Structure,
❖ Detecting cycles in Linked Lists
Context of the Floyd’s ❖ Graph Algorithms
Cycle Detection Algorithm ❖ Hash tables

In Real-World Applications,
❖ Networking
❖ Game Development
The algorithm uses two pointers to
traverse the linked list at different
Explanation of this
speeds :
algorithm
➔ Slow pointer (Tortoise) : moves one
step at a time.
➔ Fast pointer (Hare) : moves two steps at
a time.
Steps of the Algorithm:

➢ Initialization: Both pointers are set to the start


(head) of the list.
➢ Movement: The slow pointer moves one node,
and the fast pointer moves two nodes in each
Explanation of this iteration.
algorithm ➢ Cycle Detection:
○ If the fast pointer reaches the end (NULL),
it means there is no cycle.
○ If the fast pointer ever meets the slow
pointer, it means a cycle exists.
➢ Cycle Starting Point: Reset one pointer to the
head and move both pointers one step at a time
until they meet; the meeting point will be the
start of the cycle.
If we consider the following Linked List -

1→2→3→4→5→6

A simulation of this In this list, node 6 points back to node 3, creating a


algorithm with proper data cycle between nodes 3 → 4 → 5 → 6 → 3.

For Detecting a cycle,

Initial State: Both slow and fast pointers start at node 1.


Iteration Slow Pointer Fast Remark
Pointer

1 2 3 No match

2 3 5 No match
A simulation of this
algorithm with proper data 3 4 3 No match

4 5 5 Match found
(cycle
detected)

Since slow and fast pointers meet at Node 5, so this


linked list has a cycle.
For Finding the Starting Node of the cycle,
Reset one pointer (slow) to the head (Node 1). Keep the
other pointer (fast) at the meeting point (Node 5).

A simulation of this Step Slow Pointer Fast


Pointer
Remark

algorithm with proper data 1 2 6 No match

2 3 3 No match

As Slow and Fast pointers meet at Node 3, so we can say


the cycle starts from Node 3.
Pseudocode for detecting a cycle in a linked list using
Floyd’s cycle detection algorithm, finding the starting
node of the cycle -
// Function to detect cycle in the linked list

int detectCycle(struct Node* head, struct Node** meetPoint) {

struct Node* slow = head;

A simulation of this struct Node* fast = head;

while (fast != NULL && fast->next != NULL) {


algorithm with proper data slow = slow->next;

fast = fast->next->next;

if (slow == fast) {

*meetPoint = slow;

return 1;

return 0; // No cycle found

}
// Function to find the starting node of the cycle

struct Node* findCycleStart(struct Node* head, struct


Node* meetPoint) {
struct Node* ptr1 = head;
A simulation of this
struct Node* ptr2 = meetPoint;
algorithm with proper data
while (ptr1 != ptr2) {
ptr1 = ptr1->next;
ptr2 = ptr2->next;
}
return ptr1;
}
Here’s the output for the this code -

A simulation of this
algorithm with proper data
Complexity

Explanation of its Time &


Space complexity and Time Complexity : O(n)
comparison to similar
Space Complexity : O(1)
algorithms of similar class
Comparison to
Similar Algorithms

Explanation of its Time & Naive Approach (Hash Table)


Space complexity and ● Time Complexity : O(n)
comparison to similar ● Space Complexity : O(n)
algorithms of similar class Floyd's Algorithm is more space-efficient (O(1) vs.
O(n)), but both have the same time complexity.
Comparison to
Similar Algorithms

Explanation of its Time & Brent's Algorithm


Space complexity and ● Time Complexity : O(n)
comparison to similar ● Space Complexity : O(1)
algorithms of similar class Brent's Algorithm and Floyd's Algorithm have the same
complexity in both time and space, but Brent's
algorithm may be more efficient in certain scenarios by
reducing redundant checks.
Why Floyd's Algorithm is Popular -

It combines simplicity with efficiency. Despite its minimal space usage, it


reliably detects cycles in linear time. The lack of additional data structures
makes it suitable for memory-constrained environments.
In Conclusion -

Floyd's Cycle Detection Algorithm strikes a good balance between time


efficiency and memory usage, making it ideal for cycle detection in linked
lists and similar structures.
Thank You

You might also like