Skip to content

Commit 4013b48

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

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

dynamic_programming/travelling_salesman_problem.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55

66
def tsp(distances: list[list[int]]) -> int:
77
"""
8-
Solves the Travelling Salesman Problem (TSP) using
8+
Solves the Travelling Salesman Problem (TSP) using
99
dynamic programming and bitmasking.
1010
Args:
11-
distances: 2D list where distances[i][j] is the
11+
distances: 2D list where distances[i][j] is the
1212
distance between city i and city j.
1313
Returns:
14-
Minimum cost to complete the
14+
Minimum cost to complete the
1515
tour visiting all cities.
1616
Raises:
1717
ValueError: If any distance is negative.
@@ -20,7 +20,7 @@ def tsp(distances: list[list[int]]) -> int:
2020
80
2121
>>> tsp([[0, 29, 20, 21], [29, 0, 15, 17], [20, 15, 0, 28], [21, 17, 28, 0]])
2222
69
23-
>>> tsp([[0, 10, -15, 20], [10, 0, 35, 25], [15, 35, 0, 30], [20, 25, 30, 0]])
23+
>>> tsp([[0, 10, -15, 20], [10, 0, 35, 25], [15, 35, 0, 30], [20, 25, 30, 0]])
2424
ValueError: Distance cannot be negative
2525
"""
2626
n = len(distances)
@@ -35,7 +35,7 @@ def visit(city: int, mask: int) -> int:
3535
if mask == visited_all:
3636
return distances[city][0] # Return to start
3737

38-
min_cost = float('inf') # Large value to compare against
38+
min_cost = float("inf") # Large value to compare against
3939
for next_city in range(n):
4040
if not mask & (1 << next_city): # If unvisited
4141
new_cost = distances[city][next_city] + visit(
@@ -49,4 +49,5 @@ def visit(city: int, mask: int) -> int:
4949

5050
if __name__ == "__main__":
5151
import doctest
52+
5253
doctest.testmod()

0 commit comments

Comments
 (0)