MakarovSuites
English
EnglishDeutschРусскийУкраїнськаFrançaisEspañolItalianoPolski
Contact & licences

Math

Travelling salesman

A tour that shortens while you watch - greedy, 2-opt uncrossing edge by edge, annealing climbing over the wall - against the exact optimum for thirteen cities and brute force over every tour for eight.

Travelling salesman — Actual application image
Actual application image

What happens

Compare nearest-neighbour construction, 2-opt improvement and simulated annealing on the same cities. A 2-opt local optimum has no improving two-edge exchange; it need not be the globally shortest tour. Annealing sometimes accepts longer tours to escape local minima, without guaranteeing the optimum in a finite run.
The minimum spanning tree is a lower bound on every tour. For these Euclidean distances, doubling a spanning tree and shortcutting repeated visits proves that the optimal tour is at most twice the minimum spanning tree. This bound concerns the optimum and a suitable tree-based construction; it does not constrain every greedy or intermediate tour.
The small exact scene uses Held–Karp dynamic programming over subsets, with O(N²·2^N) time rather than enumerating every permutation. The eight-city scene can enumerate every distinct tour. Exact algorithms need not inspect most permutations, but no polynomial-time exact algorithm is known for the general travelling-salesman problem. Compare the measured heuristic tours with the exact reference where available.

Equations used in the module
Equations
cities:  N points in [−1, 1]² from the shared die (seed 3);  tour length L = Σ |c_k − c_{k+1}| around the cycle;  (N − 1)!/2 distinct tours: 2520 for 8, 2.4·10^8 for 13, 1.0·10^46 for 40
greedy:  from city 0, always the nearest unvisited (ties by index);  12.8105 for forty cities
2-opt:  edges (a, b) and (c, d) with a before c on the tour; Δ = |ac| + |bd| − |ab| − |cd|;  the first pair in scan order with Δ < 0 is uncrossed by reversing the segment b … c;  repeat until none: 16 uncrossings, 10.5850 - the wall (a local optimum)
annealing:  a random pair (i, j), i < j, j − i ≥ 2;  accepted if Δ ≤ 0, else with probability exp(−Δ/T);  T₀ = 0.3, T ← 0.9997 T each step, 20 000 steps;  seed 6: best 10.4954, 462 accepted, 231 uphill;  2-opt on it: 10.2673
bound:  MST ≤ L for every tour (a tour minus an edge is a spanning tree), and the tour of a doubled tree shows L_opt ≤ 2·MST;  MST = 8.4480 for the forty (GraphTheory's tree, same die);  10.2673 / 8.4480 = 1.215
Held–Karp:  dp[S, k] = shortest path from 0 through the set S ending at k;  dp[S + k, k] = min over j in S of dp[S, j] + |jk|;  2^N·N² steps;  13 cities, seed 8: optimum 7.4710, tour 0 7 1 3 12 6 8 10 5 4 11 2 9;  2-opt from 50 random tours (Fisher–Yates on seeds 100 …): 7 reach it, worst 1.0789 ×, mean 1.0678 ×
brute force:  every permutation of 1 … N − 1 in lexicographic order, each tour once (first < last);  8 cities, seed 3: 2520 tours, best 5.2175 = Held–Karp, longest 11.9462, mean 9.1055

Related experiments

Back to catalogue
Collatz conjecture — Actual application image

Math

Collatz conjecture

Halve if even, triple and add one if odd: does every positive integer reach one? Explore a number's flight, stopping times up to 4096, and the different behaviour of 3n − 1 and 5n + 1. The general 3n + 1 claim remains a conjecture.

Prime spirals — Actual application image

Math

Prime spirals

The numbers laid on a square spiral, the primes lit: diagonals appear that should not be there, four times the density of the square, and with 41 at the centre Euler's n² + n + 41 runs down one of them; the Sacks spiral, and π(N) against N/ln N as the spiral grows.

Sunflower — Actual application image

Math

Sunflower

Vogel's sunflower grown seed by seed, r = √n and θ = n·α: sweep the angle by dragging across the disc and the spirals unwind, collapse onto rays, and lock in at the golden angle 137.508°, where the closest pair is 1.602 at any size and the families are Fibonacci numbers - the packing measured live, a task on the angle, and the chart of packing against angle.