Learn R Programming

expperm (version 0.1.0)

heap: Heap's algorithm for enumerating permutations

Description

Computes the next permutation in a sequence that eventually includes, without repetition, all possible permutations. There are many R packages with functions for computing all permutations, most of which are more highly optimized than heap. The value of the current implementation is its relative transparency and `hack-ability', which helps us use it for the brute-force enumeration of expected permutation matrices.

Usage

heap(ind)

Arguments

ind

A permuted vector of the integers ${1,…,n}$ for some $n$. For all terms in the sequence other than the first, the input is vector is expected to include a `counter' attribute, which is needed by the algorithm to keep track of its position.

Value

The next permutation in the Heap sequence, accompanied by an updated counter attribute.

Examples

Run this code
# NOT RUN {
n<-4
permi<-1:n
allperms<-c()
for(i in 1:factorial(n)){
  allperms<-rbind(allperms,permi)
  permi<-heap(permi)
}
allperms
# }

Run the code above in your browser using DataLab