Iterator
TopologicalSorts
generate permutations with restrictions
Calling Sequence
Parameters
Options
Description
Examples
References
Compatibility
TopologicalSorts(n,rels,opts)
n
-
posint; the size of the set
rels
set(`<`); relations restricting the permutations
opts
(optional) equation(s) of the form option = value; specify options for the TopologicalSorts command
compile = truefalse
True means compile the iterator. The default is true.
inverse = truefalse
True means return the inverse permutations. False means return the normal permutations. The default is false.
The TopologicalSorts command returns an iterator that generates the permutations of the integers 1 to n that obey specified restrictions.
The n parameter is a positive integer that specifies the set of integers, 1 to n.
The rels parameter specifies the relations that restrict the permutations. The relations are true strict-inequalities between the integers 1 to n.
If the relation x<y appears in rels, x precedes y in all permutations.
This iterator object has the common iterator methods.
with⁡Iterator:
Create all permutations of the integers 1 to 4 such that 1 precedes 2, and 3 precedes 4.
T≔TopologicalSorts⁡4,1<2,3<4:
seq⁡t,t=T
1234,1324,1342,3124,3142,3412
Count the number of permutations that meet the restrictions.
add⁡1,t=T
6
Young rectangle
A Young rectangle is a specialization of a Young tableau. It consists of an m×n rectangular arrangement of the integers 1,…,n such that each row is increasing from left to right and each column from top to bottom. In a permutation a,…,an of the integers 1,…,n x precedes y if and only if a′x<a′y in the inverse permutation a′1,…,a′n. Consequently, specifying that i<j means that, in all the inverse permutations, the value in position i is less than that in position j.
Label the cells of a 3x2 matrix as shown.
Matrix⁡3,2,seq⁡1..6
123456
If an inverse permutation meets the requirement of the Young rectangle, the corresponding relations are
rels≔1<2,1<3,2<4,3<4,3<5,4<6,5<6:
Construct the iterator; use the inverse option to generate the inverse permutations.
Y≔TopologicalSorts⁡6,rels,inverse:
Generate and display the results as Matrices. Use ArrayTools[Alias] to create a 3x2 Matrix representation of the Vector output.
A≔ArrayTools:-Alias⁡output⁡Y,3,2,C_order:
seq⁡A,y=Y
123456,123546,132456,132546,142536
Assign a procedure that counts Young rectangles of a given size.
YoungNum := proc(m::nonnegint,n::nonnegint) local i,j,pos,rels,Y; pos := (i,j) -> (i-1)*n+j; rels := {seq(seq(pos(i,j) < pos(i,j+1), j=1..n-1), i=1..m), seq(seq(pos(i,j) < pos(i+1,j), i=1..m-1), j=1..n) }; Y := Iterator:-TopologicalSorts(m*n, rels); add(1, y=Y); end proc:
YoungNum⁡3,2
5
YoungNum⁡3,3
42
YoungNum⁡4,4
24024
Knuth, Donald Ervin. The Art of Computer Programming, volume 4, fascicle 2; generating all tuples and permutations, sec. 7.2.1.2, p. 63, generating all permutations, algorithm V, all topological sorts.
The Iterator[TopologicalSorts] command was introduced in Maple 2016.
For more information on Maple 2016 changes, see Updates in Maple 2016.
See Also
TopologicalSort
Download Help Document
What kind of issue would you like to report? (Optional)