Skip to content

Resource · Guide or article

Travelling Salesman Problem

States the travelling salesman problem, finding the shortest round trip through every city, and shows how to model it as a weighted undirected graph.

Reviewed

Open the original: Travelling Salesman Problem (opens the original site in a new tab)

Practice this topicOpens the practice interviewer with a question on Graphs.

Add to a roadmap

Keep this resource with a roadmap you follow. It appears as your own extra resource; the roadmap's steps don't change and it doesn't count toward your tracked steps.

Source content: English

The travelling salesman problem (TSP) asks the following question: "Given a list of cities and the distances between each pair of cities, what is the shortest possible route that visits each city and returns to the origin city?" Solution of a travelling salesman problem: the black line shows the shortest possible loop that connects every red dot. TSP can be modelled as an undirected weighted graph, such that cities are the graph's vertices, paths are the graph's edges, and a path's distance is the edge's weight. It is a minimization problem starting and finishing at a specified vertex after having visited each other vertex exactly once. Often, the model is a complete graph (i.e. each pair of vertices is connected by an edge). If no path exists between two cities, adding an arbitrarily long edge will complete the graph without affecting the optimal tour.

PrepKit overview

PrepKit editors wrote and reviewed this overview from the source repository. It is not text from the resource itself.

Suited to
The source does not state a level
Format
Reference

What it covers

  • Graph model: cities as vertices, roads as weighted edges, a tour that visits each other vertex once
  • Completing the graph with an arbitrarily long edge when two cities have no direct path

How to use it

PrepKit study advice, not a claim made by the source.

Solve small instances two ways, first by trying every permutation and then with bitmask dynamic programming, and confirm both return the same tour length on a four-city example of your own.

Scope and limitations

Problem statement only. No algorithm, heuristic, complexity discussion or code walkthrough appears, and the sole reference is Wikipedia.

Access and format

Free English README on GitHub in the MIT-licensed trekhleb/javascript-algorithms repository.

Reviewed by PrepKit on Oct 2, 2026. Listing source commit: 85293e3e2b88

Evidence behind this overview
  • src/algorithms/graph/travelling-salesman/README.md

Pages that include this

Get the PrepKit newsletter

Optional. You don't need it to use PrepKit.

How we handle your email