hare and tortoise algoritm of cycle detection in a linked list

This commit is contained in:
System Administrator 2023-06-06 20:16:28 +05:30
parent 8c5e5f4f0d
commit 439308c715
3 changed files with 60 additions and 0 deletions

View File

@ -0,0 +1,3 @@
# Floyd's Cycle Detection Algorithm (aka Hare and Tortoise Algorithm)
Floyd algorithm for finding the cycles in a linked list is used to determine presence of a cycle in the linked list using constant space and linear time (i.e. space complexity = O(1) and time comoplexity = O(n), where n is number of nodes in the list).
The algorithm uses two pointers where one pointer moves a littile faster (the hare pointer). If at any point of traversal, the fast pointer meets the slow pointer, we can say that we have a cycle in the linked list. Conversly if the fast pointer hits a null, we have reached the end of list and no cycle is present.

View File

@ -0,0 +1,36 @@
import LinkedList from "../../../../data-structures/linked-list/LinkedList";
import LinkedListNode from "../../../../data-structures/linked-list/LinkedListNode";
import hareAndTortoise from "../hareTortoise";
function getTail(linkedList){
let tail = linkedList.head;
while(tail.next){
tail=tail.next;
}
return tail;
}
describe('cycleDetection',()=>{
it('should be able to properly detect no cycles in a linked list',()=>{
let simpleLinkedList = new LinkedList();
simpleLinkedList
.append(1)
.append(2)
.append(3);
const containsCycle = hareAndTortoise(simpleLinkedList);
expect(containsCycle).toEqual(false);
});
it('should detect a cycle in the linked list',()=>{
let cyclicLinkedList = new LinkedList();
cyclicLinkedList
.append(1)
.append(2)
.append(3)
.append(4);
const tail = getTail(cyclicLinkedList);
tail.next = cyclicLinkedList.head;
const containsCycle = hareAndTortoise(cyclicLinkedList);
expect(containsCycle).toEqual(true);
});
})

View File

@ -0,0 +1,21 @@
/**
* @param {LinkedList} linkedList
* @returns {boolean}
*/
export default function hareAndTortoise(linkedList){
let hare = linkedList.head;
let tortoise = linkedList.head;
while(hare && hare.next && hare.next.next){
hare = hare.next.next;
tortoise = tortoise.next;
//iterate till "hare" has not reached the end of list
if(hare == tortoise){
// we got a cycle
return true;
}
}
// no cycle just the end of list
return false;
}