Note: The Listed platform will permanently shut down December 31, 2026. Your data published on Listed will still be available in your personal Standard Notes account. Learn more

Simple and Effective TSP Solver

Goal

Find shortest Hamilton path or cycle

The Algorithm

Step 1: Initialisation, greedy

  • Do nearest neighbour from start point if fixed start point, or any point

Step 2: Optimisation, untwisting

  • still_optimisable = true
  • while still_optimisable: # 2-opt until no longer optimisable
  • still_optimisable = false
  • With all possible trunk (sequence of vertices) lengths
  • Scan from start point, if reversing a trunk gives better total distance
  • Reverse that trunk, and mark still_optimisable = true

Trunk Reversing Example

  • Trunk length = 3
  • Scan from C and reverse CDE
  • Remember: It’s reversing, not swapping C and E, it’s totally wrong to swap when in between them are not single node D.

Optimisation: Before 1 reversing

image.png
Route: AB-CDE-FGH (above)

Optimisation: After 1 reversing

image.png
Route: AB-EDC-FGH (above)

Reading


You'll only receive email when they publish something new.

More from Arno
All posts