Skip to the workspace
Pebellslearn by building
Planning DP  ›  Lesson

Bitmask DP & the Exact Tour

Bitmask DP 36 minWhen the state is a set, store the set as the bits of an integer

dsa / Bitmask DP & the Exact Tour

Explore each scene at your own pace.

01 · A set is the state

A set is the state

Bitmask DP & the Exact Tour

Definition
Bitmask dynamic programming stores a set of chosen items as the bits of one integer and uses that integer, with the current position, as the table index, so problems over subsets of up to about twenty items can be solved without trying every ordering.
In plain words
Imagine a row of light switches, one per stop, where on means visited. The pattern of switches is a single number, and the best route so far for each pattern is written down so it is never worked out twice.

In short: when the rest of a route depends only on which stops are already visited and where the van is now — never on the order it took to get there — the set of done stops is the whole state, and a set of up to about 20 items fits in the bits of one integer. dp[S][j] is the shortest route that visits exactly the stops in set S and ends at j; filling it in plain numeric order always has every smaller set ready first, because turning on a bit only ever makes a number bigger.

The bitmask 0b1011 drawn as four bit boxes 1,0,1,1 over positions 3,2,1,0, with stops 0, 1 and 3 circled as "in the set" and stop 2 crossed out as "not in the set"

The afternoon courier leaves the depot, drops parcels at a handful of stops and comes back. The streets are one-way, so going from stop 2 to stop 3 isn't the same distance as going from 3 to 2. With 4 stops there are 6 possible orders, and with 10 stops there are 362,880. Trying every order is fine today and too slow by next month. Guessing (always drive to the nearest unvisited stop) is fast, and it's often wrong by a lot.

Between those two there's an exact method that works well up to about 15 stops. It rests on one question: what does the rest of the route actually depend on? It doesn't depend on the order you've driven so far. It depends only on which stops are done and where the van is now.

A set is the state

Suppose two partial routes have both visited stops {0, 1, 3} and are both standing at stop 3. One took 14 km to get there and the other took 6. Whatever the best way to finish is, it's the same for both, because the same stops are left and the van is in the same place. So the 14 km route can never be part of the best tour, and you only need to remember the 6.

That gives the state:

dp[S][j] = the shortest route that starts at the depot, visits exactly the stops in set S, and ends at stop j.

To fill it, ask which stop came just before j. It was some k in S other than j, and the route to k visited exactly S without j:

dp[S][j] = min over k of dp[S − {j}][k] + dist[k][j]

Then the full tour is min over j of dp[all][j] + dist[j][0].

Scene 1 of 11
Project context and further resources
You're building a piece ofMeridian — the day planner
This piece — best_tour(): Sequences a small round of stops exactly, with a table indexed by the set of stops visited.
Scenario The afternoon courier covers a handful of stops on one-way streets and has to come back to the depot. There are few enough stops to find the exact best round rather than a good guess, as long as the planner doesn't try every order.
best_tour.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
best_tour(dist) → dictThe shortest round from depot 0 through every stop and back: its km and the order, starting and ending at 0.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.