Bitmask DP — TSP intro
WHY a bitmask?
The state and recurrence (derive from scratch)
HOW do we build it? Think about the last step. To be standing at having visited mask, you must have just arrived from some earlier city that is also in mask, , having visited mask minus . We minimise over all such :
Complexity (and the 80/20)

Worked example 1 — 3 cities by hand
Cities . Distances (symmetric): .
Base: dp[001][0] = dp[1][0] = 0.
| Step | State | Compute | Why this step? |
|---|---|---|---|
| 1 | dp[011][1] |
dp[001][0]+dist[0][1] = 0+10 = 10 |
Add city 1 from start. mask 011={0,1}, last=1. |
| 2 | dp[101][2] |
dp[001][0]+dist[0][2] = 0+15 = 15 |
Add city 2 from start. |
| 3 | dp[111][2] |
dp[011][1]+dist[1][2] = 10+20 = 30 |
All visited, came from 1→2. |
| 4 | dp[111][1] |
dp[101][2]+dist[2][1] = 15+20 = 35 |
All visited, came from 2→1. |
| 5 | Close | Return to start. | |
| Tour cost = 45. |
Both routes and cost 45 (symmetric triangle), so 45 is optimal. ✓
Worked example 2 — reading the bits
Suppose and you're computing dp[mask][3] with mask = 1011 (binary) .
- Why this step? Bit 3 of
1011is1?1011→ bits are (3210)=1011, bit3=1✓ so city 3 is allowed as the endpoint. prev = mask & ~(1<<3) = 1011 & 0111 = 0011.- Why this step? We must have arrived at 3 from a city already in
prev, so . dp[11][3] = min( dp[3][0]+dist[0][3], dp[3][1]+dist[1][3] ).
Reference implementation
INF = float('inf')
def tsp(dist):
n = len(dist)
dp = [[INF]*n for _ in range(1<<n)]
dp[1][0] = 0 # base: at 0, visited {0}
for mask in range(1<<n):
for i in range(n):
if dp[mask][i] == INF: # unreachable, skip
continue
for k in range(n): # try going to city k next
if mask & (1<<k): # already visited -> skip
continue
nm = mask | (1<<k)
nd = dp[mask][i] + dist[i][k]
if nd < dp[nm][k]:
dp[nm][k] = nd
full = (1<<n)-1
return min(dp[full][i] + dist[i][0] for i in range(1,n))Note: this version pushes forward (mask → nm); the recurrence above pulls backward. Same DP, two iteration directions — both valid because masks only grow.
Recall Feynman: explain to a 12-year-old
Imagine you must visit every friend's house once and come home, using the least walking. Instead of remembering the whole path you took, you only jot down two things on a sticky note: which houses you've already been to (tick boxes) and whose house you're standing in right now. If two different walks led you to the same "tick boxes + current house," they're the same from now on — so you keep only the cheaper one and throw the other away. Those sticky notes are the bitmask. Throwing away duplicates is why it's fast.
Active recall
What does dp[mask][i] store in TSP bitmask DP?
mask, and ending at city i (bit i set in mask).Why does bitmask DP beat the brute force?
What is the base case?
dp[1][0] = 0 (only city 0 visited, standing at 0, cost 0); everything else .Write the transition.
dp[mask][i] = min over j in prev of dp[prev][j] + dist[j][i], where prev = mask & ~(1<<i).How do you get the final closed-tour answer?
min over i!=0 of dp[(1<<n)-1][i] + dist[i][0].How do you test if city i is in mask?
mask & (1<<i) is non-zero.How do you remove city i from mask?
mask & ~(1<<i).Time and space complexity?
Why must bit i be set in mask for dp[mask][i] to be valid?
What changes if you want a Hamiltonian path instead of a cycle?
+dist[i][0] return term; answer becomes min_i dp[full][i].Connections
- Dynamic Programming — state, optimal substructure, overlapping subproblems.
- Travelling Salesman Problem — the NP-hard problem this solves exactly for small .
- Held-Karp Algorithm — the formal name of this exact DP.
- Bit Manipulation — masks, shifts, and-not.
- Subset Enumeration — iterating over subsets.
- Hamiltonian Path and Cycle — what a valid tour is.
- Branch and Bound — alternative exact method for larger .
Concept Map
Hinglish (regional understanding)
Intuition Hinglish mein samjho
Dekho, TSP ka matlab hai: ek city se start karo, saari cities exactly ek baar visit karke wapas ghar aao, aur total distance minimum ho. Brute force me saare orders try karne padte hain — 12-13 city ke baad hi laptop hang. Bitmask DP ka jugaad ye hai: future ke liye sirf do cheezein important hain — kaun kaun si cities visit ho chuki hain aur abhi tum kahan ho. Order ki internal detail matter nahi karti, kyunki uska best cost dp value me already chhupa hua hai.
"Visited cities ka set" ko hum ek integer ke binary bits me store karte hain — isi ko bitmask kehte hain. Bit = 1 matlab city visit ho gayi. State banta hai dp[mask][i] = city 0 se shuru karke, exactly mask set visit karke, abhi city par khade ho — uska minimum cost. Transition simple: city par pahunchne se pehle kisi city par the (jo prev = mask minus me ho), to dp[mask][i] = min over j ( dp[prev][j] + dist[j][i] ).
Complexity ban jaati hai — exponential to hai, par se crores guna fast. tak comfortably chalta hai. Yahi 80/20 point hai: yaad rakho ki SET + SPOT do hi indices chahiye, aur end me ghar wapas aane ke liye + dist[i][0] add karna mat bhoolna. Bas itna pakka kar liya to TSP intro clear.