Shuna Maekawa / Projects

Held–Karp TSP Visualizer

2025-10-13 · Web App, Next.js, TypeScript, Cloudflare Workers, Algorithms, TSP

A Held–Karp TSP visualizer that uses realistic travel-time cost matrices.

Held–Karp TSP Visualizer

Overview

I wanted a small, honest demo of what an exact TSP looks like when you use real travel times instead of straight-line distances. So I built a visualizer that runs the classic Held–Karp dynamic programming algorithm on a cost matrix sourced from road networks and traffic.

You pick points, it computes the optimal order, and it shows the route with the total travel time. No heuristics, no hand-waving—just a clear, step-by-step solution you can read and reason about.

The result is a ranked visit sequence and total travel time that reflect on-the-ground conditions.


Why Held–Karp?

Heuristics are fast and often good enough, but they can be opaque when you want to understand “why this order?” This project favors clarity over raw speed: Held–Karp is exponential, yet for small to medium sets it’s fast enough and provides ground truth you can compare against or use to benchmark heuristics.


Features


Tech Stack

Frontend

Runtime & API


How It Works

  1. Construct a pairwise travel-time cost matrix by batching distance matrix requests (details below), using driving mode with a specific departure time for traffic-aware durations.
  2. Run the Held–Karp dynamic programming algorithm on the matrix to compute the exact optimal route.
  3. Reconstruct the optimal visit order by backtracking predecessors from the DP table.
  4. Render the ordered sequence and total travel time in the UI.

This approach provides correctness (exact TSP on the given matrix) and realism (costs based on road networks and traffic).


Distance Matrix: Traffic-aware durations and batching

In plain terms: we ask the distance matrix service for travel times that reflect road networks and current or predicted traffic, then stitch the responses into a full N×N cost matrix.


Algorithm Details: Held–Karp DP

Intuition: think of DP[S, j] as “the cheapest way to start at s, visit exactly the nodes in S, and arrive at j.” We grow subsets one node at a time and always pick the best predecessor, then backtrack to recover the optimal order.

Held–Karp solves TSP exactly using dynamic programming over subsets:



Links

Held–Karp TSP Visualizer screenshot
Held–Karp TSP Visualizer screenshot