I am trying to learn bit masking with dynamic programming but I'm failing to understand the overlapping sub problems for a case. Can someone please explain how the sub problems overlap based on any example they feel fit for explaining easily?
2 Answers
Let's take the example for a Shortest Hamiltonian walk
, In this problem we need to find a Hamiltonian walk that is the shortest where each edge has a certain amount of weight associated with it.
Hamiltonian walk is where we visit each and every node
in the graph exactly once
.
This problem can be solved using DP Bitmasks
for small no of nodes. So what we do is to keep a Bitmask
to keep track of which nodes we have visited in the current state, and then we can iterate over all the nodes not visited using the mask
we can go to different states.
Now suppose a subproblem lets say of k
no of nodes is computed, this solution of k
nodes constitutes of smaller subproblems, that form a larger solution of k nodes, i.e initial solution had only 2 nodes, then 3 and so on when we reached the kth
node.
Now let's take another subproblem that constitutes of let's say m
nodes also exists.
Now there is an edge from a node in the first subproblem to a node in the second subproblem and we want to join these 2 subproblems, so in this case all the smaller subproblems of the k
nodes are also smaller subproblems of the whole combined solution and hence here is referred to as overlapping as it is present in both the first subproblems and the larger combined subproblem.
In order to avoid redundant calculation of these overlapping subproblems we use the concept of memoisation
, i.e once we have the answer to a overlapping subproblem we store it for later use.
Also note that in the above 2 subproblem's no vertex should be present in both the smaller subproblem which we can check using the corresponding bitmasks.

- 1,826
- 2
- 15
- 18
I am not entirely sure if this is what you are asking. But an example which is sadly not in the domain of bit masking problems would be the de facto beginner's example to DP: Fibonacci sequence.
As you probably know, Fibonacci sequence is defined roughly as follows.
F(n) = F(n-1) + F(n-2)
F(0) = F(1) = 1
Now, say you wanted to find F(8). Then you're actually looking for F(7) + F(6). To find F(7), you need F(6) and F(5). And to find F(6), you need F(5) and F(4).
As you see, both F(6) and F(7) require solving F(5), which means they are overlapping. Incidentally, F(7) requires solving the entire problem F(6) as well, but that need not always be the case for each DP problem. In essence, sometimes your subproblems A and B may both depend on a lower-level subproblem C, in which case they are considered overlapping.

- 4,477
- 7
- 27
- 46