Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewriting description to align with code and video #220

Merged
merged 4 commits into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,38 +1,77 @@
# Floyd Cycle Detection Algorithm to find duplicate number in an array
# Floyd Cycle Detection Algorithm to find duplicate numbers in an array

## Problem Statement

Given an array of integers containing `n + 1` integers, where each integer is in the range `[1, n]` inclusive. If there is only one duplicate number in the input array, this algorithm returns the duplicate number without modifying the original array, otherwise, it returns -1.

## Approach
- We can imagine the array `arr` as a directed graph where each element is a node. `arr[i]` is the index of the node to which the i-th node points.
- For example, given the `arr = [1, 3, 4, 2, 3]`, we can represent `arr` as the following <br></br>
![image](images/graph_1.png)

- Use the function `f(x) = arr[x]` to construct the sequence:
`arr[0]`, `arr[arr[0]]`, `arr[arr[arr[0]]]`, `arr[arr[arr[arr[0]]]]`, etc....
- Each new element in the sequence is an element in `arr[]` at the index of the previous element.
- Starting from `x = arr[0]`, it will produce a linked list with a cycle.
- The cycle appears because `arr[]` contains duplicate elements(at least one). The duplicate value is an entrance to the cycle.
- Since there are duplicates in `arr`, a cycle exists in the directed graph as there is a path from node 3 to itself, `3 -> 2 -> 4 -> 3`.
- The problem now becomes finding the entrance node to the cycle (3 in this case).
- We can use the Floyd Cycle Detection Algorithm (also known as the "Hare and Tortoise algorithm") to detect the entrance of the cycle.
- The idea of the algorithm is to maintain two pointers, `hare` and `tortoise` that iterate the array at different "speeds" (just like the fable). The details are as follows:

### The procedure
- Using two pointers `hare` and `tortoise`.
- Initiate `hare = tortoise = arr[0]`.
- For every next step until `hare == tortoise` again, assign `hare = arr[arr[hare]]` and `tortoise = arr[tortoise]` (`hare` "jumps" 2 steps while `tortoise` "jumps" one step).
- At this point, `hare == tortoise`, reset `tortoise = arr[0]` while keeping the value of `hare` in the above procedure.
- For every next step until `hare == tortoise` again, assign `hare = arr[hare]` and `tortoise = arr[tortoise]` (this time `hare` only "jumps" one step).
- When `tortoise == hare`, the entrance of the cycle is found, hence `hare` and `tortoise` represent the value of the duplicated element.
- Return `tortoise` (also possible to return `hare`)

## Time Complexity

O(n)
`O(n)`

## Space Complexity

O(1)
`O(1)`, since we only use two extra variables as pointers.

## Example
## Example with step-by-step explanation
![image](images/graph_2.png)

```
arr = [3, 4, 8, 5, 9, 1, 2, 6, 7, 4]
arr = [3, 4, 8, 5, 9, 1, 2, 6, 7, 4]

return value = 4
hare = tortoise = arr[0] = 3

1st step:
- hare = arr[arr[3]] = arr[5] = 1
- tortoise = arr[3] = 5
2nd step:
- hare = arr[arr[1]] = arr[4] = 9
- tortoise = arr[5] = 1
3rd step:
- hare = arr[arr[9]] = arr[4] = 9
- tortoise = arr[1] = 4
4th step:
- hare = arr[arr[9]] = 9
- tortoise = arr[4] = 9

tortoise = arr[0] = 3

1st step:
- hare = arr[9] = 4
- tortoise = arr[3] = 5
2nd step:
- hare = arr[4] = 9
- tortoise = arr[5] = 1
3rd step:
- hare = arr[9] = 4
- tortoise = arr[1] = 4

return tortoise = 4
```

## Code Implementation Links

- [C++](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/search/floyd_cycle_detection_algo.cpp)
- [C](https://github.com/TheAlgorithms/C/blob/master/searching/floyd_cycle_detection_algorithm.c)

#### Video Explanation

[YouTube video explaining the Floyd Cycle Detection algorithm](https://www.youtube.com/watch?v=B6smdk7pZ14)
- [YouTube video explaining the Floyd Cycle Detection algorithm](https://www.youtube.com/watch?v=B6smdk7pZ14)
- [Another Youtube video](https://www.youtube.com/watch?v=PvrxZaH_eZ4&t=1s)
Binary file added en/Search Algorithms/images/graph_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added en/Search Algorithms/images/graph_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading