Dynamic Programming21 sections · 916 units
Open in Course

TSP - key observation

Visited set + current city

When planning the next move, you only care about which cities you've visited and where you are now. The exact path taken doesn't matter. If you've visited {1,2,4}\{1, 2, 4\} and are at city 44, the remaining problem is the same whether you came via 1241 \to 2 \to 4 or 2142 \to 1 \to 4.

This overlap is what DP exploits. The bitmask tracks visited cities. Combined with current city, this fully describes the state. That's n2nn \cdot 2^n states instead of n!n! paths.