Skip to content

Commit 4eb73b6

Browse files
authored
Fix formatting and update cache decorator
1 parent 4c45400 commit 4eb73b6

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

dynamic_programming/travelling_sales_person.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
Dynamic Programming: Travelling Salesman Problem (TSP)
33
-----------------------------------------------------
4-
Solves the classic TSP using the HeldKarp dynamic programming approach.
4+
Solves the classic TSP using the Held Karp dynamic programming approach.
55
66
Time Complexity: O(n^2 * 2^n)
77
Space Complexity: O(n * 2^n)
@@ -17,13 +17,13 @@
1717
80
1818
"""
1919

20-
from functools import lru_cache
20+
import functools
2121

2222

2323
def travelling_salesman(cost_matrix: list[list[int]]) -> int:
2424
"""
2525
Returns the minimum travel cost for visiting all cities and returning
26-
to the starting city (0-indexed), using the HeldKarp DP approach.
26+
to the starting city (0-indexed), using the Held Karp DP approach.
2727
2828
Args:
2929
cost_matrix (list[list[int]]): A square matrix where cost_matrix[i][j]
@@ -35,7 +35,7 @@ def travelling_salesman(cost_matrix: list[list[int]]) -> int:
3535
n = len(cost_matrix)
3636
all_visited = (1 << n) - 1 # bitmask with all cities visited
3737

38-
@lru_cache(maxsize=None)
38+
@functools.cache
3939
def dp(mask: int, pos: int) -> int:
4040
# Base case: all cities visited, return cost to go back to start
4141
if mask == all_visited:

0 commit comments

Comments
 (0)