Appendix Permutations and Combinations PC 1 Permutations n
Appendix Permutations and Combinations PC -1
Permutations n n # of m-permutations of n items: 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 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 3, 4 2 1, 4 2 3, 4 3 1, 4 3 2. PC -2
Generating permutations lexicographically n m-permutations of {1, 2, . . . , n}: first permutation: 1 2. . . m last permutation: n n-1. . . n-m+1 n n All permutations except the last permutation are updatable ( can be used to generate the next permutation). The updatable condition for a permutation (P 1 P 2. . . Pm): j, Pi < j n and j is not used at the left of Pi. PC -3
The updating algorithm n 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 n 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 n n n Let N={1, 2, 3, …, n} The rank sequence {r 1, r 2, . . . , rm} of permutation (P 1 P 2. . . Pm): ri is rank of Pi in N-{P 1 , P 2 , . . . , Pi-1}, where the smallest rank is 0. r 1 r 2. . . 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 r 2 n-2 0 r 1 n-1 n rankp(P 1 P 2. . . Pm) = (n-1 digits) PC -6
Example of ranking n 3 -permutations of {1, 2, 3, 4} P 1 P 2 P 3 ----123 124 132 134 142 143 214 231 234 241 243 : r 1 r 2 r 3 ------000 001 010 011 020 021 100 101 110 111 120 121 100 rankp … ------------149 0 1 150 2 151 3 … 4 199 5 200 6 201 7 8 9 456=4*102+5*10+6 10 11=1× 3× 2+2× 2+1 PC -7
Unranking permutations n Given d = rankp( P 1 P 2. . . Pm ), how to obtain ( P 1 P 2. . . Pm) from d? for i =1, 2, . . . , m n d=11 in 3 -permutations of {1, 2, 3, 4} PC -8
Unranking permutations n n Pi = ri + i - di where di is the smallest nonnegative integer such that d=11 P 1= r 1 + 1 - d 1 = 1+1 -0 =2 P 2= r 2 + 2 - d 2 = 2+2 -d 2 = 4 P 3= r 3 + 3 - d 3 = 1+3 -1=3 PC -9
Combinations n n # of m-combinations of n items: 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 n m-combinations of {1, 2, . . . , n}: n first combination: 12. . . m n last combination: (n-m+1)(n-m+2). . . n n The updatable condition for a combination (c 1 c 2. . . cm): j, 1 j m, cj < n - m + j PC -11
The updating algorithm n n step 1: Find the largest j satisfying the updatable condition. step 2: cj + 1 step 3: cj+1 cj + 1, cj+2 cj+1 + 1, . . . , cm cm-1 + 1 Example: 3 -combinations of {1, 2, 3, 4, 5, 6} 13 13 13 14 4 5 6 5 PC -12
Ranking combinations n 3 -combinations of {1, 2, 3, 4, 5, 6} c 1 c 2 c 3 -------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 c 1 c 2 c 3 -------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 n n 3 -combinations of {1, 2, 3, 4, 5, 6} # of combinations greater than (2 3 4): n n 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 n n Given g = rankc( c 1, c 2, . . . , cm ), how to obtain ( c 1 c 2. . . cm) from g? g=10 in 3 -combinations of {1, 2, 3, 4, 5, 6} n n PC -15
- Slides: 15