Generating Permutations and Combinations March 0, 005 Generating Permutations We have learned that there are n! permutations of {,,, n} It is important in many instances to generate a list of such permutations For example, for the permutation of {,,, }, we may insert 5 in to generate five permutations of {,,,, 5} as follows: 5, 5, 5, 5, 5 If we have a complete list of permutations for {,,, n }, then we can obtain a complete list of permutations for {,,, n} by inserting n in n ways to each permutation of the list for {,,, n } For n =, the list is just For n =, the list is For n =, the list is = = To generate a complete list of permutations for the set {,,, n}, we assign a direction to each integer k {,,, n} by writing an arrow above it pointing to the left or to the right: k or k We consider permutations of {,,, n} in which each integer is given a direction; such permutations are called directed permutations An integer k in a directed permutation is called mobile of its arrow points to a smaller integer adjacent to it For example, for 5 6, the integers, 5, and 6 are mobile It follows that can never be mobile since there is no integer in {,,, n} smaller than The integer n is mobile, except two cases: (i n is the first integer and its arrow points to the left, ie, n ; (ii n is the last integer and its arrow points to the right, ie, n
For n =, we have the list = Algorithm Algorithm for Generating Permutations of {,,, n}: Step 0 Begin with n Step Find the largest mobile integer m Step Switch m and the adjacent integer its arrow points to Step Switch the directions for all integers p > m Step Write down the resulting permutation with directions and return to Step For example, for n =, we have and For n =, we have,,,,, For n =, the algorithm produces the list Proof Observe that when n is not the largest mobile the direction of n must be either like n or n
in the permutation When the largest mobile m (with m < n is switched with its target integer to produce a new permutation, the direction of n will be changed simultaneously, and the permutation with direction becomes n or n Now n is the largest mobile Switching n with its target integer for n times to produce n more permutations, we obtain exactly n new permutations (including the one before switching n The algorithm stops at the permutation n Inversions of Permutations Let u u u n be a permutation of S = {,,, n} We can view u u u n as as a bijection π : S S defined by π( = u, π( = u,, π(n = u n If u i > u j for some i < j, then (u i, u j is called an inversion of π The number of inversions of π is denoted by inv(π For example, the permutation 765 of {,,, 7} has the inversions: (,, (,, (,, (,, (6, 5, (7, 5, (7, 6 For k {,,, n} and u j = k, let a k be the number of integers that precede k in the permutation u u u n but greater than k, ie, a k = #{u i : u i > u j = k, i < j} = #{π(i : π(i > π(j = k, i < j} It measures how much k is out of order by counting the numbers of integers larger than k but located before k The tuple (a, a,, a n is called the inversion sequence (or inversion table of the permutation π = u u u n The sum a + a + + a n measures the total disorder of a permutation Example The inversion sequence of the permutation 765 of {,,, 7} is (,, 0, 0,,, 0 It is clear that for any permutation π of {,,, n}, the inversion sequence (a, a,, a n of π satisfies 0 a n, 0 a n,, 0 a n, a n = 0 ( It is easy to see that the number of sequences (a, a,, a n satisfying ( equals n (n = n! This suggests that the inversion sequences may be characterized by ( Theorem Let (a, a,, a n be an integer sequence satisfying 0 a n, 0 a n,, 0 a n, a n = 0 Then there is a unique permutation π of {,,, n} whose inversion sequence is (a, a,, a n Proof We give two algorithms to uniquely construct the permutation whose inversion sequence is (a, a,, a n Algorithm I Construction of a Permutation from Its Inversion Sequence: Step 0 Write down n Step If a n = 0, place n before n; if a n =, place n after n Step If a n = 0, place n before the two members n and n ; if a n =, place n between n and n ; if a n =, place n after both n and n Step k If a n k = 0, place n k to the left of the first position; if a n k =, place n k to the right of the st existing number; if a n k =, place n k to the right of the nd existing number; ; if a n k = k, place n k to the right of the last existing number In general, insert n k to the right of the a n k th existing number
Step n If a = 0, place before all existing numbers; otherwise, place to the right of the a th existing number For example, for the inversion sequence (a, a,, a 8 = (, 6,, 0,,,, 0, its permutation can be constructed by Algorithm I as follows: 8 Write down 8 87 Since a 7 =, insert 7 to the right of the first number 8 867 Since a 6 =, insert 6 to the right of the first number 8 8675 Since a 5 =, insert 5 to the right of the third number 7 8675 Since a = 0, insert to the left of the first number 8 8675 Since a =, insert to the right of the first number 8675 Since a = 6, insert to the right of the sixth number 5 8675 Since a =, insert to the right of the fifth number 6 Algorithm II Construction of a Permutation from Its Inversion Sequence: Step 0 Mark down n empty spaces Step Put into the (a + th empty space from left Step Put into the (a + th empty space from left Step k Put k into the (a k + th empty space from left Step n Put n into the (a n + th empty space (the last empty box from left For example, the permutation for the inversion sequence (a, a,, a 8 = (, 6,, 0,,,, 0 can be constructed by Algorithm II as follows: Mark down 8 empty spaces Since a =, put into the 5th empty space Since a = 6, put into the 7th empty space Since a =, put into the nd empty space Since a = 0, put into the st empty space 5 Since a 5 =, put 5 into the th empty space 6 5 Since a 6 =, put 6 into the nd empty space 675 Since a 7 =, put 7 into the nd empty space 8675 Since a 8 = 0, put 8 into the st empty space Generating Combinations Let S be an n-set For convenience of generating combinations of S, we take S to be the set S = {x n, x n,, x, x, x 0 } Each subset A of S can be identified as a function χ A : S {0, }, called the characteristic function of A, defined by { if x A χ A (x = 0 if x A In practice, χ A is represented by a 0- sequence or a base numeral For example, for S = {x 7, x 6, x 5, x, x, x, x, x 0 }, 00000000 {x 7, x 5, x, x } 0000 {x 6, x 5, x, x, x 0 } 000 {x 7, x 6, x 5, x, x, x, x, x 0 }
Algorithm The algorithm for Generating Combinations of {x n, x n,, x, x, x 0 }: Step 0 Begin with a n a a 0 = 0 00 Step If a n a a 0 =, stop Step If a n a a 0, find the smallest integer j such that a j = 0 Step Change a j, a j,, a 0 (from 0 to or from to 0, write down a n a a 0, and return to Sept For n =, the algorithm produces the list 0000 000 000 00 000 00 00 0 000 00 00 0 00 0 0 The unit n-cube Q n is a graph whose vertex set is the set of all 0- sequences of length n, and two sequences are adjacent if they differ in only one place A Gray code of order n is a path of Q n that visits every vertex of Q n exactly once, ie, a Hamilton path of Q n For example, 000 00 0 00 0 00 0 is a Gray code of order It is obvious that this Gray code can not be a part of any Hamilton cycle since 000 and are not adjacent A cyclic Gray code of order n is a Hamilton cycle of Q n For example, the closed path 000 00 0 00 0 0 00 000 is a cyclic Gray code of order For n =, we have the Gray code 0 For n =, we use 0 to produce the path 00 0 by adding a 0 in the front, and use 0 to produce 0 by adding a in the front, then combine the two paths to produce the Gray code 00 0 0 For n =, we use the Gray code (of order 00 0 0 to produce the path 000 00 0 00 by adding 0 in the front, and use the Gray code 0 0 00 (the reverse of 00 0 0 to produce the path 0 0 00 Combine the two paths to produce the Gray code of order 000 00 0 00 0 0 00 The Gray codes obtained in this way are called reflected Gray codes Algorithm Algorithm for Generating reflected Gray codes of order n: Step 0 Begin with a n a n a 0 = 00 0 Step If a n a n a 0 = 0 0, stop Step If a n + a n + + a 0 = even, then change a 0 (from 0 to or to 0 Step If a n + a n + a 0 = odd, find the smallest j such that a j = and change a j+ (from 0 to or to 0 Step Write down a n a n a 0 and return to Step We note that if a n a n a 0 0 0 and a n + a n + + a 0 = odd, then j n so that j + n and a j+ is defined We also note that the smallest number j in Step may be 0, ie, a 0 = ; if so there is no i < j such that a i = 0 and we change a j+ = a as instructed in Step Proof We proceed by induction on n For n =, it is obviously true For n =, we have 00 0 0 Let n and assume that it is true for,,, n ( When the algorithm is applied, by the induction hypothesis the first resulted n words form the reflected Gray code of order n with a 0 attached to each word; the n th word is 00 0 ( Continuing the algorithm, we have 00 0 0 0 5
Now for each word of the form b n b 0, the parity of b n b 0 is the same as the parity of b n b 0 Continuing the algorithm, the next n words (including 0 0 form a reflected Gray code (of order n with a attached at the beginning; the last word is 0 0 ( Continuing the algorithm, we have 0 0 00 0 The next n words (including 00 0 form a reflected Gray code (of order n with a 0 attached at the beginning; the last word is 00 0 ( 00 0 000 0 ; there are n words with 00 attached at the beginning (n Continuing the algorithm, we have 0 000 0 0000 The next words (including 0 000 form a reflected Gray code (of order with 0 0 attached at the beginning; the last word is 0 00 (n 0 00 0 000 0 00; there are words 0 000 and 0 00 (of order with 0 00 attached at the beginning (n 0 00 0 000 The algorithm produces only word 0 000 (n + Finally, the algorithm ends at 0 000 0 0000 Note that all words produced in Steps (k (n + are distinct from the words produced in Step (k, where k n + Thus the words produced by the algorithm are distinct and the total number of words is + + + + + n = n This implies that the sequence of the words produced forms the reflected Gray code of order n Generating r-combinations Let S = {,,, n} When an r-combination or r-subset A = {a, a,, a r } of S is given, we always assume that a < a < < a r For two r-combinations A = {a, a,, a r } and B = {b, b,, b r } of S, if there is an integer k ( k r such that a = b, a = b,, a k = b k, a k < b k, we say that A precedes B in the lexicographic order, written A < B Then the set P r (S of all r-subsets of S is linearly ordered by the lexicographic order For simplicity, we write an r-combination {a, a,, a r } as an r-permutation a a a r with a < a < < a r Theorem Let a a a r be an r-combination of {,,, n} The first r-combination in lexicographic order is r, and the last r-combination in lexicographic order is (n r + (n n If a a a k a r (n r + (n n and k is the largest integer such that a k n r + k, then the successor of a a a r is a a a k (a k + (a k + (a k + r k + Proof Since a i (n r + i for all i r, then a k n r + k implies a k < n r + k Algorithm Algorithm for Generating r-combinations of {,,, n} in Lexicographic Order: Step 0 Begin with the r-combination a a a r = r Step If a a a r = (n r + (n n, stop Step If a a a r (n r + (n n, find the largest k such that a k < n r + k Step Change a a a r to a a k (a k + (a k + (a k + r k +, write down a a a r, and return back to Step 6
Example The collection of all -combinations of {,,,, 5, 6} are listed by the algorithm: 5 5 56 56 5 6 6 5 56 6 56 56 6 56 Theorem Let a a a r be an r-combination of {,,, n} Then the number of r-combinations up to the place a a a r in lexicographic order equals ( ( ( ( ( n n a n a n ar n ar r r r Proof The r-combinations b b b r after a a a r can be classified into r kinds: ( b > a ; there are ( n a r such ( r-combinations ( b = a, b > a ; there are n a r such r-combinations ( b = a, b = a, b > a ; there are such r-combinations ( n a r (r b = a,, b r = a r, b r > a r ; there are ( n a r such r-combinations (r b = a,, b r = a r, b r > a r ; there are ( n a r such r-combinations Since the number of r-combinations of {,,, n} is ( n r, the conclusion follows immediately Example The -combinations of {,,,, 5} are as follows:,, 5,, 5, 5,, 5, 5, 5 The -permutations of {,,,, 5} can be obtained by making! permutations for each -combination: 5 Partially Ordered Sets 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 Definition 5 A relation on a set X is a subset R of the product set X X A relation R on X is called reflexive if xrx for all x X; irreflexive if x Rx for all x X; symmetric provided that if xry for some x, y X then yrx; antisymmetric provided that if xry and yrx for some x, y X then x = y; 5 transitive provided that if xry and yrz for some x, y, z X then xrz Example 5 ( The relation of subset,, is a reflexive and transitive relation on the power set P (X ( The relation of divisibility,, is a reflexive and transitive relation on the set of positive integers A partial order on a set X is a reflexive, antisymmetric, and transitive relation A strict partial order on a set X is an irreflexive, antisymmetric, and transitive relation If a relation R is a partial order, we usually denote R by ; the relation < is defined by a < b if and only if a b but a b Conversely, for a strict partial order < on a set X, the relation defined by a b if and only if a < b or a = b is a partial order A set X with a partial order is called a partially ordered set (or poset for short and is denoted by (X, A linear order or total order on a set X is a strict order < such that for any two distinct elements a and b, either a < b or b < a 7
Let and be two partial orders on a set X The poset (X, is called an extension of the poset (X, if, whenever a b, then a b In particular, an extension of a partial order has more compatible pairs We show that every finite poset has a linear extension, that is, an extension which is a linearly ordered set Theorem 5 Let (X, be a finite partially ordered set Then there is a linear order such that (X, is an extension of (X, Proof We need to show that the elements of X can be listed in some order x, x,, x n in such a way that if x i x j then x i comes before x j in this list, ie, i j The following algorithm does the job Algorithm 5 Algorithm for a Linear Extension of an n-poset: Step Choose a minimal element x from X (with respect to the ordering Step Delete x from X; choose a minimal element x from X {x } Step Delete x from X {x }; choose a minimal element x from X {x, x } Step n Delete x n from X {x,, x n } and choose the only element x n in X {x,, x n } A relation R on X is called an equivalence relation if R is reflexive, symmetric, and transitive For an equivalence relation R on a set X and an element x X, we call the set [x] = {y X : xry} an equivalence class of R and x a representative of the equivalence class [x] Theorem 5 Let R be an equivalence relation on a set X Then for any x, y X, the following are logically equivalent: (i [x] [y] ; (ii [x] = [y]; and (iii xry A collection P = {A, A,, A k } of nonempty subsets of a set X is called a partition of X if A i A j = for i j and X = k i= A i We will show below that if R is an equivalence relation on a set X, then the collection P R = {[x] : x X} is a partition of X Conversely, if P = {A, A,, A k } is a partition of X, then the relation is an equivalence relation on X R P = k A i A i i= Theorem 55 Let R be an equivalence relation on a set X, and let P = {A, A,, A k } be a partition of X Then ( P R is a partition of X; ( R P is an equivalence relation on X; ( R PR = R, P RP = P 8