Appendix Permutations and Combinations PC -1 Permutations # of m-permutations of n items: n! n Pm n m ! 3-permutations of {1,2,3,4} in lexicographic order: 1 2 3, 1 2 4, 1 3 2, 1 3 4, 1 4 2, 1 4 3, 2 1 3, 2 1 4, 2 3 1, 2 3 4, 2 4 1, 2 4 3, 3 1 2, 3 1 4, 3 2 1, 3 2 4, 3 4 1, 3 4 2, 4 1 2, 4 1 3,4 2 1, 4 2 3, 4 3 1, 4 3 2. PC -2 Generating permutations lexicographically m-permutations of {1,2,...,n}: first permutation: 1 2 ... m last permutation: n n-1 ... n-m+1 All permutations except the last permutation are updatable ( can be used to generate the next permutation). The updatable condition for a permutation (P1P2 ... Pm): j, Pi < j n and j is not used at the left of Pi. PC -3 The updating algorithm Let Pi be the rightmost element and j be the smallest index satisfying the updatable condition. Pi j Pi+1 the first position which is not used. Pi+k the kth position which is not used. Pm the (m-i)th position which is not used. PC -4 Example of updating 5-permutations of {1,2,3,4,5,6,7,8,9}: 81362 81364 81365 81367 81369 81372 81379 81392 81397 81423 Pi=2, Pi=4, Pi=5, Pi=7, Pi=6, j=4 j=5 j=7 j=9 j=7 Pi=7, j=9 Pi=3, j=4 PC -5 Ranking permutations Let N={1,2,3,…,n} The rank sequence {r1, r2, ..., rm} of permutation (P1P2...Pm): ri is rank of Pi in N-{P1 , P2 ,... , Pi-1}, where the smallest rank is 0. r1r2...rm can be seen as a mixed radix integer: 0 rm n-m 0 rm-1 n-m+1 (n-m+1 digits) (n-m+2 digits) : 0 r2 n-2 0 r1 n-1 rankp(P1,P2,...,Pm) = [ (n-1 digits) m1 mi 1 r n i j ] r i 1 i j 0 m PC -6 Example of ranking 3-permutations of {1,2,3,4} P1P2P3 --------123 124 132 134 142 143 213 214 231 234 241 243 : r1 r 2 r3 ------000 001 010 011 020 021 100 101 110 111 120 121 rankp -----------------------0 1 2 3 4 5 6 7 8 9 10 11=1×3×2+2×2+1 PC -7 Unranking permutations Given d = rankp( P1, P2, ..., Pm ), how to obtain ( P1 P2 ... Pm) from d? m j 1 m j 1 i 1 ri (d rj n j k ) / n j k j 1 k 0 k 0 for i =1, 2, ..., m d=11 in 3-permutations of {1,2,3,4} 2 11 3 5 1 1 r3 2 r2 r1 PC -8 Unranking permutations Pi = ri + i - di where di is the smallest nonnegative integer such that i 1 di ri i di Pj , Pi Pj , j i j 1 d=11 P1= r1 + 1 - d1 = 1+1-0 =2 P2= r2 + 2 - d2 = 2+2-d2 = 4 P3= r3 + 3 - d3 = 1+3-d3 = 1+3-1=3 PC -9 Combinations # of m-combinations of n items: n n! Cm n m ! m! 3-combinations of {1,2,3,4} in lexicographic order: 1 2 3, 1 2 4, 1 3 4, 2 3 4 PC -10 Generating combinations lexicographically m-combinations of {1, 2, ..., n}: first combination:12...m last combination:(n-m+1)(n-m+2)...n The updatable condition for a combination (c1c2...cm): j, 1 j m, cj < n - m + j PC -11 The updating algorithm step1: Find the largest j satisfying the updatable condition. step2: cj cj + 1 step3: cj+1 cj + 1, cj+2 cj+1 + 1, ..., cm cm-1 + 1 Example: 3-combinations of {1, 2, 3, 4, 5, 6} 1 1 1 1 3 3 3 4 4 5 6 5 PC -12 Ranking combinations 3-combinations of {1, 2, 3, 4, 5, 6} c1 c2 c3 -------1 2 3 1 2 4 1 2 5 1 2 6 1 3 4 1 3 5 1 3 6 1 4 5 1 4 6 1 5 6 rankc --------0 1 2 3 4 5 6 7 8 9 c1 c2 c3 -------2 3 4 2 3 5 2 3 6 2 4 5 2 4 6 2 5 6 3 4 5 3 4 6 3 5 6 4 5 6 rankc --------10 11 12 13 14 15 16 17 18 19 PC -13 Ranking rules 3-combinations of {1, 2, 3, 4, 5, 6} # of combinations greater than (2 3 4): Fix nothing, 3-combinations of {3,4,5,6}=4 Fix (2), 2-combinations of {4,5,6}=3 Fix (2 3), 1-combinations of {5,6}=2 Rankc(2 3 4)=19-(4+3+2)=10 PC -14 Unranking combinations Given g = rankc( c1, c2, ..., cm ), how to obtain ( c1 c2 ... cm) from g? g=10 in 3-combinations of {1, 2, 3, 4, 5, 6} (C36 1) 10 19 10 9 C35 10 9 C34 4 - - - - - (2) 945 5 C23 3 - - - -- (2 3) 53 2 C12 2 - - - -- (2 3 4) PC -15