Skip to content

Commit 33dbb7b

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 7815581 commit 33dbb7b

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

dynamic_programming/travelling_salesman_problem.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#!/usr/bin/env python3
22

3+
34
def tsp(distances: list[list[int]]) -> int:
45
"""
56
Solves the Travelling Salesman Problem (TSP)
67
using dynamic programming and bitmasking.
78
Args:
8-
distances: 2D list where distances[i][j]
9+
distances: 2D list where distances[i][j]
910
is the distance between city i and city j.
1011
Returns:
1112
Minimum cost to complete the tour visiting all cities.
@@ -17,7 +18,7 @@ def tsp(distances: list[list[int]]) -> int:
1718
>>> tsp([[0, 29, 20, 21], [29, 0, 15, 17], [20, 15, 0, 28],
1819
[21, 17, 28, 0]])
1920
69
20-
>>> tsp([[0, 10, -15, 20], [10, 0, 35, 25], [15, 35, 0, 30], [20, 25, 30, 0]])
21+
>>> tsp([[0, 10, -15, 20], [10, 0, 35, 25], [15, 35, 0, 30], [20, 25, 30, 0]])
2122
# doctest: +ELLIPSIS
2223
Traceback (most recent call last):
2324
...
@@ -46,7 +47,11 @@ def visit(city: int, mask: int) -> int:
4647
min_cost = min(min_cost, new_cost)
4748
memo[city][mask] = min_cost # Store result in the memoization table
4849
return min_cost
50+
4951
return visit(0, 1) # Start from city 0 with city 0 visited
52+
53+
5054
if __name__ == "__main__":
5155
import doctest
56+
5257
doctest.testmod()

0 commit comments

Comments
 (0)