What Bitmask DP Solves

TSP, assignments, partitions

Bitmask DP solves problems where you need to track which elements from a small set (n20n \le 20) have been selected. Classic examples include TSP, assignment problems, and counting valid arrangements.

This works because an integer with nn bits can represent any subset of nn elements. Bit ii being 11 means element ii is included. This gives 2n2^n possible states, which is manageable for small nn.

unnamed - 2026-01-20T063541.394.jpg