Convert integer to permutation number - MathOverflow most recent 30 from http://mathoverflow.net2013-05-25T13:52:00Zhttp://mathoverflow.net/feeds/question/42344http://www.creativecommons.org/licenses/by-nc/2.5/rdfhttp://mathoverflow.net/questions/42344/convert-integer-to-permutation-numberConvert integer to permutation numberTom2010-10-15T23:40:01Z2010-10-16T09:50:13Z
<p>I have no idea how to achieve this, any help would be greatly appreciated and very useful to me.</p>
<p>I have a loop in some computer code, that loops through every single combination of 7 on bits in a 64 bit integer.</p>
<p>For example,</p>
<pre><code>Permutation 1: 00...001111111
Permutation 2: 00...010111111
Permutation 3: 00...011011111
</code></pre>
<p>etc. </p>
<p>These evaluate to the decimal numbers:</p>
<pre><code>Permutation 1: 00...001111111 = 127
Permutation 2: 00...010111111 = 191
Permutation 3: 00...011011111 = 223
</code></pre>
<p>In total there are:</p>
<pre><code>621,216,192
</code></pre>
<p>Combinations (64 choose 7).</p>
<p>Given any decimal/binary number (it doesn't matter which type), that is guaranteed to be a valid permutation value (we don't need to worry about 128 being passed in for example), how can I calculate which permutation number this is?</p>
<p>IE:</p>
<pre><code>whatPermutation(127) = 1;
whatPermutation(191) = 2;
whatPermutation(223) = 3;
</code></pre>
<p>etc.</p>
<p>Any help would be brilliant, again, I have no idea where to start.</p>
http://mathoverflow.net/questions/42344/convert-integer-to-permutation-number/42345#42345Answer by Ross Millikan for Convert integer to permutation numberRoss Millikan2010-10-15T23:49:20Z2010-10-15T23:49:20Z<p>There are 63 choose 7 combinations that start with a 0 and 63 choose 6 that start with a 1. So if you have a 1 in the first bit you are at least 63 choose 7 into the list. Go through the bits in order adding up how many combinations are earlier in the list. So a 1 in the first position adds 63 choose 7. If the first bit is in the second position count 62 choose 7. If the first two bits are 1's count 63 choose 7 + 62 choose 6. You'll need to add 1 for your indexing. Hope this helps.</p>
http://mathoverflow.net/questions/42344/convert-integer-to-permutation-number/42346#42346Answer by Michael Lugo for Convert integer to permutation numberMichael Lugo2010-10-15T23:55:34Z2010-10-15T23:55:34Z<p>The magic words here are that you want to "rank" the k-subsets of $[n]$. See Herb Wilf's lecture notes <a href="http://www.math.upenn.edu/~wilf/lecnotes.html" rel="nofollow">"East Side, West Side"</a>, pp. 18-19. I think this is also in Volume 4, Fascicle 3 of Knuth, <i>The art of computer programming</i> -- at least that's what the titles <a href="http://www-cs-faculty.stanford.edu/~uno/taocp.html" rel="nofollow">at Knuth's web page</a> lead me to believe.</p>
http://mathoverflow.net/questions/42344/convert-integer-to-permutation-number/42347#42347Answer by Richard Stanley for Convert integer to permutation numberRichard Stanley2010-10-16T00:12:52Z2010-10-16T00:12:52Z<p>Suppose that your 64 bit number is $n=a_{63} a_{62} ... a_0$. Let $i_7>i_6>\cdots>i_1$ be those indices $j$ for which $a_j=1$. Then
$$ whatPermutation(n)=1+{i_7\choose 7} +{i_6\choose 6}+\cdots+{i_1\choose 1}. $$
For instance, if $n= 00\cdots 110001011011$, then
$$ whatPermutation(n)= 1+{11\choose 7}+{10\choose 6}+{6\choose 5}+{4\choose 4}+{3\choose 3}+ {1\choose 2}+{0\choose 1} $$ $$ = 549. $$</p>
http://mathoverflow.net/questions/42344/convert-integer-to-permutation-number/42378#42378Answer by cxseven for Convert integer to permutation numbercxseven2010-10-16T09:50:13Z2010-10-16T09:50:13Z<p>This correspondence is known as "combinadics", for which there is a nice <a href="http://en.wikipedia.org/wiki/Combinatorial_number_system" rel="nofollow">Wikipedia article</a>. <a href="http://en.wikipedia.org/wiki/Factorial_number_system" rel="nofollow">Factoradics</a> is a similar correspondence for permutations.</p>
<p>I'd used similar self-generated methods a few times to compress data structures (thinking of the factoradic method as a "variable radix"), before recently discovering their names. They should probably be better publicized!</p>