Skip to the workspace
Pebellslearn by building
Hard Problems & Adversarial Search  ›  Lesson

Capstone: Meridian's Bid Desk

55 minRun the heuristic router against a fixed seeded benchmark and report how it actually performs against the MST lower bound

dsa / Capstone: Meridian's Bid Desk

Explore each scene at your own pace.

01 · Run the benchmark, read what actually happened

Run the benchmark, read what actually happened

Capstone: Meridian's Bid Desk

Definition
This capstone measures two approximate tour methods on a fixed set of seeded instances, comparing each result to a minimum-spanning-tree lower bound: the spanning-tree shortcut is provably within twice the optimum, while nearest-neighbour has no such guarantee.
In plain words
The bid desk tests two route-planning shortcuts on the same batch of tenders and records how far each lands from the best possible. One carries a proven limit on how bad it can get; the other might do well or badly, and only the measurements tell you.

In short: this section built four separate pieces of hard-problem machinery — brute force vs. verification, a reduction between two NP-hard problems, a provably-bounded approximation, and a search over an opponent who's also optimising. The bid desk is where the approximation piece earns its keep for real: a fixed, seeded benchmark of tender instances, scored two ways — the no-guarantee nearest-neighbour heuristic, and the MST-shortcut approximation with its proven ratio of at most 2. Measured honestly, one of them can break its informal expectations. The other, provably, cannot.

Run the benchmark, read what actually happened

step through it
1import heapq
2import random
3 
4def manhattan_edges(points):
5 cities = list(points)
6 edges = []
7 for i in range(len(cities)):
8 for j in range(i + 1, len(cities)):
9 a, b = cities[i], cities[j]
10 (x1, y1), (x2, y2) = points[a], points[b]
11 edges.append([a, b, abs(x1 - x2) + abs(y1 - y2)])
12 return edges
13 
14def mst(cities, edges):
15 adj = {c: [] for c in cities}
16 for a, b, cost in edges:
17 adj[a].append((cost, b))
18 adj[b].append((cost, a))
19 start = cities[0]
20 visited = {start}
21 tree = {c: [] for c in cities}
22 weight = 0
23 heap = [(cost, b, start) for cost, b in adj[start]]
24 heapq.heapify(heap)
25 while heap and len(visited) < len(cities):
26 cost, node, parent = heapq.heappop(heap)
27 if node in visited:
28 continue
29 visited.add(node)
30 weight += cost
31 tree[parent].append(node)
32 tree[node].append(parent)
33 for c, nxt in adj[node]:
34 if nxt not in visited:
35 heapq.heappush(heap, (c, nxt, node))
36 return tree, weight
37 
38rng = random.Random(7)
39worst_ratio = 0.0
40for _ in range(15):
41 n = rng.randint(4, 9)
42 points = {f"c{i}": (rng.randint(0, 30), rng.randint(0, 30)) for i in range(n)}
43 cities = list(points)
44 edges = manhattan_edges(points)
45 tree, weight = mst(cities, edges)
46 if not weight:
47 continue
48 # preorder walk, shortcutting repeats
49 order, seen = [], set()
50 def dfs(node):
51 seen.add(node)
52 order.append(node)
53 for nxt in sorted(tree[node]):
54 if nxt not in seen:
55 dfs(nxt)
56 dfs(cities[0])
57 dist = {}
58 for a, b, cost in edges:
59 dist[(a, b)] = cost
60 dist[(b, a)] = cost
61 cost = sum(dist[(order[i], order[(i + 1) % len(order)])] for i in range(len(order)))
62 worst_ratio = max(worst_ratio, cost / weight)
63print(round(worst_ratio, 4))

At seed=7, fifteen trials, the MST approximation's worst ratio lands at 1.9535 — under the 2x ceiling, as the proof in lesson 3 said it must. Run bid_desk_benchmark(7, 15) in full and the nearest-neighbour column tells a different story: 2.0465, past 2x. Nothing about nearest-neighbour is broken — it's a perfectly reasonable heuristic most of the time — but nothing was ever proven about it either, and this benchmark is where that absence shows up as a real number, not a footnote.

Scene 1 of 5
Project context and further resources
You're building a piece ofMeridian — the bid desk
This piece — bid_desk_benchmark(): Runs the heuristic router against a fixed benchmark set and reports how close it stays to the MST lower bound.
Scenario Meridian's bid desk has to commit to a route-quoting method before the tender deadline, not after proving something new about TSP. Run the heuristic router against a fixed, reproducible benchmark set and report, honestly, how close it stays to the MST lower bound -- and how a method with no proof behind it can do worse.
bid_desk_benchmark.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
bid_desk_benchmark(seed, trials) → dictHow the nearest-neighbour heuristic and the proven MST approximation each performed against the MST lower bound, across a fixed seeded benchmark set.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.