2 * Introduction to combinatorics::
3 * Functions and Variables for Combinatorics::
6 @node Introduction to combinatorics, Functions and Variables for Combinatorics, Package combinatorics, Package combinatorics
7 @section Package combinatorics
9 The @code{combinatorics} package provides several functions to work with
10 permutations and to permute elements of a list. The permutations of
11 degree @emph{n} are all the @emph{n}! possible orderings of the first
12 @emph{n} positive integers, 1, 2, @dots{}, @emph{n}. The functions in this
13 packages expect a permutation to be represented by a list of those
16 Cycles are represented as a list of two or more integers @emph{i_1},
17 @emph{i_2}, @dots{}, @emph{i_m}, all different. Such a list represents a permutation
18 where the integer @emph{i_2} appears in the @emph{i_1}th position, the
19 integer @emph{i_3} appears in the @emph{i_2}th position and so on, until
20 the integer @emph{i_1}, which appears in the @emph{i_m}th position.
22 For instance, [4, 2, 1, 3] is one of the 24 permutations of degree four,
23 which can also be represented by the cycle [1, 4, 3]. The functions
24 where cycles are used to represent permutations also require the
25 order of the permutation to avoid ambiguity. For instance, the same
26 cycle [1, 4, 3] could refer to the permutation of order 6: [4, 2, 1, 3, 5,
27 6]. A product of cycles must be represented by a list of cycles; the
28 cycles at the end of the list are applied first. For example, [[2,
29 4], [1, 3, 6, 5]] is equivalent to the permutation [3, 4, 6, 2, 1, 5].
31 A cycle can be written in several ways. for instance, [1, 3, 6, 5], [3,
32 6, 5, 1] and [6, 5, 1, 3] are all equivalent. The canonical form used in
33 the package is the one that places the lowest index in the first
34 place. A cycle with only two indices is also called a transposition and
35 if the two indices are consecutive, it is called an adjacent
38 To run an interactive tutorial, use the command @code{demo
39 (combinatorics)}. Since this is an additional package, it must be loaded
40 with the command @code{load("combinatorics")}.
43 @opencatbox{Categories:}
44 @category{Share packages}
45 @category{Package combinatorics}
48 @node Functions and Variables for Combinatorics, , Introduction to combinatorics, Package combinatorics
49 @section Functions and Variables for Combinatorics
52 @deffn {Function} apply_cycles (@var{cl},@var{l})
54 Permutes the list or set @var{l} applying to it the list of cycles
55 @var{cl}. The cycles at the end of the list are applied first and the
56 first cycle in the list @var{cl} is the last one to be applied.
58 See also @mrefdot{permute}
63 @c load("combinatorics")$
64 @c lis1:[a,b*c^2,4,z,x/y,1/2,ff23(x),0];
65 @c apply_cycles ([[1, 6], [2, 6, 5, 7]], lis1);
68 (%i1) load("combinatorics")$
70 (%i2) lis1:[a,b*c^2,4,z,x/y,1/2,ff23(x),0];
72 (%o2) [a, b c , 4, z, -, -, ff23(x), 0]
76 (%i3) apply_cycles ([[1, 6], [2, 6, 5, 7]], lis1);
78 (%o3) [-, -, 4, z, ff23(x), a, b c , 0]
83 @opencatbox{Categories:}
84 @category{Package combinatorics}
90 @deffn {Function} cyclep (@var{c}, @var{n})
92 Returns true if @var{c} is a valid cycle of order @var{n} namely, a list
93 of non-repeated positive integers less or equal to @var{n}. Otherwise,
96 See also @mrefdot{permp}
101 @c load("combinatorics")$
102 @c cyclep ([-2,3,4], 5);
103 @c cyclep ([2,3,4,2], 5);
104 @c cyclep ([6,3,4], 5);
105 @c cyclep ([6,3,4], 6);
108 (%i1) load("combinatorics")$
110 (%i2) cyclep ([-2,3,4], 5);
114 (%i3) cyclep ([2,3,4,2], 5);
118 (%i4) cyclep ([6,3,4], 5);
122 (%i5) cyclep ([6,3,4], 6);
127 @opencatbox{Categories:}
128 @category{Package combinatorics}
134 @deffn {Function} perm_cycles (@var{p})
136 Returns permutation @var{p} as a product of cycles. The cycles are
137 written in a canonical form, in which the lowest index in the cycle is
138 placed in the first position.
140 See also @mrefdot{perm_decomp}
145 @c load("combinatorics")$
146 @c perm_cycles ([4, 6, 3, 1, 7, 5, 2, 8]);
149 (%i1) load("combinatorics")$
151 (%i2) perm_cycles ([4, 6, 3, 1, 7, 5, 2, 8]);
152 (%o2) [[1, 4], [2, 6, 5, 7]]
156 @opencatbox{Categories:}
157 @category{Package combinatorics}
163 @deffn {Function} perm_decomp (@var{p})
165 Returns the minimum set of adjacent transpositions whose product equals
166 the given permutation @var{p}.
168 See also @mrefdot{perm_cycles}
173 @c load("combinatorics")$
174 @c perm_decomp ([4, 6, 3, 1, 7, 5, 2, 8]);
177 (%i1) load("combinatorics")$
179 (%i2) perm_decomp ([4, 6, 3, 1, 7, 5, 2, 8]);
180 (%o2) [[6, 7], [5, 6], [6, 7], [3, 4], [4, 5], [2, 3], [3, 4],
181 [4, 5], [5, 6], [1, 2], [2, 3], [3, 4]]
185 @opencatbox{Categories:}
186 @category{Package combinatorics}
191 @anchor{perm_inverse}
192 @deffn {Function} perm_inverse (@var{p})
194 Returns the inverse of a permutation of @var{p}, namely, a permutation
195 @var{q} such that the products @var{pq} and @var{qp} are equal to the
196 identity permutation: [1, 2, 3, @dots{}, @var{n}], where @var{n} is the
199 See also @mrefdot{permult}
204 @c load("combinatorics")$
205 @c perm_inverse ([4, 6, 3, 1, 7, 5, 2, 8]);
208 (%i1) load("combinatorics")$
210 (%i2) perm_inverse ([4, 6, 3, 1, 7, 5, 2, 8]);
211 (%o2) [4, 7, 3, 1, 6, 2, 5, 8]
215 @opencatbox{Categories:}
216 @category{Package combinatorics}
222 @deffn {Function} perm_length (@var{p})
224 Determines the minimum number of adjacent transpositions necessary to
225 write permutation @var{p} as a product of adjacent transpositions. An
226 adjacent transposition is a cycle with only two numbers, which are
227 consecutive integers.
229 See also @mrefdot{perm_decomp}
234 @c load("combinatorics")$
235 @c perm_length ([4, 6, 3, 1, 7, 5, 2, 8]);
238 (%i1) load("combinatorics")$
240 (%i2) perm_length ([4, 6, 3, 1, 7, 5, 2, 8]);
245 @opencatbox{Categories:}
246 @category{Package combinatorics}
251 @anchor{perm_lex_next}
252 @deffn {Function} perm_lex_next (@var{p})
254 Returns the permutation that comes after the given permutation @var{p},
255 in the sequence of permutations in lexicographic order.
260 @c load("combinatorics")$
261 @c perm_lex_next ([4, 6, 3, 1, 7, 5, 2, 8]);
264 (%i1) load("combinatorics")$
266 (%i2) perm_lex_next ([4, 6, 3, 1, 7, 5, 2, 8]);
267 (%o2) [4, 6, 3, 1, 7, 5, 8, 2]
271 @opencatbox{Categories:}
272 @category{Package combinatorics}
277 @anchor{perm_lex_rank}
278 @deffn {Function} perm_lex_rank (@var{p})
280 Finds the position of permutation @var{p}, an integer from 1 to the
281 degree @var{n} of the permutation, in the sequence of permutations in
284 See also @mref{perm_lex_unrank} and @mrefdot{perms_lex}
289 @c load("combinatorics")$
290 @c perm_lex_rank ([4, 6, 3, 1, 7, 5, 2, 8]);
293 (%i1) load("combinatorics")$
295 (%i2) perm_lex_rank ([4, 6, 3, 1, 7, 5, 2, 8]);
300 @opencatbox{Categories:}
301 @category{Package combinatorics}
306 @anchor{perm_lex_unrank}
307 @deffn {Function} perm_lex_unrank (@var{n}, @var{i})
309 Returns the @emph{n}-degree permutation at position @var{i} (from 1 to
310 @emph{n}!) in the lexicographic ordering of permutations.
312 See also @mref{perm_lex_rank} and @mrefdot{perms_lex}
317 @c load("combinatorics")$
318 @c perm_lex_unrank (8, 18255);
321 (%i1) load("combinatorics")$
323 (%i2) perm_lex_unrank (8, 18255);
324 (%o2) [4, 6, 3, 1, 7, 5, 2, 8]
328 @opencatbox{Categories:}
329 @category{Package combinatorics}
335 @deffn {Function} perm_next (@var{p})
337 Returns the permutation that comes after the given permutation @var{p},
338 in the sequence of permutations in Trotter-Johnson order.
340 See also @mrefdot{perms}
345 @c load("combinatorics")$
346 @c perm_next ([4, 6, 3, 1, 7, 5, 2, 8]);
349 (%i1) load("combinatorics")$
351 (%i2) perm_next ([4, 6, 3, 1, 7, 5, 2, 8]);
352 (%o2) [4, 6, 3, 1, 7, 5, 8, 2]
356 @opencatbox{Categories:}
357 @category{Package combinatorics}
363 @deffn {Function} perm_parity (@var{p})
365 Finds the parity of permutation @var{p}: 0 if the minimum number of
366 adjacent transpositions necessary to write permutation @var{p} as a
367 product of adjacent transpositions is even, or 1 if that number is odd.
369 See also @mrefdot{perm_decomp}
374 @c load("combinatorics")$
375 @c perm_parity ([4, 6, 3, 1, 7, 5, 2, 8]);
378 (%i1) load("combinatorics")$
380 (%i2) perm_parity ([4, 6, 3, 1, 7, 5, 2, 8]);
385 @opencatbox{Categories:}
386 @category{Package combinatorics}
392 @deffn {Function} perm_rank (@var{p})
394 Finds the position of permutation @var{p}, an integer from 1 to the
395 degree @var{n} of the permutation, in the sequence of permutations in
396 Trotter-Johnson order.
398 See also @mref{perm_unrank} and @mrefdot{perms}
403 @c load("combinatorics")$
404 @c perm_rank ([4, 6, 3, 1, 7, 5, 2, 8]);
407 (%i1) load("combinatorics")$
409 (%i2) perm_rank ([4, 6, 3, 1, 7, 5, 2, 8]);
414 @opencatbox{Categories:}
415 @category{Package combinatorics}
420 @anchor{perm_undecomp}
421 @deffn {Function} perm_undecomp (@var{cl}, @var{n})
423 Converts the list of cycles @var{cl} of degree @var{n} into an @var{n}
424 degree permutation, equal to their product.
426 See also @mrefdot{perm_decomp}
431 @c load("combinatorics")$
432 @c perm_undecomp ([[1,6],[2,6,5,7]], 8);
435 (%i1) load("combinatorics")$
437 (%i2) perm_undecomp ([[1,6],[2,6,5,7]], 8);
438 (%o2) [5, 6, 3, 4, 7, 1, 2, 8]
442 @opencatbox{Categories:}
443 @category{Package combinatorics}
449 @deffn {Function} perm_unrank (@var{n}, @var{i})
451 Returns the @emph{n}-degree permutation at position @var{i} (from 1 to
452 @emph{n}!) in the Trotter-Johnson ordering of permutations.
454 See also @mref{perm_rank} and @mrefdot{perms}
459 @c load("combinatorics")$
460 @c perm_unrank (8, 19729);
463 (%i1) load("combinatorics")$
465 (%i2) perm_unrank (8, 19729);
466 (%o2) [4, 6, 3, 1, 7, 5, 2, 8]
470 @opencatbox{Categories:}
471 @category{Package combinatorics}
477 @deffn {Function} permp (@var{p})
479 Returns true if @var{p} is a valid permutation namely, a list of length
480 @var{n}, whose elements are all the positive integers from 1 to @var{n},
481 without repetitions. Otherwise, it returns false.
486 @c load("combinatorics")$
487 @c permp ([2,0,3,1]);
488 @c permp ([2,1,4,3]);
491 (%i1) load("combinatorics")$
493 (%i2) permp ([2,0,3,1]);
497 (%i3) permp ([2,1,4,3]);
502 @opencatbox{Categories:}
503 @category{Package combinatorics}
509 @deffn {Function} perms @
510 @fname{perms} (@var{n}) @
511 @fname{perms} (@var{n}, @var{i}) @
512 @fname{perms} (@var{n}, @var{i}, @var{j})
514 @code{perms(@var{n})} returns a list of all
515 @emph{n}-degree permutations in the so-called Trotter-Johnson order.
517 @code{perms(@var{n}, @var{i})} returns the @emph{n}-degree
518 permutation which is at the @emph{i}th position (from 1 to @emph{n}!) in
519 the Trotter-Johnson ordering of the permutations.
521 @code{perms(@var{n}, @var{i}, @var{j})} returns a list of the @emph{n}-degree
522 permutations between positions @var{i} and @var{j} in the Trotter-Johnson
523 ordering of the permutations.
525 The sequence of permutations in Trotter-Johnson order starts with the
526 identity permutation and each consecutive permutation can be obtained
527 from the previous one a by single adjacent transposition.
529 See also @mref{perm_next}, @mref{perm_rank} and @mrefdot{perm_unrank}
534 @c load("combinatorics")$
537 @c perms (4, 12, 14);
540 (%i1) load("combinatorics")$
543 (%o2) [[1, 2, 3, 4], [1, 2, 4, 3], [1, 4, 2, 3], [4, 1, 2, 3],
544 [4, 1, 3, 2], [1, 4, 3, 2], [1, 3, 4, 2], [1, 3, 2, 4],
545 [3, 1, 2, 4], [3, 1, 4, 2], [3, 4, 1, 2], [4, 3, 1, 2],
546 [4, 3, 2, 1], [3, 4, 2, 1], [3, 2, 4, 1], [3, 2, 1, 4],
547 [2, 3, 1, 4], [2, 3, 4, 1], [2, 4, 3, 1], [4, 2, 3, 1],
548 [4, 2, 1, 3], [2, 4, 1, 3], [2, 1, 4, 3], [2, 1, 3, 4]]
555 (%i4) perms (4, 12, 14);
556 (%o4) [[4, 3, 1, 2], [4, 3, 2, 1], [3, 4, 2, 1]]
560 @opencatbox{Categories:}
561 @category{Package combinatorics}
567 @deffn {Function} perms_lex @
568 @fname{perms_lex} (@var{n}) @
569 @fname{perms_lex} (@var{n}, @var{i}) @
570 @fname{perms_lex} (@var{n}, @var{i}, @var{j})
572 @code{perms_lex(@var{n})} returns a list of all
573 @emph{n}-degree permutations in the so-called lexicographic order.
575 @code{perms_lex(@var{n}, @var{i})} returns the @emph{n}-degree
576 permutation which is at the @emph{i}th position (from 1 to @emph{n}!) in
577 the lexicographic ordering of the permutations.
579 @code{perms_lex(@var{n}, @var{i}, @var{j})} returns a list of the @emph{n}-degree
580 permutations between positions @var{i} and @var{j} in the lexicographic
581 ordering of the permutations.
583 The sequence of permutations in lexicographic order starts with all the
584 permutations with the lowest index, 1, followed by all permutations
585 starting with the following index, 2, and so on. The permutations
586 starting by an index @emph{i} are the permutations of the first @emph{n}
587 integers different from @emph{i} and they are also placed in
588 lexicographic order, where the permutations with the lowest of those
589 integers are placed first and so on.
591 See also @mref{perm_lex_next}, @mref{perm_lex_rank} and
592 @mrefdot{perm_lex_unrank}
597 @c load("combinatorics")$
599 @c perms_lex (4, 12);
600 @c perms_lex (4, 12, 14);
603 (%i1) load("combinatorics")$
606 (%o2) [[1, 2, 3, 4], [1, 2, 4, 3], [1, 3, 2, 4], [1, 3, 4, 2],
607 [1, 4, 2, 3], [1, 4, 3, 2], [2, 1, 3, 4], [2, 1, 4, 3],
608 [2, 3, 1, 4], [2, 3, 4, 1], [2, 4, 1, 3], [2, 4, 3, 1],
609 [3, 1, 2, 4], [3, 1, 4, 2], [3, 2, 1, 4], [3, 2, 4, 1],
610 [3, 4, 1, 2], [3, 4, 2, 1], [4, 1, 2, 3], [4, 1, 3, 2],
611 [4, 2, 1, 3], [4, 2, 3, 1], [4, 3, 1, 2], [4, 3, 2, 1]]
614 (%i3) perms_lex (4, 12);
618 (%i4) perms_lex (4, 12, 14);
619 (%o4) [[2, 4, 3, 1], [3, 1, 2, 4], [3, 1, 4, 2]]
623 @opencatbox{Categories:}
624 @category{Package combinatorics}
630 @deffn {Function} permult (@var{p_1}, @dots{}, @var{p_m})
632 Returns the product of two or more permutations @var{p_1}, @dots{}, @var{p_m}.
637 @c load("combinatorics")$
638 @c permult ([2,3,1], [3,1,2], [2,1,3]);
641 (%i1) load("combinatorics")$
643 (%i2) permult ([2,3,1], [3,1,2], [2,1,3]);
648 @opencatbox{Categories:}
649 @category{Package combinatorics}
655 @deffn {Function} permute (@var{p}, @var{l})
657 Applies the permutation @var{p} to the elements of the list (or set)
663 @c load("combinatorics")$
664 @c lis1: [a,b*c^2,4,z,x/y,1/2,ff23(x),0];
665 @c permute ([4, 6, 3, 1, 7, 5, 2, 8], lis1);
668 (%i1) load("combinatorics")$
670 (%i2) lis1: [a,b*c^2,4,z,x/y,1/2,ff23(x),0];
672 (%o2) [a, b c , 4, z, -, -, ff23(x), 0]
676 (%i3) permute ([4, 6, 3, 1, 7, 5, 2, 8], lis1);
678 (%o3) [z, -, 4, a, ff23(x), -, b c , 0]
683 @opencatbox{Categories:}
684 @category{Package combinatorics}
690 @deffn {Function} random_perm (@var{n})
692 Returns a random permutation of degree @var{n}.
694 See also @mrefdot{random_permutation}
699 @c load("combinatorics")$
703 (%i1) load("combinatorics")$
705 (%i2) random_perm (7);
706 (%o2) [6, 3, 4, 7, 5, 1, 2]
710 @opencatbox{Categories:}
711 @category{Package combinatorics}