Closed ba-00001 closed 1 month ago
ISSUE#: #5693
Title: Adding Hare and Tortoise Algorithm
Prepared by: BRIAN B.
Date: 10/18/2024
File Path: src/main/java/com/thealgorithms/datastructures/lists/HareAndTortoise.java
File Link: src/main/java/com/thealgorithms/datastructures/lists/HareAndTortoise.java
Objective:
Implement Floyd’s Cycle Detection algorithm (Hare and Tortoise) to detect cycles in a singly linked list.
Key Questions:
What is the problem to be solved?
Detect if a cycle exists in a singly linked list.
What are the expected outcomes?
The algorithm should return true
if a cycle is found, otherwise false
.
Sub-tasks:
ListNode
structure to represent linked list nodes.HareAndTortoise
class to detect a cycle using two pointers.HareAndTortoiseTest
class.Input Data:
head
of a singly linked list.Expected Output:
true
or false
):
true
if there is a cycle.false
if no cycle is detected.Time Complexity:
The time complexity should be O(n) where n
is the number of nodes in the linked list, as each node is visited at most twice (once by the slow pointer and once by the fast pointer).
Space Complexity:
The space complexity should be O(1), as the algorithm only uses a constant amount of extra space (two pointers).
Resource Constraints:
There are no special CPU or memory constraints, as the algorithm efficiently operates in linear time and constant space.
Ethical Considerations:
No ethical considerations for this algorithm.
Chosen Data Structures:
ListNode
nodes.Justification:
Design Paradigm:
slow
and fast
), where slow
moves one step at a time, and fast
moves two steps at a time. If there is a cycle, these two pointers will eventually meet inside the cycle.Modular Design:
ListNode
class: Defines the structure of nodes in the linked list.HareAndTortoise.hasCycle()
method: Implements the core cycle detection algorithm.Edge Cases:
null
(empty).Error Handling:
null
, the algorithm should return false
(no cycle).Initial Version:
Optimization Techniques:
Test Cases:
null
).Debugging Tools:
assertTrue()
and assertFalse()
to check the correctness of the algorithm's output.Documentation:
HareAndTortoise
class and hasCycle()
method are well-documented with JavaDoc comments explaining their purpose and usage.HareAndTortoiseTest
contains clear test cases to ensure correctness.Maintenance Plan:
Date | Changes Made | Version |
---|---|---|
10/18/2024 | Initial Draft | 1.0 |
[MM/DD/YYYY] | [Describe subsequent changes] | X.X |
`
Algorithm Development Template
ISSUE#: [Issue Number]
Title: [Issue Title]
Prepared by: [Developer Name]
Date: [MM/DD/YYYY]
1. Problem Definition and Clarification
2. Input/Output Specifications
3. Constraints and Requirements
4. Data Structures
5. Algorithm Design Approach
6. Edge Cases and Error Handling
7. Optimization
8. Testing and Debugging
9. Documentation and Maintenance
Comments/Notes
[Additional notes or comments regarding the issue development.]
Version History
`