Bitmask DP (State Compression)
7 min read.
Use bits to represent subset states, enabling DP over all subsets efficiently. Essential for Travelling Salesman, subset selection, and assignment problems.
Think of it this way: Imagine tracking which cities a salesman has visited using light switches. Each switch ON/OFF represents visited/not visited. Instead of storing lists, you store a single number whose binary bits encode the entire state.
Start at city 0 with only city 0 visited: dp[0001][0] = 0.
Check your understanding
Answer from memory before you scroll back up. Pulling an idea out of your head is what makes it stay there.
Subsets as integers
Sort each bitmask expression by what it does to the subset.
Tap a piece, then tap where it goes — or drag it. Tap the tray to take one back.
Test membership
Drop items hereAdd an element
Drop items hereRemove an element
Drop items hereQuiz
20 questionsHow do you iterate over all subsets of a bitmask m?
Done with this lesson?
Sign in free to keep your progress, streak and quiz scores.