From b10c4c11653f27a29ebc9380822fa069350ac593 Mon Sep 17 00:00:00 2001 From: Rajata Hegde Date: Wed, 9 Oct 2024 18:40:44 +0530 Subject: [PATCH] added floyd's algorithm --- .../Detect_loop_or_cycle.c | 58 +++++++++++++++++++ .../Floyd's_cycle_finding_algorithm/readme.md | 27 +++++++++ 2 files changed, 85 insertions(+) create mode 100644 Linked_list/Floyd's_cycle_finding_algorithm/Detect_loop_or_cycle.c create mode 100644 Linked_list/Floyd's_cycle_finding_algorithm/readme.md diff --git a/Linked_list/Floyd's_cycle_finding_algorithm/Detect_loop_or_cycle.c b/Linked_list/Floyd's_cycle_finding_algorithm/Detect_loop_or_cycle.c new file mode 100644 index 00000000..bd74b473 --- /dev/null +++ b/Linked_list/Floyd's_cycle_finding_algorithm/Detect_loop_or_cycle.c @@ -0,0 +1,58 @@ +#include +#include +#include + +// Define the structure for a linked list node +struct Node { + int data; // Data part of the node + struct Node* next; // Pointer to the next node +}; + +// Function to detect a loop in the linked list +bool detectLoop(struct Node* head) { + // Initialize two pointers, slow and fast + struct Node *slow = head; // Slow pointer moves one step + struct Node *fast = head; // Fast pointer moves two steps + + // Loop until we reach the end of the list + while (slow != NULL && fast != NULL && fast->next != NULL) { + slow = slow->next; // Move slow one step + fast = fast->next->next; // Move fast two steps + + // If slow and fast meet, there is a loop + if (slow == fast) { + return true; // Loop detected + } + } + return false; // No loop +} + +// Function to create a new linked list node +struct Node* createNode(int data) { + struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); // Allocate memory + newNode->data = data; // Set node data + newNode->next = NULL; // Initialize next pointer to NULL + return newNode; +} + +int main() { + // Create a linked list: 10 -> 20 -> 30 -> 40 -> 50 -> 60 + struct Node* head = createNode(10); + head->next = createNode(20); + head->next->next = createNode(30); + head->next->next->next = createNode(40); + head->next->next->next->next = createNode(50); + head->next->next->next->next->next = createNode(60); + + // Create a loop for testing (linking the last node back to the third node) + head->next->next->next->next->next->next = head->next->next; // Creating a loop + + // Check for loop in the linked list and print the result + if (detectLoop(head)) + printf("Loop detected\n"); + else + printf("No loop detected\n"); + + return 0; // End of the program +} + diff --git a/Linked_list/Floyd's_cycle_finding_algorithm/readme.md b/Linked_list/Floyd's_cycle_finding_algorithm/readme.md new file mode 100644 index 00000000..f240499f --- /dev/null +++ b/Linked_list/Floyd's_cycle_finding_algorithm/readme.md @@ -0,0 +1,27 @@ +# Floyd's Cycle Finding Algorithm or Hare-Tortoise Algorithm +It is a pointer algorithm that uses only two pointers.one pointer moves twice as fast as the other one.The faster one is called fast pointer and the other one is called slow pointer. + +While traversing the linked list one of these things will occur- +1. The Fast pointer may reach the end (NULL) which shows that there is no loop in the linked list. +2. The Fast pointer again catches the slow pointer at some time therefore a loop exists in the linked list. + +## why does Floyd’s Algorithm works? +1. After each iteration where the slow pointer moves one step forward and the fast pointer moves two steps forward, the distance between the two pointers increases. +2. Initially, if the slow pointer is at a distance k from a certain point in the cycle, then after one iteration, the distance between the slow and fast pointers becomes k+1.After two iterations, this distance becomes k+2 and so on. +3. As they continue to move within the cycle, this distance will eventually equal the cycle length n. +4. At this point, since the distance wraps around the cycle and both pointers are moving within the same cycle, they will meet. + +## Time Complexity +O(n): The algorithm traverses the linked list at most twice. The first traversal is to determine if a loop exists Therefore, the time complexity is linear with respect to the number of nodes in the linked list. +## Space Complexity +O(1): The algorithm uses only two pointers (slow and fast), regardless of the size of the linked list. This means the space complexity is constant. + +## Example +10 -> 20 -> 30 ->40 -> 50 -> 60->30 +here we can see the link part of data of 60 is connected to data of 30.therefore there is a loop.and this algorithm will detect it and prints loop detected. + +10 -> 20 -> 30 ->40->NULL +here there is no loop and last node is connected to null which means the end of linked list.Therefore it prints no loop dected. + + +