Upcoming SlideShare
×

# A Formal Model and an Algorithm for Generating the Permutations of a Multiset

526 views
376 views

Published on

This paper may be considered as a mathematical divertissement as well as a didactical tool for
undergraduate students in a universitary course on algorithms and computation. The well-known problem of
generating the permutations of a multiset of marks is considered. We define a formal model and an abstract
machine (an extended Turing machine). Then we write an algorithm to compute on that machine the successor
of a given permutation in the lexicographically ordered set of permutations of a multiset. Within the model we
analyze the algorithm, prove its correctness, and show that the algorithm solves the above problem. Then we
describe a slight modification of the algorithm and we analyze in which cases it may result in an improvement of
execution times.

This paper, the ideas in it, and its realization are the work of the first author only.

Published in: Education, Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
526
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
4
0
Likes
0
Embeds 0
No embeds

No notes for slide

### A Formal Model and an Algorithm for Generating the Permutations of a Multiset

1. 1. A Formal Model and an Algorithm for Generating the Permutations of a Multiset VINCENZO DE FLORIO and GEERT DECONINCK Electrical Engineering Department – ESAT Katholieke Universiteit Leuven, Kasteelpark Arenberg 10, 3001 Leuven-Heverlee BELGIUM Abstract: – This paper may be considered as a mathematical divertissement as well as a didactical tool for undergraduate students in a universitary course on algorithms and computation. The well-known problem of generating the permutations of a multiset of marks is considered. We deﬁne a formal model and an abstract machine (an extended Turing machine). Then we write an algorithm to compute on that machine the successor of a given permutation in the lexicographically ordered set of permutations of a multiset. Within the model we analyze the algorithm, prove its correctness, and show that the algorithm solves the above problem. Then we describe a slight modiﬁcation of the algorithm and we analyze in which cases it may result in an improvement of execution times. Key-Words: – Computational combinatorics, permutations, Turing machines, algorithms, number theory. 1 Introduction {0, 1, . . . , n − 1}. Let furthermore m be an integer, 0 < m ≤ n. The problem of generating the permutations of a multiset of marks is considered. First some mathematical entities and properties are deﬁned within a formal model. Then an abstract machine is introduced and an algorithm is shown which computes those entities on the abstract machine. Within this framework the algorithm is analyzed and proved to be correct. Finally it is shown that the algorithm computes the successor of a given permutation in the ordered set of lexicographically ordered permutations. The structure of this work is as follows: the formal model, the machine and the algorithm are described in Section 2. Section 3 deals with the link between the algorithm and the process of generating the permutations of a multiset in lexicographical order. Some conclusions are drawn in Section 4. 2 2.1 Deﬁnition 1 Set A = {a0 , a1 , . . . , am−1 } be called “the Alphabet”. Let Im = {0, 1, . . . , m − 1}. Let us consider multiset M = {a0 , . . . , a0 , a1 , . . . , a1 , · · · , am−1 , . . . , am−1 } c0 such that i∈Im ci c1 cm−1 = n and ∀i ∈ Im : ci > 0. Deﬁnition 2 Any ordered arrangement of the elements of a multiset M be called M -permutation and represented as pM or, where M is evident from the context, as p. Let pM [i] (or p[i]) denote the (i + 1)th element of p. Let us call P M (or simply P when unambiguous) the set of the M -permutations. A machine and an algorithm Let o : An → In be a bijection deﬁned so that ∀i ∈ In : o(ai ) = i. Clearly o induces a total order on the marks (and, a fortiori, on the elements of the Alphabet). Furthermore, through o, any M -permutation may be interpreted as an n-digit, base-m number [1]. Prologue—some deﬁnitions Let n be an integer, n > 2, and let An = {a0 , a1 , . . . , an−1 } be a set of “marks”. Let In be set 1
2. 2. 2.2 Deﬁnition 3 Given multiset M as above, arrangement Let us consider M, a generalized Turing machine [6] with two tapes, T1 and T2 , and three heads, H1l , H1r , and H2 . Heads H1l and H1r are “jointed”, i.e., capable of reading or writing any two consecutive squares of T1 at a time. H2 operates on T2 . Let us call S1 and S2 resp. the set of squares of T1 and the set of squares of T2 . a0 · · · · · a0 · a1 · · · · · a1 · · · am−1 · · · · · am−1 c0 c1 A machine cm−1 is called the zero permutation of M , or brieﬂy its zero, and is denoted as pM or simply as p0 when this can be 0 done unambiguously. For the sake of brevity, operator “·”, concatenating any two marks, will be omitted in the following. Deﬁnition 7 Be s a square; square s then represents the square which immediately follows s. For any t ∈ N t Deﬁnition 4 Given multiset M as above, arrangement let s cm−2 represent square ((. . . ( s ) . . . ) . Deﬁnition 8 Let z1 ∈ S1 . such that   π1 (s) = 0    π1 (s) = t       ∀s ∈ S1 :   π1 (s) = −t         am−1 . . . am−1 am−2 . . . am−2 · · · a0 . . . a0 cm−1 ... c0 is called the last permutation of M and is denoted as pM . ∞ Deﬁnition 5 Given any two M -permutations, p1 and p2 : • p1 is said to be equal to p2 iff ∀i ∈ In : p1 [i] = p2 [i]. Let p1 = p2 represent this property. t t Bijection π1 : S1 → Z, if s = z1 , if s = z1 and ∃t ∈ N t (s = z1 . . . ), if s = z1 and ∃t ∈ N t (z1 = s ... ), is called the relative distance from z1 in S1 . Following the same procedure, let π2 : S2 → Z be the relative distance from a ﬁxed z2 ∈ S2 . • p1 and p2 are said to be different iff ¬(p1 = p2 ), or p1 = p2 for brevity. From Def. 2, any M -permutation p can be coded on Tape T1 of M by writing, ∀i ∈ In , mark p[i] onto • p1 < p2 iff ∃k ∈ In (∀j < k : p1 [j] = p2 [j]) ∧ (p1 [k] < p2 [k]). i Theorem 1 For any M , pM is the only possible INF∞ permutation. −1 square π1 (i), i.e., onto square z1 . . . . Machine M can execute read, write and headmovement actions. These basic actions can be described via the following symbolic notation: Let i be an integer and Hx and Hy be heads respectively operating on Tape Tx and Tape Ty (possibly the same). Let sx be the square under Head Hx and sy the square under Hy . Let us call πx and πy the relative distances respectively for Tx and Ty (possibly equal). Let r be a function converting a square into the mark currently coded onto that square. Then let: P ROOF By construction, pM is INF. Ab absurdo, let us ∞ suppose there exists a permutation p = pM which is ∞ INF. Then either p < pM or vice-versa. In both cases ∞ we reach a contradiction by Def. 5. . (Hx ) represent the output of a read of square sx , available as o(r(sx )) ∈ Im , and let Hx represent the position of Head Hx , available as πx (sx ) ∈ Z. Deﬁnition 6 Let p ∈ P M . An inversion is a couple of contiguous marks of p, p[i] and p[i + 1], i ∈ {0, . . . , n − 2}, such that p[i] < p[i + 1]. If no such couple can be found in p, then p is said to be inversionfree (INF). If at least one such couple does exist, then p is said to be non-inversion-free (NIF). 2