@@ -9,7 +9,8 @@ def tsp_brute_force(graph: list[list[int]]) -> int:
99 graph: 2D list representing distances between cities.
1010
1111 Returns:
12- The minimal total travel distance visiting all cities exactly once and returning to the start.
12+ The minimal total travel distance visiting all cities exactly once,
13+ and then returning to the start.
1314
1415 Example:
1516 >>> tsp_brute_force([[0, 29, 20], [29, 0, 15], [20, 15, 0]])
@@ -23,8 +24,9 @@ def tsp_brute_force(graph: list[list[int]]) -> int:
2324 # Enumerate all the permutations from city 1 to city n-1.
2425 for perm in permutations (nodes ):
2526 # Construct a complete path:
26- # starting from point 0, visit in the order of arrangement, and then return to point 0.
27- path = [0 ] + list (perm ) + [0 ]
27+ # Starting from point 0, visit in the order of arrangement,
28+ # and then return to point 0.
29+ path = [0 , * perm , 0 ]
2830
2931 # Calculate the total distance of the path.
3032 # Update the shortest path.
@@ -51,7 +53,8 @@ def tsp_dp(graph: list[list[int]]) -> int:
5153 n = len (graph )
5254 # Create a dynamic programming table of size (2^n) x n.
5355 # Noting: 1 << n = 2^n
54- # dp[mask][i] represents the shortest path starting from city 0, passing through the cities in the mask, and ultimately ending at city i.
56+ # dp[mask][i] represents the shortest path starting from city 0,
57+ # passing through the cities in the mask, and ultimately ending at city i.
5558 dp = [[float ("inf" )] * n for _ in range (1 << n )]
5659 # Initial state: only city 0 is visited, and the path length is 0.
5760 dp [1 ][0 ] = 0
@@ -73,14 +76,16 @@ def tsp_dp(graph: list[list[int]]) -> int:
7376 next_mask = mask | (1 << v )
7477 dp [next_mask ][v ] = min (dp [next_mask ][v ], dp [mask ][u ] + graph [u ][v ])
7578
76- # After completing visits to all cities, return to city 0 and obtain the minimum value.
79+ # After completing visits to all cities,
80+ # return to city 0 and obtain the minimum value.
7781 return min (dp [(1 << n ) - 1 ][i ] + graph [i ][0 ] for i in range (1 , n ))
7882
7983
8084def tsp_greedy (graph : list [list [int ]]) -> int :
8185 """
8286 Solves TSP approximately using the nearest neighbor heuristic.
83- Warming: This algorithm is not guaranteed to find the optimal solution! But it is fast and applicable to any input size.
87+ Warming: This algorithm is not guaranteed to find the optimal solution!
88+ But it is fast and applicable to any input size.
8489
8590 Args:
8691 graph: 2D list representing distances between cities.
@@ -91,7 +96,8 @@ def tsp_greedy(graph: list[list[int]]) -> int:
9196 Example:
9297 >>> tsp_greedy([[0, 29, 20], [29, 0, 15], [20, 15, 0]])
9398 64
94- >>> tsp_greedy([[0, 10, 15, 20], [10, 0, 35, 25], [15, 35, 0, 30], [20, 25, 30, 0]])
99+ >>> tsp_greedy([[0, 10, 15, 20], [10, 0, 35, 25],
100+ [15, 35, 0, 30], [20, 25, 30, 0]])
95101 80
96102 """
97103 n = len (graph )
@@ -104,8 +110,9 @@ def tsp_greedy(graph: list[list[int]]) -> int:
104110 for _ in range (n - 1 ):
105111 # Find the nearest city to the current location that has not been visited.
106112 next_city = min (
107- ((city , cost ) for city , cost in enumerate (graph [current ]) if not visited [city ] and city != current ),
108- key = lambda cost : cost [1 ],
113+ ((city , cost ) for city , cost in enumerate (graph [current ])
114+ if not visited [city ] and city != current ),
115+ key = lambda cost : cost [1 ],
109116 default = (None , float ('inf' ))
110117 )[0 ]
111118
0 commit comments