Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is an implementation in Mathematica, from the package <a href="http://www.combinatorica.com/" rel="nofollow"><em>Combinatorica</em></a>. The semantics are fairly generic, so I think it is helpful. Please leave a comment if you need anything explained.</p> <pre><code>UnrankKSubset::usage = "UnrankKSubset[m, k, l] gives the mth k-subset of set l, listed in lexicographic order." UnrankKSubset[m_Integer, 1, s_List] := {s[[m + 1]]} UnrankKSubset[0, k_Integer, s_List] := Take[s, k] UnrankKSubset[m_Integer, k_Integer, s_List] := Block[{i = 1, n = Length[s], x1, u, $RecursionLimit = Infinity}, u = Binomial[n, k]; While[Binomial[i, k] &lt; u - m, i++]; x1 = n - (i - 1); Prepend[UnrankKSubset[m - u + Binomial[i, k], k-1, Drop[s, x1]], s[[x1]]] ] </code></pre> <p>Usage is like:</p> <pre><code>UnrankKSubset[1, 4, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}] </code></pre> <pre><b> {1, 2, 3, 5}</b></pre> <p>As you can see this function operates on sets.</p> <hr> <p>Below is my attempt to explain the code above.</p> <h3>UnrankKSubset is a recursive function with three arguments, called (m, k, s):</h3> <ol> <li><code>m</code> an Integer, the "rank" of the combination in lexigraphical order, starting from zero.</li> <li><code>k</code> an Integer, the number of elements in each combination</li> <li><code>s</code> a List, the elements from which to assemble combinations</li> </ol> <h3>There are two boundary conditions on the recursion:</h3> <ol> <li><p>for any rank <code>m</code>, and any list <code>s</code>, if the number of elements in each combination <code>k</code> is <code>1</code>, then:</p> <p>return the <code>m + 1</code> element of the list <code>s</code>, itself in a list.</p> <p>(<code>+ 1</code> is needed because Mathematica indexes from one, rather than zero. I believe in C++ this would be s[m] )</p></li> <li><p>if rank <code>m</code> is <code>0</code> then for any <code>k</code> and any <code>s</code>:</p> <p>return the first <code>k</code> elements of <code>s</code></p></li> </ol> <h3>The main recursive function, for any other arguments than ones specified above:</h3> <p>local variables: (<code>i</code>, <code>n</code>, <code>x1</code>, <code>u</code>)</p> <p><code>Binomial</code> is binomial coefficient: <code>Binomial[7, 5]</code> = 21</p> <p>Do:</p> <pre><code>i = 1 n = Length[s] u = Binomial[n, k] While[Binomial[i, k] &lt; u - m, i++]; x1 = n - (i - 1); </code></pre> <p>Then return:</p> <pre><code>Prepend[ UnrankKSubset[m - u + Binomial[i, k], k - 1, Drop[s, x1]], s[[x1]] ] </code></pre> <p>That is, "prepend" the <code>x1</code> element of list <code>s</code> (remember Mathematica indexes from one, so I believe this would be the <code>x1 - 1</code> index in C++) to the list returned by the recursively called UnrankKSubset function with the arguments:</p> <ul> <li><code>m - u + Binomial[i, k]</code></li> <li><code>k - 1</code></li> <li><code>Drop[s, x1]</code></li> </ul> <p><code>Drop[s, x1]</code> is the rest of list <code>s</code> with the first <code>x1</code> elements removed.</p> <hr> <p>If anything above is not understandable, or if what you wanted was an explanation of the algorithm, rather than an explanation of the code, please leave a comment and I will try again.</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. This table or related slice is empty.
    1. 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