forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtravelling_salesman_problem.py
56 lines (53 loc) · 2.09 KB
/
travelling_salesman_problem.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env python3
def tsp(distances: list[list[int]]) -> int:
"""
Solves the Travelling Salesman Problem (TSP)
using dynamic programming and bitmasking.
Args:
distances: 2D list where distances[i][j]
is the distance between city i and city j.
Returns:
Minimum cost to complete the
tour visiting all cities.
Raises:
ValueError: If any distance is negative.
>>> tsp([[0, 10, 15, 20], [10, 0, 35, 25], [15, 35, 0, 30],
[20, 25, 30, 0]])
80
>>> tsp([[0, 29, 20, 21], [29, 0, 15, 17], [20, 15, 0, 28],
[21, 17, 28, 0]])
69
>>> tsp([[0, 10, -15, 20], [10, 0, 35, 25], [15, 35, 0, 30],
[20, 25, 30, 0]]) # doctest: +ELLIPSIS
Traceback (most recent call last):
...
ValueError: Distance cannot be negative
"""
n = len(distances)
if any(distances[i][j] < 0 for i in range(n) for j in range(n)):
raise ValueError("Distance cannot be negative")
# Memoization table
memo = [[-1] * (1 << n) for _ in range(n)]
visited_all = (1 << n) - 1 # All cities visited mask
def visit(city: int, mask: int) -> int:
"""Recursively calculates the minimum cost to visit all cities."""
if mask == visited_all:
return distances[city][0] # Return to the starting city
if memo[city][mask] != -1: # Return cached result if exists
return memo[city][mask]
min_cost = float('inf') # Use infinity for initial comparison
for next_city in range(n):
if not (mask & (1 << next_city)): # If unvisited
new_cost = distances[city][next_city] + visit(
next_city, mask | (1 << next_city)
)
min_cost = min(min_cost, new_cost)
memo[city][mask] = int(min_cost) # Store result as an integer
return memo[city][mask] # Return the cached result
return visit(0, 1) # Start from city 0 with city 0 visited
if __name__ == "__main__":
import doctest
try:
doctest.testmod()
except Exception as e:
print(f"An error occurred during doctest: {e}")