Skip to content

Commit f9356a0

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

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

dynamic_programming/TSL.py

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from itertools import combinations
22

3+
34
def tsp_dynamic_programming(dist_matrix):
4-
n = len(dist_matrix) # number of cities
5+
n = len(dist_matrix) # number of cities
56

67
# Initialize a dictionary to store the costs of visiting a subset of cities ending at city i
78
# dp[subset][i] will store the minimum cost to visit all cities in 'subset' and end at city i
@@ -30,15 +31,11 @@ def tsp_dynamic_programming(dist_matrix):
3031
final_mask = (1 << n) - 2 # All cities visited except city 0
3132
return min(dp[(final_mask, i)] + dist_matrix[i][0] for i in range(1, n))
3233

34+
3335
# Example usage
3436
if __name__ == "__main__":
3537
# Example distance matrix (symmetric matrix where dist_matrix[i][j] is the distance between city i and city j)
36-
dist_matrix = [
37-
[0, 10, 15, 20],
38-
[10, 0, 35, 25],
39-
[15, 35, 0, 30],
40-
[20, 25, 30, 0]
41-
]
38+
dist_matrix = [[0, 10, 15, 20], [10, 0, 35, 25], [15, 35, 0, 30], [20, 25, 30, 0]]
4239

4340
result = tsp_dynamic_programming(dist_matrix)
4441
print(f"The minimum travel cost is: {result}")

0 commit comments

Comments
 (0)