10.4 KB · commit a9602da
In many applications, we need to generate all permutations or combinations of a set rather than just count them. For instance, in computer simulations, to find a tournament schedule with optimal properties, we may need to generate all possible matchings between players. In planning a work schedule, we may need to generate all possible ways to assign workers to shifts to evaluate which schedule best meets business criteria. To find the optimal route for a delivery truck, we may want to generate all possible routes through n locations to evaluate which route has the shortest total distance. These examples show that it is often necessary to generate permutations and combinations to solve problems.
Any set with n elements can be placed in one-to-one correspondence with the set {1,2,3,…,n}. We can list the permutations of any set of n elements by generating the permutations of the n smallest positive integers and then replacing these integers with the corresponding elements.
Many different algorithms have been developed to generate the n! permutations of this set. We will describe one of these that is based on the lexicographic (or dictionary) ordering of the set of permutations of {1,2,3,…,n}. In this ordering, the permutation a1a2⋯an precedes the permutation b1b2⋯bn, if for some k, with 1≤k≤n, a1=b1,a2=b2,…,ak−1=bk−1, and ak<bk. In other words, a permutation of the set of the n smallest positive integers precedes (in lexicographic order) a second permutation if the number in this permutation in the first position where the two permutations disagree is smaller than the number in that position in the second permutation.
The permutation 23415 of the set {1,2,3,4,5} precedes the permutation 23514, because these permutations agree in the first two positions, but the number in the third position in the first permutation, 4, is smaller than the number in the third position in the second permutation, 5. Similarly, the permutation 41532 precedes 52143. ◀
An algorithm for generating the permutations of {1,2,…,n} can be based on a procedure that constructs the next permutation in lexicographic order following a given permutation a1a2⋯an. We will show how this can be done. First, suppose that an−1<an. Interchange an−1 and an to obtain a larger permutation. No other permutation is both larger than the original permutation and smaller than the permutation obtained by interchanging an−1 and an. For instance, the next larger permutation after 234156 is 234165. On the other hand, if an−1>an, then a larger permutation cannot be obtained by interchanging these last two terms in the permutation. Look at the last three integers in the permutation. If an−2<an−1, then the last three integers in the permutation can be rearranged to obtain the next largest permutation. Put the smaller of the two integers an−1 and an that is greater than an−2 in position n−2. Then, place the remaining integer and an−2 into the last two positions in increasing order. For instance, the next larger permutation after 234165 is 234516.
On the other hand, if an−2>an−1 (and an−1>an), then a larger permutation cannot be obtained by permuting the last three terms in the permutation. Based on these observations, a general method can be described for producing the next larger permutation in increasing order following a given permutation a1a2⋯an. First, find the integers aj and aj+1 with aj<aj+1 and aj+1>aj+2>⋯>an, that is, the last pair of adjacent integers in the permutation where the first integer in the pair is smaller than the second. Then, the next larger permutation in lexicographic order is obtained by putting in the jth position the least integer among aj+1,aj+2,…, and an that is greater than aj and listing in increasing order the rest of the integers aj,aj+1,…,an in positions j+1 to n. It is easy to see that there is no other permutation larger than the permutation a1a2⋯an but smaller than the new permutation produced.
What is the next permutation in lexicographic order after 362541?
Solution: The last pair of integers aj and aj+1 where aj<aj+1 is a3=2 and a4=5. The least integer to the right of 2 that is greater than 2 in the permutation is a5=4. Hence, 4 is placed in the third position. Then the integers 2, 5, and 1 are placed in order in the last three positions, giving 125 as the last three positions of the permutation. Hence, the next permutation is 364125. ◀
To produce the n! permutations of the integers 1,2,3,…,n, begin with the smallest permutation in lexicographic order, namely, 123⋯n, and successively apply the procedure described for producing the next larger permutation n!−1 times. This yields all the permutations of the n smallest integers in lexicographic order.
Generate the permutations of the integers 1, 2, 3 in lexicographic order.
Solution: Begin with 123. The next permutation is obtained by interchanging 3 and 2 to obtain 132. Next, because 3>2 and 1<3, permute the three integers in 132. Put the smaller of 3 and 2 in the first position, and then put 1 and 3 in increasing order in positions 2 and 3 to obtain 213. This is followed by 231, obtained by interchanging 1 and 3, because 1<3. The next larger permutation has 3 in the first position, followed by 1 and 2 in increasing order, namely, 312. Finally, interchange 1 and 2 to obtain the last permutation, 321. We have generated the permutations of 1, 2, 3 in lexicographic order. They are 123, 132, 213, 231, 312, and 321. ◀
Algorithm 1 displays the procedure for finding the next permutation in lexicographic order after a permutation that is not n(n−1)(n−2)…21, which is the largest permutation.
ALGORITHM 1 Generating the Next Permutation in Lexicographic Order.
procedure next permutation(a1a2...an: permutation of
{1, 2, ..., n} not equal to n (n - 1)...2 1)
j := n - 1
while aj > aj+1
j := j - 1
{j is the largest subscript with aj < aj+1}
k := n
while aj > ak
k := k - 1
{ak is the smallest integer greater than aj to the right of aj}
interchange aj and ak
r := n
s := j + 1
while r > s
interchange ar and as
r := r - 1
s := s + 1
{this puts the tail end of the permutation after the jth position in increasing order}
{a1a2...an is now the next permutation}
How can we generate all the combinations of the elements of a finite set? Because a combination is just a subset, we can use the correspondence between subsets of {a1,a2,…,an} and bit strings of length n.
Recall that the bit string corresponding to a subset has a 1 in position k if ak is in the subset, and has a 0 in this position if ak is not in the subset. If all the bit strings of length n can be listed, then by the correspondence between subsets and bit strings, a list of all the subsets is obtained.
Recall that a bit string of length n is also the binary expansion of an integer between 0 and 2n−1. The 2n bit strings can be listed in order of their increasing size as integers in their binary expansions. To produce all binary expansions of length n, start with the bit string 000…00, with n zeros. Then, successively find the next expansion until the bit string 111…11 is obtained. At each stage the next binary expansion is found by locating the first position from the right that is not a 1, then changing all the 1s to the right of this position to 0s and making this first 0 (from the right) a 1.
Find the next bit string after 10 0010 0111.
Solution: The first bit from the right that is not a 1 is the fourth bit from the right. Change this bit to a 1 and change all the following bits to 0s. This produces the next larger bit string, 10 0010 1000. ◀
The procedure for producing the next larger bit string after bn−1bn−2…b1b0 is given as Algorithm 2.
ALGORITHM 2 Generating the Next Larger Bit String.
procedure next bit string(bn-1 bn-2...b1b0: bit string not equal to 11...11)
i := 0
while bi = 1
bi := 0
i := i + 1
bi := 1
{bn-1 bn-2...b1b0 is now the next bit string}
Next, an algorithm for generating the r-combinations of the set {1,2,3,…,n} will be given. An r-combination can be represented by a sequence containing the elements in the subset in increasing order. The r-combinations can be listed using lexicographic order on these sequences. In this lexicographic ordering, the first r-combination is {1,2,…,r−1,r} and the last r-combination is {n−r+1,n−r+2,…,n−1,n}. The next r-combination after a1a2⋯ar can be obtained in the following way: First, locate the last element ai in the sequence such that ai=n−r+i. Then, replace ai with ai+1 and aj with ai+j−i, for j=i+1,i+2,…,r. It is left for the reader to show that this produces the next larger r-combination in lexicographic order. This procedure is illustrated with Example 5.
Find the next larger 4-combination of the set {1,2,3,4,5,6} after {1,2,5,6}.
Solution: The last term among the terms ai with a1=1,a2=2,a3=5, and a4=6 such that ai=6−4+i is a2=2. To obtain the next larger 4-combination, increment a2 by 1 to obtain a2=3. Then set a3=3+1=4 and a4=3+2=5. Hence the next larger 4-combination is {1,3,4,5}. ◀
Algorithm 3 displays pseudocode for this procedure.
ALGORITHM 3 Generating the Next r-Combination in Lexicographic Order.
procedure next r-combination({a1, a2, ..., ar}: proper subset of
{1, 2, ..., n} not equal to {n - r + 1, ..., n} with
a1 < a2 < ... < ar)
i := r
while ai = n - r + i
i := i - 1
ai := ai + 1
for j := i + 1 to r
aj := ai + j - i
{{a1, a2, ..., ar} is now the next combination}