60 Minutes Faster Than the World Record
The world record for visiting all 272 London Underground stations is 17h45m. An automated solver on public timetable data does it in 16h45m. That's one hour faster. This is how I got there, from 19h42m to 16h45m, across 19 progressive improvements.
Three attempts at the right abstraction
This project went through three distinct modeling approaches before landing on the current one. The progression matters because each attempt revealed a specific limitation that shaped the next.
Static graph + Google Maps API
Scraped Wikipedia for station names, queried Google Maps for travel times between same-line stations, built a fixed-weight adjacency matrix. No time dependency at all.
Probabilistic headway models
Model train arrivals as distributions, compute expected wait times per station, score routes by expected total time. I abandoned this because there were too many parameters and no ground truth to validate against.
GTFS backtesting on real timetables
Parse published timetable data, pick a date, simulate the exact schedule deterministically. No probability needed. You replay exact train times like a financial backtest.
v1: The static graph (tspmetro)
The first attempt treated the Underground as a classical weighted graph. Station names scraped from Wikipedia, travel times from the Google Maps Directions API (transit mode), stored in an adjacency matrix. The plan was Floyd-Warshall for all-pairs shortest paths, then solve TSP on the complete distance matrix.
The fundamental problem: edge weights are fixed. The graph says Baker Street → Bond Street costs 3 minutes, always. But in reality, if you arrive at Baker Street at 8:07 and the next Jubilee line train departs at 8:12, you wait 5 minutes, so the real cost is 8 minutes. And that wait time changes every minute of the day. A static graph can't distinguish between Kensington Olympia (9 trains/day) and Oxford Circus (one every 90 seconds).
The errors weren't small. The static graph estimated total routes at around 12 hours because it just sums the fixed edge weights along a path. In practice, the same route takes 18+ hours because you spend a huge fraction of the time waiting for trains. The gap between "travel time" and "elapsed time" is enormous on a network with 272 stations and headways ranging from 90 seconds to 2 hours. I ran the static solver's output through the real timetable and it missed dozens of connections, arriving at stations minutes after trains had left. That's what killed v1.
Static (tspmetro)
Same cost at 8am and 11pm.
Time-expanded (covtsp)
Costs depend on when you arrive.
v2: Probabilistic headway models (abandoned)
Realising the static graph couldn't capture time dependency, the next idea was to model it statistically. Treat train arrivals at each station as a probability distribution: Poisson for high-frequency lines, something heavier-tailed for infrequent branches. Then compute expected wait times per station and weight the graph edges accordingly. Stations with faster service get lower expected waits, making the TSP solver naturally prefer efficient connections.
The problems stacked up quickly:
- Train headways aren't stationary. They vary by time of day, line, and direction. A single distribution per station loses the signal.
- The model introduces parameters (distribution family, time-of-day bins, smoothing) with no clean way to validate them. Expected wait time of 4.2 minutes at Baker Street: is that right? How would you check?
- Even with perfect distributions, expected value isn't enough. A route that works "on average" can fail catastrophically if one connection is missed. You need to reason about specific trains, not averages.
v3: The backtesting insight
The breakthrough came from an analogy to quantitative finance. In finance, you don't model stock prices with probability distributions and compute expected returns. You backtest. You take historical price data for a specific day, run your strategy against it, and see what actually happens. No distribution assumptions, no parameter tuning, full determinism.
Transit works the same way. Every transit authority publishes their timetable in GTFS format, a set of CSV files listing every trip, every stop time, every calendar exception. Pick a date, filter the active services, and you get the exact schedule for that day. Every train departure, every arrival, no ambiguity.
Probabilistic
Model headways as distributions. Compute expected wait times. Score routes by expected total.
Σ parameters to tune
No ground truth to validate
GTFS backtesting
Parse real timetable. Pick a date. Simulate exactly. Every train is deterministic.
1,262,131 edges
Zero free parameters
This gives us the time-expanded graph. Every node is a (station, departure_time) pair
from the actual schedule. The solver doesn't estimate wait times. It knows exactly when the next train leaves,
because it's in the timetable. And you can test against any historical date, comparing routes across
weekdays, weekends, and disrupted services.
The time-expanded graph in detail
The problem on this graph is a Covering TSP: visit at least one node per required station (not every node), no return to start, minimizing elapsed time. NP-hard in general. Three edge types encode the full timetable:
- Transit legs: (Baker St, 8:12) → (Bond St, 8:15), a specific train departure
- Wait arcs: (Baker St, 8:07) → (Baker St, 8:12), waiting for the next train
- Walking transfers: (Baker St, 8:07) → (Oxford Circus, 8:19), running between stations
The solver finds the minimum-time path that touches at least one node per required station. I use Dijkstra with early termination (~0.02s per step) on this graph.
Building the TEG starts with parsing the GTFS feed: stops.txt for station coordinates, stop_times.txt for every scheduled arrival and departure, trips.txt to group stop times into trips, and calendar.txt plus calendar_dates.txt to filter down to the services actually running on a given date. For London, that's about 11,000 trips on a typical weekday. Once the nodes and transit edges are placed, I compute walking transfers using a KDTree spatial index over station coordinates, filtered to cross-line pairs within 1.2km, then hit the Google Routes API for actual walking times on the 723 pairs that survive the dominance prune (described below). The whole graph builds in about 8 seconds on a single core.
The Kensington Olympia breakthrough
The single biggest optimization, worth 40 minutes, came from one station. Kensington Olympia is served by a shuttle from Earl's Court that runs 9 times per day: 5 morning (05:49–07:20), 4 evening (20:01–20:40). That's 9 TEG nodes for the entire day. Compare to Oxford Circus with hundreds.
The greedy heuristic can't see 14 hours ahead. It optimizes locally, and by the time it reaches Earl's Court it's 20:41, one minute after the last shuttle. Cue a 60+ minute detour.
The fix is a prefix strategy: force K.O. first, before the greedy solver runs. This is a form of hard station scheduling: identify bottleneck stations with constrained service windows and handle them explicitly.
Reach Earl's Ct at 20:41
Last shuttle was
20:4060+ min backtrack detour
Earl's Ct →
05:49 shuttleK.O. done → continue NN
40 min saved
Urgency scoring and phase transitions
K.O. isn't the only bottleneck. Mill Hill East (single-track Northern line branch, limited evening service) and Heathrow T4 (separate Piccadilly loop) are also fragile. The solver detects these automatically by analyzing TEG sparsity.
We bias the nearest-neighbor with an urgency term:
score = travel_time + urgency_weight * time_until_last_service(station)
The weight has a sharp phase transition. From Euston Square:
From Finchley Road the urgency weight doesn't matter because the route naturally covers Mill Hill East regardless. The solver's best variant: K.O. prefix + k=5 NN + urgency 0.5 + T4 injection via Hatton Cross.
The randomized search
The deterministic greedy solver is fast but rigid. It always makes the same choice at each step, so it always produces the same route from a given start. To escape that, I added epsilon-greedy randomization: at each step, with probability epsilon (typically 0.05 or 0.1), the solver picks a random unvisited station instead of the nearest one by urgency-weighted score. This injects enough variation that different trials explore meaningfully different orderings.
I ran 80,000+ trials across different start stations and start times. Each trial takes about 0.3 seconds on the TEG (Dijkstra dominates), so the full sweep runs in a few hours. The key insight was that the randomized solver had to include the hard station logic from the deterministic solver. Early versions of the randomized search just picked random stations without prefix visits or junction grabs, so they'd regularly miss K.O. or Terminal 4 and produce 271/272 routes. Once I wired the pairings into every random trial, the hit rate for 272/272 routes jumped from under 5% to over 60%.
The distribution of results is interesting: most 272/272 trials land between 17h and 18h, with a long tail down toward 16h45m. The best route found so far sits well into that tail, which suggests there's probably still room below 16h30m with more trials or smarter perturbation strategies.
Results
Improvement curve
The solver improved from 19h42m to 16h45m across 19 incremental improvements. Each improvement was saved with the full 272-station route.
| # | Time | Start Station | Start | Method | vs Record |
|---|---|---|---|---|---|
| 1 | 19h42m | Battersea Power Station | 05:00 | Deterministic pairings | +117 min |
| 3 | 18h43m | Arsenal | 05:35 | Deterministic pairings | +58 min |
| 5 | 18h16m | Bromley-by-Bow | 05:30 | Deterministic pairings | +31 min |
| 6 | 18h03m | Chesham | 05:00 | Deterministic pairings | +18 min |
| 7 | 17h30m | Upminster | 06:14 | Deterministic pairings | −15 min |
| 8 | 17h12m | Upminster | 06:15 | Randomized (eps=0.05) | −33 min |
| 15 | 17h01m | Upminster | 05:57 | Randomized (eps=0.05) | −44 min |
| 16 | 16h58m | Upminster | 05:59 | Randomized (eps=0.05) | −47 min |
| 17 | 16h57m | Upminster | 06:02 | Randomized (eps=0.05) | −48 min |
| 18 | 16h53m | Upminster | 06:05 | Randomized (eps=0.1) | −52 min |
| 19 | 16h45m | Upminster | 06:09 | Randomized (eps=0.1) | −60 min |
The two biggest jumps: start station discovery (Chesham→Upminster, −33 min) and randomized search with pairings (deterministic→randomized, −45 min from the same station).
Multi-city
| City | Stations | Time | TEG |
|---|---|---|---|
| London Underground | 272 | 16h45m | 160K nodes / 1.26M edges |
| NYC Subway | 475 | 21h35m | 228K nodes / 656K edges |
| Berlin U-Bahn | 170 | 7h53m | smaller, denser |
Same algorithm, different GTFS feeds. Drop in a config and solve.
What didn't work
- k=3 lookahead consistently misses 5 branch-end stations. The multi-step penalty defers branches the solver should visit early.
- TEG local search (2-opt/or-opt) finds no improvement. Swapping two stations cascades through the entire route: different arrival times lead to different available trains, which changes the entire downstream path. The landscape is rugged.
- Line-aware scoring with a 30% same-line bonus never achieves 272/272.
- 500-trial randomized search (old) without pairings couldn't find 272/272. Fixed by adding hard station logic to the randomized solver.
- Evening K.O. injection arrives Earl's Court 20:41, last shuttle 20:40. One minute too late.
The fundamental issue: local search doesn't work on TEGs. Small reorderings cascade because arrival times shift, changing which trains are available everywhere downstream. Classical TSP moves assume edge weights are fixed. Here they're not.
Running transfer filtering: $375 → $7
Tube Challenge competitors run between stations. It's sometimes faster to run from a branch end to a nearby station on a different line than to backtrack. Computing running times needs the Google Routes API. All-pairs for 272 stations: 37,128 queries, ~$375.
The dominance prune
For each candidate pair (A, B):
train_time = shortest_path(A, B) // optimistic: no headway wait
if crow_flies ≥ train_time: eliminate // running can never win
Both sides are lower bounds. If the train's lower bound beats running's lower bound, the inequality holds for all real values. The prune is provably correct and cannot discard a useful shortcut. Eliminates 93% of remaining pairs. These 723 pairs are now integrated into the TEG as running transfer edges.
What got us past the record
Three changes took the solver from 18h03m (18 min behind) to 16h45m (60 min ahead):
- Pairings-aware randomized search. The randomized solver (80K+ trials) wasn't using hard station logic. Adding prefix visits and junction grabs to every randomized trial was the single largest code change. Without it, randomized trials handle K.O., Terminal 4, and Mill Hill East suboptimally.
- Start station discovery. Sweeping all 272 stations at 5-minute granularity found that Upminster (eastern terminus of District line) beats Chesham (northwestern Met line) by over an hour. You can't know this without exhaustive search.
- Start time sensitivity. Switching from 30-minute to 1-minute start time resolution. 06:09 beats 06:15 by 15 minutes due to train connection cascades downstream.
Upminster works because of its position in the network. It's the eastern terminus of the District line, which gives it direct access to a long east-west corridor through central London. Starting there early (around 06:09) means the solver can sweep the eastern branches first while morning service is ramping up, then move west through the dense central zone during peak frequency. The alternative start stations I tried in northwestern London (Chesham, Amersham) force the solver to backtrack south through the Metropolitan line before it can reach the central network, burning 30-40 minutes on low-frequency branch service.
The start time sweep was 272 stations times roughly 200 candidate start times (every minute from 05:00 to 08:20), so about 54,000 deterministic trials. Most stations cluster within 30 minutes of each other, but outliers like Upminster and a few District/Central line termini sit well below the median. The sensitivity to start time is surprisingly sharp: at Upminster, 06:09 catches a specific District line train that connects to a Hammersmith & City service at Barking, and missing that by 6 minutes means waiting for the next one, which cascades into a later arrival at every subsequent junction.
What's next
- LP relaxation on TEG. The current bound (7h43m, static graph) is very loose. A TEG-based LP would prove how close 16h45m is to optimal.
- More cities. Tokyo, Chicago, Singapore. Each has different topology characteristics that stress different parts of the algorithm.
- Longer randomized runs. The improvement curve hasn't plateaued. More trials from more start stations may push below 16h30m.