Skip to main content

Travelling Salesman Problem

Definition​

The Travelling Salesman Problem (TSP) is a classic problem in computer science and operations research where the objective is to find the shortest possible route that visits a given set of cities and returns to the original city. It's an NP-hard problem, meaning there is no known polynomial-time solution for large inputs. However, there are several approaches and algorithms to approximate the solution

Practice​

tsp_brute_force(graph, current_city, visited_cities):
if all cities visited:
return distance from current_city to starting_city
else:
min_distance = INFINITY
for each city in graph:
if city not in visited_cities:
distance = tsp_brute_force(graph, city, visited_cities + [city])
min_distance = min(min_distance, distance + distance from current_city to city)
return min_distance