liveBook · Manning

Traveling Salesman problem

As explained in Classic Computer Science Problems in Swift: Essential techniques for practicing programmers

The Traveling Salesman problem asks for a minimum-cost cycle that visits every city exactly once and returns to the starting city.

The Traveling Salesman problem asks for a minimum-cost cycle that visits every city exactly once and returns to its starting city. Cities form vertices, routes form edges, and the visit order may vary. Unlike a minimum spanning tree, the solution must be one giant returning cycle. section 5.7 section 8.2

A direct Swift solution builds and evaluates every candidate tour:

  • Expect brute force to become impractical as cities increase: the problem is NP-hard and its difficulty grows exceptionally quickly. section 8.2

Five Vermont cities

Evaluating all tours through Rutland, Burlington, White River Junction, Bennington, and Brattleboro produces a shortest cycle of 318 miles: Bennington → Brattleboro → White River Junction → Burlington → Rutland → Bennington.

How Classic Computer Science Problems in Swift: Essential techniques for practicing programmers covers it

How deep it goes
Develops a complete brute-force Swift solver with backtracking, cycle closure, distance evaluation, and a concrete five-city result, but not scalable alternatives.
Examples
Uses a Vermont distance matrix, recursive permutations, closed tours, and a 318-mile route as worked artifacts.
What it assumes
Assumes familiarity with arrays, dictionaries, recursion, graph terminology, and minimum spanning trees.
What you can do after
Build a brute-force Swift solver that enumerates tours and returns the shortest cycle and its distance.
How it connects
Connects graph problems, minimum spanning trees, genetic algorithms, and permutation generation.

Also explained in

Compare how 6 books explain Traveling Salesman problem →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Classic Computer Science Problems in Swift: Essential techniques for practicing programmers. The section links point to the parts of the book it draws on.