Using Dynamic Programming (DP) with Bit Masking is a very common and powerful technique in competitive programming and algorithm design, especially when dealing with combinatorial optimization problems involving subsets.
Here’s a detailed explanation to help you know when and why to use DP with Bitmasking:
🧠 Key Idea:
When the state of your problem involves keeping track of a combination of items (like whether some objects have been used), you can use bits (in an integer) to represent the subset of items. Then, you use DP to memorize the solutions for each state.
✅ WHEN to Use DP with Bitmasking
1. When You Need to Track Subsets Efficiently
Bitmasking is extremely useful when you're trying to keep track of which elements in a set of size N ≤ 20 have or haven’t been used yet. Each subset can be represented as an N-bit number, where the i-th bit indicates whether the i-th item is included (1) or not (0).
✔️ Good when N ≤ 20 (sometimes up to 24 depending on constraints).
2. Typical Scenarios That Work Well
🔹 Traveling Salesman Problem (TSP)
You are given a cost matrix of traveling between cities. You must find the minimum cost to visit all cities exactly once and return to the starting point.
- Mask represents the set of visited cities.
- DP[i][mask] = minimum cost to reach city
i after visiting cities in mask.
int dp[1<<N][N];
// where N is the number of cities
🔹 Counting / Finding Ways for Subset Traversals or Assignments
For example, assigning tasks to people such that each person gets one task.
- DP[mask] = number of ways to assign tasks to people in current mask.
- If mask represents assigned tasks, each state transition assigns a new task to a person.
🔹 Bit DP for Game States / Puzzles / Matching Problems
You can use bitmask to represent:
- Game board states (on/off tiles, visited/unvisited paths),
- Matching in bipartite graphs (e.g. minimum-weight perfect matching),
- Set of used cards, switches, or items.
🔹 Combining DP with Recursive Backtracking
Many backtracking problems can be optimized by memoizing states represented with bitmasks.
🧩 Common State Format:
dp[pos][mask]
pos may represent the current index/city/task
mask represents the "state", which elements are used/chosen/etc.
⏳ Time Complexity
If you have N elements, the number of possible masks is 2^N.
If for each mask, you have up to N transitions, then:
This is acceptable for:
- N ≤ 20 ⇒ up to 20 * 2^20 ≈ 20 million operations.
✅ Summary — Use DP + Bitmask When:
| Condition | You should use bitmask DP |
|---|
| Need to keep track of subsets | ✅ |
| Number of elements ≤ 20-24 | ✅ |
| Each element is either selected or not | ✅ |
| Need to memoize visited combinations | ✅ |
| Minimization/Maximization over combinations | ✅ |
| Problem mentions "visit all", "select subset", "match pairs" | ✅ |
🧪 Example Problem:
"Given n tasks and n workers. Each worker can do one task with a specific cost. Find the minimum total cost of assigning all tasks to each worker uniquely."
n <= 20
- Use bitmask to track assigned tasks
def assign_tasks(cost):
n = len(cost)
dp = [-1] * (1 << n)
def solve(mask):
worker = bin(mask).count('1')
if worker == n:
return 0
if dp[mask] != -1:
return dp[mask]
ans = float('inf')
for task in range(n):
if not (mask & (1 << task)):
ans = min(ans, cost[worker][task] + solve(mask | (1 << task)))
dp[mask] = ans
return ans
return solve(0)
If you have a specific problem in mind, feel free to paste it here — I can help you apply bitmask DP to it.