Introduction to Bitmask DP

Subsets as binary numbers

You need to assign nn workers to nn tasks. Each worker has different costs for different tasks. Find the minimum total cost. There are n!n! possible assignments. For n=20n = 20, that's over 2×10182 \times 10^{18} possibilities. So... impossible.

But you don't need to track the order of assignments, just which workers are used. That's 2n2^n states. For n=20n = 20, that's about a million. Bitmasks let you represent "which elements are chosen" as a single integer. I'll show you how to think in subsets.

unnamed - 2026-01-20T065255.134.jpg