perms | R Documentation |
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.
perms(n) plainperms(n)
n |
Integer argument; permutations of |
Comments in the C
code; algorithm lifted from
‘fasc2b.pdf’.
D. E. Knuth; C
and R transliteration by Robin K. S. Hankin
D. E. Knuth 2004. “The art of computer programming,
pre-fascicle 2B. A draft of section 7.2.1.2: Generating all
permutations”.
https://www-cs-faculty.stanford.edu/~knuth/taocp.html
parts
perms(4) 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))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.