Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's straightforward to construct each subset from the preceding subset. It's also straightforward to represent a subset as a 128-bit number (although obviously most values wouldn't map onto a qualifying subset, and I don't know if the value of 128 in the question was real or just an example.) That's certainly the first approach I would use; if it works, it's all O(1) and the storage cost is not extreme (16 bytes for indices instead of 4).</p> <p>If you really want to store concise indices for the subsets, I'd use the following recursion, where S(n,k) represents all the subsets (or the count of the subsets) of size &le; k from values &lt; n:</p> <p><code>s(n,0) = { {} }</code><br/> <code>s(n,k) = (s(n-1,k-1) &odot; {n}) &Union; s(n-1,k) if n &ge; k &gt; 0</code><br/> <code>s(n,k) = {} if n &lt; k</code></p> <p>where the operator <code>P &odot; S</code> means "Add <code>S</code> to each element of <code>P</code>" (and therefore the result is exactly the same size as <code>P</code>). So, viewed as a counting algorithm, we get:</p> <p><code>S(n,0) = 1</code><br/> <code>S(n,k) = S(n-1,k-1) + S(n-1,k) if n &ge; k &gt; 0</code><br/> <code>S(n,k) = 0 if n &lt; k</code> </p> <p>The second recursion can be re-expressed as:</p> <p><code>S(n,k) = &Sigma;<sup>n</sup><sub>i=k</sub>S(i-1,k-1)</code><br/> (which would have come out looking nicer with jsMath, grrr.)</p> <p>That's another way of saying that we'll generate the sets in order by the largest element, so we start with the set <code>{0...k-1}</code>, then all the sets with <code>{k}</code> as the largest element, then all the ones with <code>{k+1}</code>, and so on. Within each group of sets, we recurse to find the <code>(k-1)</code>-sized sets, again starting with the least maximum value and working our way up to one less than the maximum value we just selected.</p> <p>So we can figure out what the largest value in the indexed set for an index in <code>S(n,k)</code> by successively subtracting <code>S(i-1, k-1)</code> for <code>i</code> from <code>k</code> to <code>n</code> until the result would be negative; then we add <code>{i}</code> to the result set; reduce <code>k</code> by 1 and repeat with <code>n</code> now set to <code>i-1</code>. </p> <p>If we precompute the relevant tables of <code>S(n,k)</code>, of which there are about 640 valid combinations, we can use binary search instead of iteration to find the <code>i</code> at each step, so the computation takes time <code>k log(n)</code>, which is not awful.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload