perms: Enumerate the permutations of a vector

Description Usage Arguments Note Author(s) References See Also Examples

Description

Given an integer n, return a matrix whose columns enumerate various permutations of 1:n.

Function perms() returns all permutations in lexicographic order; function plainperms() returns all permutations by repeatedly exchanging adjacent pairs.

Usage

1
2

Arguments

n

Integer argument; permutations of 1:n returned

Note

Comments in the C code; algorithm lifted from ‘fasc2b.pdf’.

Author(s)

D. E. Knuth; C and R transliteration by Robin K. S. Hankin

References

See Also

parts

Examples

1
2
3
4
5
6
summary(perms(5))

# Knuth's Figure 18:
matplot(t(apply(plainperms(4),2,order)),
        type='l', lty=1, lwd=5, asp=1,
        frame=FALSE, axes=FALSE, ylab="", col=gray((1:5)/5))

partitions documentation built on July 7, 2017, 9:03 a.m.