The Travelling Salesperson Problem (TSP) seeks the minimum weight tour visiting every city node exactly once and returning to the origin city.
Initial state: visited mask = 0001 (City 0 visited), current position = 0.
Mask: 0001 | Current City: 0 | Cost: 0Recursively branch to unvisited cities (1, 2, 3), memoizing minimum costs for sub-states (mask, city).
Path: 0 -> 1 -> 3 -> 2 -> 0 vs 0 -> 2 -> 3 -> 1 -> 0Combine shortest sub-path costs to compute final minimum tour distance returning to City 0.
Optimal Tour Cost: 35 | Tour Path: 0 -> 1 -> 3 -> 2 -> 0A delivery truck must visit 5 neighborhood addresses and return to the warehouse using the shortest total driving distance.
500 steps (Linear)
O(N × W) Memoized Time
O(N × W) Cache Matrix
Memory growth rate as N expands.
Relative efficiency rating for large N.
| Operation | Time Complexity | Space Complexity |
|---|---|---|
| Bitmask Dynamic Programming | O(N² × 2ᴺ) | O(N × 2ᴺ) |
function tsp(dist) {
const n = dist.length;
const memo = Array.from({ length: 1 << n }, () => new Array(n).fill(-1));
// bitmask DP solver
}Calculates optimal package delivery paths for FedEx/UPS.
Minimizes robotic arm movement across PCB hole coordinates.
Validate your conceptual understanding, operation mechanics, and core concepts of Tsp.
Using Held-Karp Bitmask DP reduces TSP runtime from O(N!) factorial down to O(N² 2ⁿ) exponential!
4 Cities loaded. Run solver to compute minimum cyclic tour cost.
-
-