Professional Documents
Culture Documents
TSP@
TSP@
TSP@
{ { 0, 10, 15, 20 },
{ 10, 0, 35, 25 },
{ 15, 35, 0, 30 },
Code
import itertools
distance_matrix = [
num_cities = len(distance_matrix)#4
all_permutations = itertools.permutations(cities)
best_route = None
total_distance = 0
total_distance += distance_matrix[current_city][next_city]
current_city = next_city
total_distance += distance_matrix[current_city][0]
best_distance = total_distance
best_route = (0,) + route + (0,) # Include the starting city at the beginning and end of the route
output:
Best Route: (0, 1, 3, 2, 0)
Best Distance: 80