Parallel Generation of Permutations by Inversion Vectors Hoang Chi Thanh Department of Computer Science VNU University of Science 334 - Nguyen Trai Rd., Thanh Xuan, Hanoi, Vietnam Abstract: Permutations of a set are used in many practical problems There are some algorithms for generating permutations of a set, e.g a reverse alphabetical order algorithm, an algorithm based on adjacent transpositions, an algorithm determining permutations from their reductions… But these algorithms are rather long and difficult to parallelize In this paper we point out that the set of all permutations of a set is isomorphic to the set of their inversion vectors Thus, we construct a new algorithm determining permutations from their inversion vectors The algorithm is simpler and shorter than existing ones Furthermore, this algorithm is easy to parallelize We parallelize the proposed algorithm by one of two methods The first one is tasks dividing: 1) finding an inversion vector, and 2) determining a permutation from the just found inversion vector Two these tasks are performed in parallel with equal computing amounts The second parallelizing method is based on splitting the sequence of all desirable permutations into subsequences and then concurrently generating the subsequences Keywords: algorithm, inversion, parallel computing, permutation I INTRODUCTION Permutations of a finite set are applied in many areas of sciences and technologies, for example, the scheduling problems, the control problems and the path finding problems So modifying an exciting algorithm or constructing a new algorithm to generate permutations are attracted many researches [1-5,7-9] There are some algorithms to generate permutations of a set, e.g a reverse alphabetical order algorithm, an algorithm based on adjacent transpositions…[2,4,8] Recently, J Ginsburg constructed a method determining a permutation from its set of reductions [1], M Monks reconstructed permutations from cycle minors [4] and T Kuo proposed a new method for generating permutations based on factorial digits [3] But the above algorithms are rather long and difficult to parallelize Based on the notion of inversion of a permutation [5], we propose a notion of inversion vector and show that the inversion vector becomes a good representation of permutations We construct a novel algorithm generating permutations of a finite set by inversion vectors The algorithm is short, simple and highly possible to parallelize We parallelize the proposed algorithm by one of two methods The first one is tasks dividing: 1) finding an inversion vector, and 2) determining a permutation from the just found inversion vector Two these tasks are performed in parallel with equal computing amounts The second parallelizing method is based on splitting the sequence of all desirable permutations into subsequences and then generating the subsequences concurrently as presented in [9-10] The obtained results take part in developing the theory of computational combinatorics and applying in practice The organization of the rest of the paper is as follows: In part we recall the permutation problem, propose a notion of inversion vector and consider the relationship between permutations and inversion vectors A new algorithm generating permutations by inversion vector is constructed in the part The part is devoted two techniques for parallelizing the above presented algorithm Some developing directions are proposed in conclusion II PERMUTATIONS OF A SET Let X be a finite set A permutation of the set X is a checklist of its all elements It is easy to see that each permutation of the set X is a bijection from X to itself 2.1 Permutation problem Given an n-element set X Find all permutations of the set X It is easy to show that the number of solutions is n! The number of elements n is as big as great the time to find all solutions of the problem Identify X ≡ {1, 2, …, n} Let denote Sn the set of all permutations of the nelement set X A permutation f ∈ Sn can be represented by a positive integer sequence of the length n as follows: f = < f(1) f(2) … f(n) >, where f(i) ∈ X and f(i) ≠ f(j), ≤ i ≠ j ≤ n For simplicity, the above sequence can be written as: f = < a1 a2 an >, where = f(i) , i = 1, 2, , n In the sequence there is a pair of integers, the preceding is greater than following one Such a pair is an inversion of the permutation Definition 2.1: A pair (ai, aj), where i < j is called an inversion of the permutation < a1 a2 an > iff > aj The notion of inversion is used to determine the sign of a permutation [5,8] We use it to generate permutations 2.2 Inversion vector of a permutation An inversion vector of a permutation is defined as follows: Definition 2.2: An n-dimension vector (d1, d2, , dn) is called an inversion vector of the permutation < a1 a2 an > iff dj = |{ i | i < j , > aj }| , j = 1, 2, , n By definition, the coordinator dj is indeed the number of components of the permutation, greater than aj and to its left Note that following components not effect the number of inversions of preceding ones Example 2.3: The sequences of permutations and inversion vectors of an 3-element set No Permutations 123 132 231 213 312 321 978-1-4673-0309-5/12/$31.00 ©2012 IEEE Inversion vectors 000 001 002 010 011 012 By Definition 2.2, the first coordinator d1 of an inversion vector of any permutation always equals (one choice), the second coordinator d2 equals or (two choices) H: (2.1) ≤ dj ≤ j - , where j = 1, 2, , n Let denote N the set of all positive integers Set: Vn = { (d1, d2, , dn) | dj ∈ N , ≤ dj ≤ j - , j = 1, 2, , n } This is the set of all n-dimension integer vectors satisfying (2.1) Of course: | Vn | = n! The relationship between the set of permutations Sn and the set of vectors Vn is pointed out by the following theorem Theorem 2.1: The set Sn is isomorphic to the set Vn Proof: Construct a mapping T : Sn → Vn as follows: ∀ f = < a1 a2 an > ∈ Sn , T(f) = (d1, d2, , dn), where (d1, d2, , dn) is the inversion vector of the permutation We show that the mapping T is a bijection Because two sets Sn and Vn are finite and have the same cardinality, so we show only that the mapping T is injective Assume that two following permutations have the same inversion vector: ∃ < a1 a2 an > , < b1 b2 bn > ∈ Sn : (d1a, d2a, , dna) = (d1b, d2b, , dnb) a b Due to dn = dn so both an and bn are less than dna elements in X Thus, an = bn (= n - dna) Remove an and bn in the above permutations, we get two permutations < a1 a2 an-1 > , < b1 b2 bn-1 > of the n-1 element set X \ {an} that have the same inversion vector (d1a, d2a, , dn-1a) Repeat the above reasoning we have: an-1 = bn-1 Analogously, we show that: < a1 a2 an > = < b1 b2 bn > The two permutations are identical So the mapping T is an injection Thus, the number of inversion vectors of permutations of a set is equal to the number of the permutations In other words, each vector belonging to Vn is indeed an inversion vector of some permutation in Sn One used to represent a permutation of an nelement set by a matrix of rows and n columns or an integer sequence with the length of n or a directed graph of n nodes Theorem 2.1 points out that inversion vector is a good representation of permutations III PERMUTATIONS GENERATION BY INVERSION VECTORS Based on Theorem 2.1 we are able to construct a new algorithm consisting of two steps, to generate all permutations of an n-element set as follows: 1) Consequently generating inversion vectors in the set Vn 2) Determining a permutation corresponding to the just found inversion vector 3.1 Generating inversion vectors To perform the step we consider each inversion vector (d1, d2, , dn) in the set Vn as a word d1 d2 dn on the alphabet N We sort these words in ascending by the alphabetical order (see Example 2.3) So, - The first inversion vector (the least) is 0 0, corresponding to the identical permutation < n-1 n > - The last inversion vector (the most) is n-2 n-1, corresponding to the permutation < n n-1 n-2 >, that is the reverse of the identical permutation Assume that d = (d1, d2, , dn) is an inversion vector We have to find an inversion vector d' = (d'1, d'2, , d'n) next to the above inversion vector in the sorted sequence By the alphabetical order, the vector d' is inherited a left part as long as possible of the vector d from the coordinator indexed to the coordinator indexed k-1, where: k = max { j ⏐ dj < j - } Then the coordinators indexed from to k-1 are unchanged: d'i = di , i = 1, 2, , k-1; The coordinators indexed from k to the last are determined as follows: d'k = dk + 1, and d'i = , i = k+1, k+2, , n The step terminates when all inversion vectors in Vn have been generated It means, when the last inversion vector n-2 n-1 was generated At that time, the variable: k = This is a termination condition of this algorithm 3.2 Determining permutation from inversion vector Let (d1, d2, , dn) be an inversion vector We have to find a permutation < a1 a2 an >, whose inversion vector is the above vector All components (i = 1, 2, , n) of the permutation belong to the set X = {1, 2, …, n} In order to determine the components, we use a list XS represented by an integer array, that contains remaining elements of the set X after each component selection Elements in the list XS are sorted in ascending First, XS[i] = i , i = 1, 2, , n As an is less than dn elements in the list XS so: an = XS[n - dn] Remove an from XS and gather the list, we get a new n-1 element list Then, an-1 = XS[n-1 - dn-1] Repeat the above process until the list XS becomes empty, we find all components of the permutation < a1 a2 an > 3.3 Algorithm generating permutation from inversion vector Use an integer array D[1 n] to contain inversion vectors, an integer array F[1 n] for permutations and an integer variable l for the current length of the list XS As the above analysis we construct a detail algorithm to generate permutations from inversion vectors as follows Algorithm 3.1 (Generating permutation from inversion vector): Input: A positive integer n Output: A sequence of all permutations of the set {1, 2, …, n}, their inversion vectors are sorted by ascending in the alphabetical order 1 Begin D[1 n] ← ; repeat FIND_PER(); k ←n; while D[k] = k -1 { D[k] ← ; k ← k - } ; if k ≥ then D[k] ← D[k] +1 ; until k = ; End 10 Procedure FIND_PER() ; 11 begin 12 for i ← to n XS[i] ← i ; 13 l ←n ; 14 for i ← n downto 15 { j ← l - D[i] ; 16 F[i] ← XS[j] ; 17 l ←l-1; 18 for i ← j to l XS[i] ← XS[i+1] ; } 19 print F[1 n] ; 20 end ; Complexity of the algorithm: To generate an inversion vector, the algorithm executes two following steps: - Instructions 5) - 7) determine a changing position k and generates an inversion vector with the complexity of O(n) - Procedure FIND_PER() in instructions 10) 20) determines and prints the corresponding permutation by the procedure call 4) with the complexity of O(n.lg n) So the complexity of a permutation generation is O(n.lg n) The total complexity of Algorithm 3.1 is O(n!.n.lg n) The complexities of Algorithm 3.1, the reverse alphabetical order algorithm and the algorithm based on adjacent transpositions presented in [4,8] are the same But Algorithm 3.1 is more simpler if k ≥ then D[k] ← D[k] +1 ; until k = ; End P2 10 Procedure FIND_PER() ; 11 begin 12 for i ← to n XS[i] ← i ; 13 l ←n; 14 for i ← n downto 15 { j ← l - D[i] ; 16 F[i] ← XS[j] ; 17 l ←l-1; 18 for i ← j to l XS[i] ← XS[i+1] ; } 19 print F[1 n] ; 29 end ; Communication between two processors P1 and P2 is done by procedure call FIND_PER() in the instruction 4) The two parts are executed in parallel on two processors P1 and P2 The time for finding all permutations of a set reduces to a half 4.2 Splitting the sequence of inversion vectors In order to execute the algorithm in parallel, we split the sequence of all desirable permutations into subsequences The number of subsequences depends on the number of calculating processors Let us split the sequence of all permutations into m subsequences (m ≥ 2) The scheme of the parallel computing organization to find all permutations of a finite set is illustrated as the following figure IV PARALLEL ALGORITHM FOR PERMUTATIONS GENERATION Algorithm 3.1 may be parallelized by one of the two following methods 4.1 Tasks dividing Algorithm 3.1 consists of a main program and a procedure FIND_PER() The execution of the procedure does not depend on computation in the main program So, the algorithm may be parallelized by dividing whole computation into two parts (tasks), each part is executed by a processor A processor P1 calculates the part generating an inversion vector whilst another processor P2 calculates the part determining a permutation from the just found inversion vector P1 Begin D[1 n] ← ; repeat FIND_PER(); k ←n; while D[k] = k -1 { D[i] ← ; k ← k -1 } ; Fig 4.1 The scheme of the parallel computing organization to find a set's permutations In order to make the parallel computing organization realistic and optimal, the subsequences of permutations should be satisfied two following criteria: 1) It is easy to determine the input and the termination condition for the computing process of each subsequence 2) The difference of lengths of the subsequences is the less, the better Of course, the input is indeed the input of the problem and the last termination condition is the termination condition of the algorithm The first criterion ensures that the splitting of permutations sequence is realistic In many cases, we can use (a part of) the input of the computing process for the next subsequence as the termination condition of the computing process for the previous subsequence The second criterion implements the balance of computing to processors Then the parallel computing processes become optimal To parallelize the new sequential algorithm above presented for the permutation problem, we split the sequence of all desirable inversion vectors into two subsequences (m = 2) The pivot chosen as the inversion vector at the beginning of the second subsequence is: 0 0 (4.1) So, the last inversion vector of the first subsequence is: 0 n-2 n-1 (4.2) It is easy to see that, the first subsequence consists of all inversion vectors in Vn, where two first coordinators are 0, whilst the second one consists of all inversion vectors in Vn, where two first coordinators are So these two subsequences have the same length (n!/2) Thus, the length of subsequences of corresponding permutations are equal The instructions 5) - 6) determine a changing position k on the inversion vector (4.2) at the end of the first subsequence to be So the termination condition for the first computing process is: k = (instruction 8*) The input for the second computing process is the inversion vector (4.1) So we have to add an instruction: 2') D[2] ← ; Two following computing processes are executed in parallel on two processors P1 and P2 P1 Begin D[1 n] ← ; repeat FIND_PER(); k ←n; while D[k] = k -1 { D[i] ← ; k ← k -1 } ; if k ≥ then D[k] ← D[k] +1 ; 8* until k = ; End 10 Procedure FIND_PER() ; 11 begin 12 for i ← to n XS[i] ← i ; 13 l ← n ; 14 for i ← n downto 15 { j ← l - D[i] ; 16 F[i] ← XS[j] ; 17 l ←l-1; 18 for i ← j to l XS[i] ← XS[i+1] ; } 19 print F[1 n] ; 20 end ; Generating permutations of the first subsequence P2 Begin D[1 n] ← ; 2' D[2] ← ; repeat FIND_PER(); k ←n; while D[k] = k -1 {D[i] ← ; k ← k -1} ; if k ≥ then D[k] ← D[k] +1 ; until k = ; End 10 Procedure FIND_PER() ; 11 begin 12 for i ← to n XS[i] ← i ; 13 l ← n ; 14 for i ← n downto 15 { j ← l - D[i] ; 16 F[i] ← XS[j] ; 17 l ←l-1; 18 for i ← j to l XS[i] ← XS[i+1] ; } 19 print F[1 n] ; 20 end ; Generating permutations of the second subsequence Thus, the time for finding all permutations of a set reduces to a half The experimental results show that the above presented techniques have high performance These results take part in development of combinatorial computations and may be applied in practice V CONCLUSION In this paper, we propose a new algorithm to generate permutations of a finite set by inversion vectors We also organize parallel computing for this algorithm by tasks dividing or splitting the sequence of all inversion vectors into subsequences as our new approach presented in [9-10] Therefore, the amount of time required for finding all the permutations will be drastically decreased by the number of subsequences The computing organization is an association of the bottom-up design and the divide and conquer one The technique may be applied well in some other problems and algorithms We keep developing the parallelizing technique for those problems with a huge computing amount ACKNOWLEDGMENT The author would like to thank the Asia Research Center and the Korea Foundation for Advanced Studies as the sponsors for the research project REFERENCES [1] J Ginsburg, Determining a permutation from its set of reductions, Ars Combinatoria, No 82, 2007, pp 55-57 [2] S.M Johnson, Generation of permutations by adjacent transpositions, Mathematics of Computation, No 17 (83), 1963, pp 282-285 [3] T Kuo, A new method for generating permutations in lexicographic order, Journal of Science and Engineering Technology, Vol 5, No 4, 2009, pp 21-20 [4] M Monks, Reconstructing permutations from cycle minors, The Electronic Journal of Combinatorics, No 16, 2009, #R19 [5] W Lipski, Kombinatoryka dla programistów, WNT, Warszawa, 1982 [6] W Reisig, Petri Nets: An Introduction, Springer-Verlag, 1985 [7] J Steinhardt, Permutations with ascending and descending blocks, The Electronic Journal of Combinatorics, No 17, 2010, #R14 [8] H.C Thanh, Combinatorics, VNU Hanoi Press,1999 (in Vietnamese) [9] H.C Thanh, V.T Que, N.D Ham and D.T Ha, Parallelizing computation based on segments of solutions, Proceedings of the 12 Vietnam National Conference on Information Technology, Natural Sciences and Technology Press, Hanoi - 2010, pp 413-425 [10] H.C Thanh and N.Q Thanh, An efficient parallel algorithm for the set partition problem, In "New Challenges for Intelligent Information and Database Systems" (ACIIDS'2011, Daegu, Eds: N T Nguyen, B Trawinski and J J Jung), Springer-Verlag, 2011, pp 2532 ... with the length of n or a directed graph of n nodes Theorem 2.1 points out that inversion vector is a good representation of permutations III PERMUTATIONS GENERATION BY INVERSION VECTORS Based... Determining a permutation from its set of reductions, Ars Combinatoria, No 82, 2007, pp 55-57 [2] S.M Johnson, Generation of permutations by adjacent transpositions, Mathematics of Computation, No 17... organization to find all permutations of a finite set is illustrated as the following figure IV PARALLEL ALGORITHM FOR PERMUTATIONS GENERATION Algorithm 3.1 may be parallelized by one of the two following