Hi Everyone, I encountered this subproblem in a problem.
Suppose we are given an array of integers of size N and want to generate subsets of size K from it. I used this simple backtracking approach to generate the combinations of size K.
Backtracking Code
I was wondering if there is a possible approach to do this by using bits, a simple approach is generating all subsets and processing only the ones that have size K, but in this, we have to generate all 2N subsets.
Bits Solution
Is there a way to do this using bits without generating all the possible subsets?.
One nice way to do this is to create a vector of $$$n-k$$$ 0's followed by $$$k$$$ 1's. Use
std::next_permutation
to iterate over all combinations.Indeed, a really nice solution. I didn't know that next_permutation takes care of duplicates as well, I thought that it will generate duplicates. I will look into how it actually works.
You can use Pascal's recursion. Go concatenating the string.
Can you elaborate more on this?
I suggest using
std::next_permutation
orstd::prev_permutation
for better complexity $$$O(\binom{n}{k})$$$There is an error in the sample code, in the second line it should be
p[N-i-1] = 1;
. Instead of generating the first subset, it is creating the last one.I fixed my code, thanks for reminding me
Kactl has really short iterative code for this in the "10.5.1 Bit Hacks" section, with the extremely nice properties that the bitsets are generated in increasing order and no other bitsets are generated; here's roughly what it is:
The loop bounds I've written only work for $$$n \leq 30$$$ (or 62 for long longs). It's a really good exercise in bit operations to think about why this works.
Note: if you are adapting this code to long longs, make sure you change things like (1 << k) to (1LL << k).
This is exactly what I was looking for and it looks like that the complexity is even better than the solution that Monogon suggested, as worst-case complexity for next_permutation is
O(n)
. I will now try to think about why it works.In case you need to find previous subset instead of next subset, you can you a trick to get the inverse function:
prev = ~next(~mask)
,next = ~prev(~mask)
Moreover, if you want to get the kth previous or kth next subset, you can use lexicographical dynamic programming for it.
Here I use mask of
long long
, you can change tobitset<>
to store larger but it will be a bit complicatedIn case guys interesting, there are some other effecient or non-effecient way to find it. (Sorry ! here I only provides implementations and not describe how these function works perfectly)
Can you please explain the divide part
(x ^ higher) / right) >> 2
You can output step-to-step bitwise for a few examples to understand it better. As it just does not naturally occur but I compact the code whenever it is possible.
But my ideas are:
IMO, you can come up to yourself a better or/and simpler solution and I'd love to see that.