3.7.15 · D1Algorithm Paradigms

Foundations — Bitmask DP — TSP intro

2,014 words9 min readBack to topic

Before you can read the parent note, you must own every piece of notation it throws at you. This page builds each one from absolute zero — plain words first, then the picture, then why the topic needs it. Read top to bottom; each block leans on the one above.


0. What problem are we even solving?

Look at the picture below. There are 4 dots (cities) joined by lines (roads). The red loop is one possible tour; there are many others. We want the cheapest loop among all of them.

Figure — Bitmask DP — TSP intro

1. — the number of cities

The picture: dots on paper. Naming them instead of "Paris, Rome…" is not laziness — it means a city's name is an index we can plug straight into an array. That habit is what makes the code short.

Why the topic needs it
every loop and every array size below is written in terms of .

2. — the cost table

Picture a spreadsheet: cell (row , col ) holds one road's price. The parent's 3-city example filled in dist[0][1]=10 and so on.


3. Sets, and why "which cities visited" is a set

Figure — Bitmask DP — TSP intro
Why the topic needs it
the set is one of the two indices of the DP table.

4. Binary digits — turning a set into one integer

Here is the trick that names the whole technique. We need to use a set of cities as an array index, and array indices must be plain integers. How do we squeeze a set into a single number?

Figure — Bitmask DP — TSP intro
Why the topic needs it
an integer is the cheapest possible array index, and there are exactly of them for cities — the second index of the DP table.

5. and — how many masks, and "all visited"


6. The bit operations — how we read and edit a mask

We picture a mask as a row of light switches, one per city. These four moves are all we ever need. Here &, |, ~ act on each bit position independently.

See Bit Manipulation for these operators in full generality.


7. Iterating over subsets — where the masks come from


8. , , and "impossible states"

Why the topic needs it
the DP fills a table of minimums; slots we can't legally reach stay and are silently ignored.

9. The DP table itself — dp[mask][i]

Now every symbol in the parent's central definition is earned:

That is the parent's mnemonic SET + SPOT — two indices, one for which cities, one for where you are. Branch and Bound attacks the same TSP differently (by pruning), but Held–Karp fills this table exhaustively and cleverly.


Prerequisite map

Cities named 0 to n-1

dist grid of costs

Set of visited cities

Binary digits one bit per city

Bitmask an integer per set

Bit ops test add remove

Loop 0 to 2 pow n minus 1

dp mask i table

min and infinity

TSP cheapest tour


Equipment checklist

Cover the right side and answer aloud. If any stalls, reread its section.

What does naming cities buy us?
A city's name is directly an array index, so cities plug straight into dist[i][j] and into bit .
What does hold, and what shape is it?
The cost to go from city to city ; a 2-D grid (spreadsheet), mirrored across the diagonal if roads are two-way.
Why do we store the set of visited cities instead of the full path order?
Future cost depends only on which cities are ticked plus your current spot; order is already optimised inside the dp value, so keeping it would re-explode to .
Convert visited set (with ) to a mask integer.
Bits (3,2,1,0)=0101 = 5.
What is ?
The integer with only bit set, equal to .
Give the three edits: test / add / remove city .
Test mask & (1<<i) non-zero; add mask | (1<<i); remove mask & ~(1<<i).
What integer is and what set does it mean?
, binary all-ones — every city visited.
How many masks are there, and how do we visit them all?
of them; for mask in range(1<<n) — counting in binary enumerates every subset once.
Why do impossible states hold ?
They're unreachable placeholders; any real cost beats in a , so they never corrupt the answer.
State dp[mask][i] in one sentence.
Cheapest path starting at 0, having visited exactly mask, ending at city (bit must be set).