Skip to content

Commit 006a79e

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 6de700e commit 006a79e

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

dynamic_programming/travelling_salesman_problem.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env python3
22

3+
34
def tsp(distances: list[list[int]]) -> int:
45
"""
56
Solves the Travelling Salesman Problem (TSP)
@@ -41,7 +42,7 @@ def visit(city: int, mask: int) -> int:
4142
return distances[city][0] # Return to the starting city
4243
if memo[city][mask] != -1: # Return cached result if exists
4344
return memo[city][mask]
44-
min_cost = float('inf') # Use infinity for initial comparison
45+
min_cost = float("inf") # Use infinity for initial comparison
4546
for next_city in range(n):
4647
if not (mask & (1 << next_city)): # If unvisited
4748
new_cost = distances[city][next_city] + visit(
@@ -50,7 +51,11 @@ def visit(city: int, mask: int) -> int:
5051
min_cost = min(min_cost, new_cost)
5152
memo[city][mask] = int(min_cost) # Store result as an integer
5253
return memo[city][mask] # Return the cached result
54+
5355
return visit(0, 1) # Start from city 0 with city 0 visited
56+
57+
5458
if __name__ == "__main__":
5559
import doctest
60+
5661
doctest.testmod()

0 commit comments

Comments
 (0)