Recently when reading StackExchange, I found this question Algorithm wanted: Enumerate all subsets of a set in order of increasing sums

Now, I am wondering if there is any way to find $k$-th subset faster than generating first $k$ subsets.

Or is there any faster way to generate $k$ first subsets than $O(k^2)$ algorithm presented in the answer for question above?


This problem has been answer by David Eisenstat on Stackoverflow. I assume that the universe contains only positive numbers.

The idea is to "conceptually" construct a binary heap, whose nodes correspond to all subsets and then pick the $k$-th smallest node in the heap. The root of the heap is a singleton set containing the smallest element. The possible two children of a heap node corresponding to a subset $S$ are:

  1. $S \setminus \{\max(S)\} \cup \{\text{next}(S)\}$, where $\text{next}(S)$ is the smallest element in the universe that is larger than all elements in $S$.
  2. $S \cup \{\text{next}(S)\}$.

Let the heap be $H$. Since building this heap takes $O(2^n)$, we won't build the whole heap. Our algorithm only depends on a way of producing children of a given heap node.

We create another heap $Q$ and each node corresponds to a node in $H$. Initially, $Q$ contains only one node, which is the root of $H$. We repeat the following process $k-1$ times: Let the minimum of $Q$ be $x$. Delete $x$ from $Q$ and then add $x$'s children in $H$ to $Q$.

In the end, the root of $Q$ corresponds to the $k$-th subset.

In order to produce children in $O(1)$ time, we need a way to compute $\max(S)$ and $\text{next}(S)$ efficiently. Thus, for each heap node in $H$, we store the rank (in the universe) of the largest element in $S$. If elements in the universe are sorted, accessing the $p$-th smallest element in the universe can be done in $O(1)$ time. Hence, $\text{next}(S)$ can also be computed in $O(1)$ time.

Since each node only stores the rank of the largest element, we should also maintain parent links and other auxiliary information so that the subset can be reconstructed.

Overall, if the universe is already sorted, this algorithm finds the $k$-th subset in time $O(k \lg k)$.

Theoretically, selection in a binary heap can be done in $O(k)$ time [link].

  • $\begingroup$ Isn't it gonna take $O(2^n * log (k))$ time? This algorithm creates all subsets. $\endgroup$ – user128409235 Nov 1 '15 at 14:24
  • $\begingroup$ You don't need to build the whole binary heap. You just need a way to produce children of each node. Let me clarify this. $\endgroup$ – Yu-Han Lyu Nov 1 '15 at 14:33
  • 1
    $\begingroup$ In this case, in each node of $H$, you can store the subset and its indices explicitly to help comparison. Of course, the performance will degrade, since this method consumes more memory. $\endgroup$ – Yu-Han Lyu Nov 1 '15 at 17:07

For arbitrary ground sets, the answer is almost certainly no. However if the ground set has special structure, then the answer is yes.

Case 1: If the elements increase very quickly, i.e. each element is greater than the sum of all previous ones. For example, $\{1,2,4,8\}$. Then the desired enumeration is exactly lexicographic order, i.e. to compare set $S$ to set $T$, we first compare their largest elements. In case of tie, we compare the second largest, and so on. The resulting order is $$\{1\}<\{2\}<\{2,1\}<\{4\}<\{4,1\}<\{4,2\}<\{4,2,1\}<\cdots$$ which is bijective with binary representation of the naturals.

Case 2: If the elements increase very slowly, i.e. the sum of the $k$ largest elements is less than the sum of the $(k+1)$ smallest elements, for each $k$. For example, $\{11,12,13,14\}$. Then the desired enumeration is exactly graded lexicographic order, i.e. to compare set $S$ to set $T$, we first compare their cardinalities. In case of tie, we then use lexicographic order as in case 1. Now the ${n\choose 1}$ singleton sets come first, then the ${n\choose 2}$ doubleton sets, then the ${n\choose 3}$ tripleton sets, and so on. Within the doubleton sets, we order lexicographically, i.e. $$\{12,11\}<\{13,11\}<\{13,12\}<\{14,11\}<\cdots$$


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.