Permutations and Combinations

Permutations and subsets are the most basic combinatorial objects. DiscreteMath`Combinatorica` provides functions for constructing objects both randomly and deterministically, to rank and unrank them, and to compute invariants on them. Here we provide examples of some of these functions in action.

These permutations are generated in minimum change order, where successive permutations differ by exactly one transposition. The built­in generator Permutations constructs permutations in lexicographic order.

MinimumChangePermutations[{a,b,c,d}]
[Graphics:combinatoricagr2.gif][Graphics:combinatoricagr1.gif]

The ranking function illustrates that the built­in function Permutations uses lexicographic sequencing.

Map[RankPermutation, Permutations[{1,2,3,4}]]
[Graphics:combinatoricagr2.gif][Graphics:combinatoricagr3.gif]

With 3! = 6 distinct permutations of three elements, within 20 random permutations we are likely to see all of them. Observe that it is unlikely for the first six permutations to all be distinct.

Table[RandomPermutation[3], {20}]
[Graphics:combinatoricagr2.gif][Graphics:combinatoricagr5.gif]

A fixed point of a permutation p is an element in the same position in p as in the inverse of p. Thus, the only fixed point in this permutation is 7.

InversePermutation[{4,8,5,2,1,3,7,6}]
[Graphics:combinatoricagr2.gif][Graphics:combinatoricagr9.gif]

The identity permutation consists of n singleton cycles or fixed points.

ToCycles[{1,2,3,4,5,6,7,8,9,10}]
[Graphics:combinatoricagr2.gif][Graphics:combinatoricagr11.gif]

The classic problem in Polya theory is counting how many different ways necklaces can be made out of k beads, when there are m different types or colors of beads to choose from. When two necklaces are considered the same if they can be obtained only by rotating the beads (as opposed to turning the necklace over), the symmetries are defined by k permutations, each of which is a cyclic shift of the identity permutation. When a variable is specified for the number of colors, a polynomial results.

Polya[Table[
RotateRight[Range[8],i], {i,8}], m]
[Graphics:combinatoricagr2.gif][Graphics:combinatoricagr15.gif]

The number of inversions in a permutation is equal to that of its inverse.

(p=RandomPermutation[50]; {Inversions[p], Inversions[InversePermutation[p]]})
[Graphics:combinatoricagr2.gif][Graphics:combinatoricagr16.gif]

Generating subsets incrementally is efficient when the goal is to find the first subset with a given property, since every subset need not be constructed.

Table[NthSubset[n,{a,b,c,d}], {n,0,15}]
[Graphics:combinatoricagr2.gif][Graphics:combinatoricagr17.gif]

In a Gray code, each subset differs in exactly one element from its neighbors. Observe that the last eight subsets all contain 4, while none of the first eight do.

GrayCode[{1,2,3,4}]
[Graphics:combinatoricagr2.gif][Graphics:combinatoricagr19.gif]

A k­subset is a subset with exactly k elements in it. Since the lead element is placed in first, the k­subsets are given in lexicographic order.

KSubsets[{1,2,3,4,5},3]
[Graphics:combinatoricagr2.gif][Graphics:combinatoricagr22.gif]

[Graphics:combinatoricagr2.gif][Graphics:combinatoricagr23.gif]

Combinatorica functions for permutations and combinations.