|
11 | 11 | from typing import Any, Callable, Optional, Tuple |
12 | 12 |
|
13 | 13 |
|
14 | | -def floyds_cycle_finding(f: Callable[[Any], Any], x0: Any) -> Optional[Tuple[int, int]]: |
| 14 | +def floyds_cycle_finding( |
| 15 | + successor_function: Callable[[Any], Any], start_value: Any |
| 16 | +) -> Optional[Tuple[int, int]]: |
15 | 17 | """ |
16 | | - Finds a cycle in the sequence of values generated by the function f. |
| 18 | + Finds a cycle in the sequence of values generated by the successor function. |
17 | 19 |
|
18 | 20 | Args: |
19 | | - f: A function that takes a value and returns the next value in the sequence. |
20 | | - x0: The starting value of the sequence. |
| 21 | + successor_function: A function that takes a value and returns the next value. |
| 22 | + start_value: The starting value of the sequence. |
21 | 23 |
|
22 | 24 | Returns: |
23 | 25 | A tuple containing the index of the first element of the cycle (mu) |
24 | 26 | and the length of the cycle (lam), or None if no cycle is found. |
25 | 27 |
|
26 | 28 | Doctest examples: |
27 | | - >>> # Example with a cycle |
28 | | - >>> f = lambda x: (2 * x + 3) % 17 |
29 | | - >>> floyds_cycle_finding(f, 0) |
| 29 | + >>> # Example with a mathematical sequence |
| 30 | + >>> sequence_func = lambda x: (2 * x + 3) % 17 |
| 31 | + >>> floyds_cycle_finding(sequence_func, 0) |
30 | 32 | (0, 8) |
31 | 33 |
|
32 | | - >>> # Example with a different starting point and cycle |
33 | | - >>> f = lambda x: [1, 2, 3, 4, 5, 3][x] |
34 | | - >>> floyds_cycle_finding(f, 0) |
| 34 | + >>> # Example with a list acting as a sequence |
| 35 | + >>> get_next_item = lambda x: [1, 2, 3, 4, 5, 3][x] |
| 36 | + >>> floyds_cycle_finding(get_next_item, 0) |
35 | 37 | (2, 3) |
36 | 38 |
|
37 | | - >>> # Example with no cycle (sequence terminates) |
38 | | - >>> nodes = {0: 1, 1: 2, 2: 3, 3: None} |
39 | | - >>> f = lambda x: nodes.get(x) |
40 | | - >>> floyds_cycle_finding(f, 0) |
| 39 | + >>> # Example with a graph-like structure (no cycle) |
| 40 | + >>> get_next_node = lambda x: {0: 1, 1: 2, 2: 3, 3: None}.get(x) |
| 41 | + >>> floyds_cycle_finding(get_next_node, 0) |
41 | 42 |
|
42 | 43 | """ |
43 | | - # Main phase of the algorithm: finding a repetition x_i = x_2i. |
| 44 | + # Main phase: finding a repetition x_i = x_2i. |
44 | 45 | # The hare moves twice as fast as the tortoise. |
45 | | - tortoise = f(x0) |
46 | | - hare = f(f(x0)) |
| 46 | + tortoise = successor_function(start_value) |
| 47 | + hare = successor_function(successor_function(start_value)) |
47 | 48 | while tortoise != hare: |
48 | 49 | # If the hare reaches the end of the sequence, there is no cycle. |
49 | | - if hare is None or f(hare) is None: |
| 50 | + if hare is None or successor_function(hare) is None: |
50 | 51 | return None |
51 | | - tortoise = f(tortoise) |
52 | | - hare = f(f(hare)) |
| 52 | + tortoise = successor_function(tortoise) |
| 53 | + hare = successor_function(successor_function(hare)) |
53 | 54 |
|
54 | | - # At this point, the tortoise and hare have met. |
55 | | - # Now, find the position of the first repetition. |
56 | | - # The distance from the start to the cycle's beginning is mu. |
| 55 | + # Phase 2: find the position of the first repetition (mu). |
57 | 56 | mu = 0 |
58 | | - tortoise = x0 |
| 57 | + tortoise = start_value |
59 | 58 | while tortoise != hare: |
60 | | - tortoise = f(tortoise) |
61 | | - hare = f(hare) |
| 59 | + tortoise = successor_function(tortoise) |
| 60 | + hare = successor_function(hare) |
62 | 61 | mu += 1 |
63 | 62 |
|
64 | | - # Finally, find the length of the cycle. |
65 | | - # The hare moves one step at a time while the tortoise stays put. |
66 | | - # The number of steps until they meet again is the cycle length (lam). |
| 63 | + # Phase 3: find the length of the cycle (lam). |
67 | 64 | lam = 1 |
68 | | - hare = f(tortoise) |
| 65 | + hare = successor_function(tortoise) |
69 | 66 | while tortoise != hare: |
70 | | - hare = f(hare) |
| 67 | + hare = successor_function(hare) |
71 | 68 | lam += 1 |
72 | 69 |
|
73 | 70 | return mu, lam |
|
0 commit comments