Submask Enumeration
Enumerating all submasks of a given mask
Given a bitmask , you want to efficiently iterate through all of its submasks, that is, masks in which only bits that were included in mask are set.
Consider the implementation of this algorithm, based on tricks with bit operations:
int s = m;
while (s > 0) {
... you can use s ...
s = (s-1) & m;
}or, using a more compact for statement:
for (int s=m; s; s=(s-1)&m)
... you can use s ...In both variants of the code, the submask equal to zero will not be processed. We can either process it outside the loop, or use a less elegant design, for example:
for (int s=m; ; s=(s-1)&m) {
... you can use s ...
if (s==0) break;
}Let us examine why the above code visits all submasks of , without repetition, and in descending order.
Suppose we have a current bitmask , and we want to move on to the next bitmask. By subtracting from the mask one unit, we will remove the rightmost set bit and all bits to the right of it will become 1. Then we remove all the "extra" one bits that are not included in the mask and therefore can't be a part of a submask. We do this removal by using the bitwise operation (s-1) & m. As a result, we "cut" mask to determine the highest value that it can take, that is, the next submask after in descending order.
Thus, this algorithm generates all submasks of this mask in descending order, performing only two operations per iteration.
A special case is when . After executing we get a mask where all bits are set (bit representation of -1), and after (s-1) & m we will have that will be equal to . Therefore, with the mask be careful — if the loop does not end at zero, the algorithm may enter an infinite loop.
Iterating through all masks with their submasks. Complexity
In many problems, especially those that use bitmask dynamic programming, you want to iterate through all bitmasks and for each mask, iterate through all of its submasks:
for (int m=0; m<(1<<n); ++m)
for (int s=m; s; s=(s-1)&m)
... s and m ...Let's prove that the inner loop will execute a total of iterations.
First proof: Consider the -th bit. There are exactly three options for it:
- it is not included in the mask (and therefore not included in submask ),
- it is included in , but not included in , or
- it is included in both and .
As there are a total of bits, there will be different combinations.
Second proof: Note that if mask has enabled bits, then it will have submasks. As we have a total of masks with enabled bits (see binomial coefficients), then the total number of combinations for all masks will be:
To calculate this number, note that the sum above is equal to the expansion of using the binomial theorem. Therefore, we have combinations, as we wanted to prove.