Permutations

Permutations are basic elements in algebra. They have a natural non-commutative product (as matrices do as well), and hence can encode highly nontrivial structures in a compact way. Permutations provide a way of representing any finite group, which makes them key tools in many applications in mathematics, science, engineering, or even art. In particular, permutations play a central role in the description of discrete symmetries.

Permutations are, roughly speaking, reorderings of a set of elements, or more precisely, bijections from the set onto itself. Only sets with a finite number of elements will be considered. The number of possible permutations of a set of n elements is n!, and therefore for a moderate number n==100 there are already 100! permutations, which is almost (10^158).

This tutorial discusses how to manipulate permutations in cyclic notation in the Wolfram Language, and "Permutation Lists" describes the relation to permutation list notation. Other tutorials, "Permutation Groups" and "Named Groups", describe how to work with groups of permutations, and "Group Theory Algorithms" shows how to extract information from them without listing all elements of the group.

Representation of a Permutation

Representation of a permutation.

The disjoint cyclic representation of a permutation has the form (RefLink[Cycles, paclet:ref/Cycles])[{(Subscript[cyc, 1]),(Subscript[cyc, 2]),…}] in the Wolfram Language, where the cycles (Subscript[cyc, i]) are disjoint lists of positive integers. Integers are mapped under the permutation to their right neighbors, and the last integer of a cycle is mapped to the first member of that cycle. Integers not present in the cycles are mapped onto themselves, though they could also appear in cycles of length 1, which are called singletons or fixed points. The ordering of cycles is immaterial, and individual cycles can be rotated without changing the permutation. Permutations are automatically canonicalized so that the smallest integer of each cycle comes first, and then cycles are sorted by their first integer.

This is a permutation with two cycles 3->8->5->3 and 6->1->6:

Cycles[{{3,8,5},{6,1}}]

This represents the same permutation. Singletons and empty cycles are removed:

Cycles[{{1,6},{2},{5,3,8},{4},{7},{9},{},{10}}]

Any permutation with only singletons represents the identity:

Cycles[{{5},{8},{3}}]

With permutations containing explicit lists of numbers, there is automatic syntax checking. In other cases, you can use the function (RefLink[PermutationCyclesQ, paclet:ref/PermutationCyclesQ]) to check the syntax.

The cycles must be disjoint:

Cycles[{{1,2},{1,3}}]

Integers must be positive:

Cycles[{{1,0},{-2,3}}]

Numbers other than integers are not accepted:

Cycles[{{1,2},{3,1/2}}]

This is a valid permutation in cyclic form:

PermutationCyclesQ[Cycles[{{1,6},{3,8,5}}]]

These are invalid permutations, for obvious reasons:

PermutationCyclesQ[Cycles[{{1,2},{0,3}}]]

PermutationCyclesQ[Cycles[{{a,1},{2,3}}]]

PermutationCyclesQ[Cycles[{cycle1,cycle2}]]

Permutation Action and Support

If a permutation perm maps the integer p to the integer q, then q is called the image of p under the permutation perm. Images are computed with the function (RefLink[PermutationReplace, paclet:ref/PermutationReplace]).

Standard action of permutations on other expressions.

This is the image of 3:

PermutationReplace[3,Cycles[{{2,3,4}}]]

These are the images of the first 6 integers:

PermutationReplace[Range[6],Cycles[{{2,3,4}}]]

The standard action of permutations can be extended to other objects, like other permutations or arrays of integers or permutations.

Map integers of the first permutation under the second permutation (conjugation):

PermutationReplace[Cycles[{{1,6},{3,8,5}}],Cycles[{{3,2,1}}]]

Permutations are not assumed to belong to any particular finite group, not even a particular symmetric group of some degree. However, there is the concept of support, defined as the set of integers moved by the permutation, which better describes where a permutation acts naturally.

Permutation support functions.

The following permutation moves only five points:

perm=Cycles[{{4,18,55},{21,37}}]

PermutationSupport[perm]

PermutationLength[perm]

The largest moved point is 55:

PermutationMax[perm]

The smallest moved point is 4:

PermutationMin[perm]

Random generation of permutations.

Generate a random permutation moving (perhaps not all) integers {1,…,15}:

SeedRandom[1234];RandomPermutation[15]

Generate several random permutations in a given group:

SeedRandom[1234];RandomPermutation[AlternatingGroup[7],5]

Permute Parts of an Expression

Permutations can be used to permute the parts of other expressions with the function (RefLink[Permute, paclet:ref/Permute]). Integer p being mapped to integer q is interpreted as part p being moved to part q. (RefLink[Permute, paclet:ref/Permute]) never changes the number of elements of an expression, it simply reorders them.

Reordering under a permutation.

Swap the first and last letters of the alphabet:

Permute[CharacterRange["a","z"],Cycles[{{1,26}}]]

Permute three parts of an expression, leaving the rest invariant:

expr=f[u,v,w,x,y,z];
perm=Cycles[{{2,3,4}}];

Permute[expr,perm]

The same result can be obtained using a permutation list representation and the function (RefLink[Part, paclet:ref/Part]):

permlist=PermutationList[perm,6]

expr[[Ordering@permlist]]

PermutationCycles[permlist]===perm

However, the length of the permutation list must match that of the expression:

expr=f["a","b","c","d","e","f","g","h"];
Permute[expr,perm]

expr[[Ordering@permlist]]

Permutation lists can also be used with the function (RefLink[Permute, paclet:ref/Permute]):

Permute[expr,permlist]

Vice versa, given two expressions with the same elements, a permutation linking them can be computed:

expr1=f[u,v,w,x,y,z];
expr2=f[u,x,v,w,y,z];

FindPermutation[expr1,expr2]

Permute[expr1,%]===expr2

Reversing the two arguments gives the inverse permutation:

FindPermutation[expr2,expr1]

With a single argument, (RefLink[FindPermutation, paclet:ref/FindPermutation]) returns the permutation that generates an expression from its canonical order:

FindPermutation[Characters["Mathematica"]]

Permute[Sort@Characters["Mathematica"],%]

Product of Permutations

Product of permutations.

There are two possible conventions for the product of two permutations (Subscript[perm, 1]) and (Subscript[perm, 2]), depending on whether (RefLink[PermutationProduct, paclet:ref/PermutationProduct])[(Subscript[perm, 1]),(Subscript[perm, 2])] means that you first use (Subscript[perm, 1]) and then (Subscript[perm, 2]) (this is called a left-to-right product) or you first use (Subscript[perm, 2]) and then (Subscript[perm, 1]) (right-to-left product). With the convention of writing images as right neighbors in cycles, the Wolfram Language's (RefLink[PermutationProduct, paclet:ref/PermutationProduct]) effectively is a left-to-right product.

The product of permutations is not commutative:

perm1=Cycles[{{2,3,4}}];
perm2=Cycles[{{1,2},{3,4}}];

PermutationProduct[perm1,perm2]

PermutationProduct[perm2,perm1]

(RefLink[PermutationReplace, paclet:ref/PermutationReplace]) acts from the right, consistent with the left-to-right character of the product:

PermutationReplace[PermutationReplace[3,perm1],perm2]===PermutationReplace[3,PermutationProduct[perm1,perm2]]

PermutationReplace[PermutationReplace[3,perm1],perm2]===PermutationReplace[3,PermutationProduct[perm2,perm1]]

(RefLink[Permute, paclet:ref/Permute]) also acts from the right:

Permute[Permute[{x,y,z,t},perm1],perm2]===Permute[{x,y,z,t},PermutationProduct[perm1,perm2]]

Permute[Permute[{x,y,z,t},perm1],perm2]===Permute[{x,y,z,t},PermutationProduct[perm2,perm1]]

After the permutation product law has been given, the associated concepts of inversion, power, and order can be immediately defined. For permutations of finite support, the order is always finite.

You can compute inverses of permutations:

InversePermutation[perm1]

PermutationProduct[perm1,%]

And you can compute powers of them. Negative powers are powers of the inverse:

PermutationPower[perm1,4]

PermutationProduct[perm1,perm1,perm1,perm1]===%

PermutationPower[perm2,-11]

PermutationPower[perm1,0]

The order of a permutation is the lowest positive power such that the identity permutation is obtained:

PermutationOrder[perm1]

PermutationPower[perm1,%]

Another example:

perm3=Cycles[{{1,9,12,3},{2,17,4,29,20,30,26,15,14},{8,10,16,13,27,25,22,28,23,19,24,18,11}}];

order=PermutationOrder[perm3]

PermutationPower[perm3,order]

This means that (for permutations of finite support) the inverse is always a positive power of the permutation:

InversePermutation[perm3]==PermutationPower[perm3,order-1]

Equality and Sorting of Permutations

As is standard in the Wolfram Language, there are two types of equality tests: structural equality ((RefLink[SameQ, paclet:ref/SameQ])) and mathematical equality ((RefLink[Equal, paclet:ref/Equal])). The former can compare any two expressions, but the latter will only return (RefLink[True, paclet:ref/True]) or (RefLink[False, paclet:ref/False]) when comparing mathematical expressions with the same type of value. The same happens when ordering expressions: there is structural (canonical) order, implemented through (RefLink[Order, paclet:ref/Order]) and (RefLink[OrderedQ, paclet:ref/OrderedQ]), and there is mathematical order, given by (RefLink[Less, paclet:ref/Less]) and related functions.

Any two permutations of any degree can always be tested for equality and ordering. This is done by comparing sequentially the images of the integers 1, 2, 3, … . The smaller permutation corresponds to smaller images, such that the identity permutation always comes first. This defines mathematical order. Canonical order follows standard rules, and may differ from mathematical order.

Take two permutations:

perm1=Cycles[{{2,5,4}}];
perm2=Cycles[{{3,4,7},{5,6}}];

These are the images of the first 7 integers under them:

PermutationReplace[Range[7],perm1]

PermutationReplace[Range[7],perm2]

Permutation perm2 must be sorted before perm1 because its second image is smaller:

perm1<perm2

The standard canonical order will sort smaller expressions first:

Sort[{Cycles[{{1,3}}],Cycles[{{1,2,3}}],Cycles[{{1,3,2}}]}]

You can sort permutations using mathematical order by using an "ordering function":

Sort[{Cycles[{{1,3}}],Cycles[{{1,2,3}}],Cycles[{{1,3,2}}]},LessEqual]

You can check whether a list of permutations is sorted:

OrderedQ[%,LessEqual]

Special Types of Permutations

There are special types of permutations. In many cases the cyclic notation allows simple constructions to detect them.

Involutions contain only cycles of maximal length 2:

permutationInvolutionQ[perm_?PermutationCyclesQ]:=MatchQ[perm,Cycles[{{_,_}...}]]

permutationInvolutionQ[Cycles[{{1,2},{4,8},{3,6}}]]

PermutationPower[Cycles[{{1,2},{4,8},{3,6}}],2]

A permutation is a derangement of degree n if all integers {1,…,n} are moved:

permutationDerangementQ[perm_?PermutationCyclesQ,n_]:=Union@@First[perm]===Range[n]

permutationDerangementQ[Cycles[{{1,3,5},{2,4}}],5]

permutationDerangementQ[Cycles[{{1,2,4,5}}],5]

A permutation is said to be circular of degree n if it consists of a single cycle of length n:

circularPermutationQ[perm_?PermutationCyclesQ,n_]:=Length@First[perm]===1&&Length@First@First[perm]===n

circularPermutationQ[Cycles[{{1,5,3,2,6,4}}],6]

circularPermutationQ[Cycles[{{1,3,5},{2,4}}],5]

From the cyclic notation of a permutation it is also simple to construct a decomposition into transpositions, that is, permutations with a single cycle of length 2.

These functions construct a list of transpositions whose product is the original permutation:

breakCycle[{first_,rest___}]:={first,#}&/@{rest}
toTranspositions[perm_?PermutationCyclesQ]:=Cycles/@List/@Flatten[breakCycle/@First[perm],1]

perm=Cycles[{{1,2,5},{3,7},{6,8,10}}];

toTranspositions[perm]

perm==PermutationProduct@@%

Such decomposition into transpositions is not unique, but the parity (even or odd) of the number of transpositions is invariant. This is called the signature of the permutation:

permutationSignature[perm_?PermutationCyclesQ]:=Apply[Times,(-1)^(Length/@First[perm]-1)]

permutationSignature[Cycles[{{4,3,5,2,1}}]]

permutationSignature[Cycles[{{1,2,3},{4,5}}]]

The previous example permutation perm was decomposed into five transpositions, an odd number. Hence it has signature -1:

permutationSignature[perm]