Heap's algorithm



Heap's algorithm generates all possible permutations of $n$ objects. It was first proposed by B. R. Heap in 1963. The algorithm minimizes movement: it generates each permutation from the previous one by interchanging a single pair of elements; the other $n−2$ elements are not disturbed. In a 1977 review of permutation-generating algorithms, Robert Sedgewick concluded that it was at that time the most effective algorithm for generating permutations by computer.

The sequence of permutations of $n$ objects generated by Heap's algorithm is the beginning of the sequence of permutations of $n+1$ objects. So there is one infinite sequence of permutations generated by Heap's algorithm.

Details of the algorithm
For a collection $$C$$ containing $n$ different elements, Heap found a systematic method for choosing at each step a pair of elements to switch in order to produce every possible permutation of these elements exactly once.

Described recursively as a decrease and conquer method, Heap's algorithm operates at each step on the $$k$$ initial elements of the collection. Initially $$k=n$$ and thereafter $$k<n$$. Each step generates the $$k!$$ permutations that end with the same $$n-k$$ final elements. It does this by calling itself once with the $$k\text{th}$$ element unaltered and then $$k-1$$ times with the ($$k\text{th}$$) element exchanged for each of the initial $$k-1$$ elements. The recursive calls modify the initial $$k-1$$ elements and a rule is needed at each iteration to select which will be exchanged with the last. Heap's method says that this choice can be made by the parity of the number of elements operated on at this step. If $$k$$ is even, then the final element is iteratively exchanged with each element index. If $$k$$ is odd, the final element is always exchanged with the first.

One can also write the algorithm in a non-recursive format.

Proof
In this proof, we'll use the implementation below as Heap's Algorithm. While it is not optimal (it does not minimize moves, which is described in the section below), the implementation is nevertheless still correct and will produce all permutations. The reason for using the below implementation is that the analysis is easier, and certain patterns can be easily illustrated.

Claim: If array $A$ has length $n$, then performing Heap's algorithm will either result in $A$ being "rotated" to the right by 1 (i.e. each element is shifted to the right with the last element occupying the first position) or result in $A$ being unaltered, depending if $n$ is even or odd, respectively.

Basis: The claim above trivially holds true for $$n=1$$ as Heap's algorithm will simply return $A$ unaltered in order.

Induction: Assume the claim holds true for some $$i \geq 1$$. We will then need to handle two cases for $$i+1$$: $$i+1$$ is even or odd.

If, for $A$, $$n=i+1$$ is even, then the subset of the first $i$ elements will remain unaltered after performing Heap's Algorithm on the subarray, as assumed by the induction hypothesis. By performing Heap's Algorithm on the subarray and then performing the swapping operation, in the $k$th iteration of the for-loop, where $$k \leq i+1$$, the $k$th element in $A$ will be swapped into the last position of $A$ which can be thought as a kind of "buffer". By swapping the 1st and last element, then swapping 2nd and last, all the way until the $n$th and last elements are swapped, the array will at last experience a rotation. To illustrate the above, look below for the case $$n = 4$$ 1,2,3,4 ... Original Array 1,2,3,4 ... 1st iteration (Permute subset) 4,2,3,1 ... 1st iteration (Swap 1st element into "buffer") 4,2,3,1 ... 2nd iteration (Permute subset) 4,1,3,2 ... 2nd iteration (Swap 2nd element into "buffer") 4,1,3,2 ... 3rd iteration (Permute subset) 4,1,2,3 ... 3rd iteration (Swap 3rd element into "buffer") 4,1,2,3 ... 4th iteration (Permute subset) 4,1,2,3 ... 4th iteration (Swap 4th element into "buffer") ... The altered array is a rotated version of the original

If, for $A$, $$n=i+1$$ is odd, then the subset of the first $i$ elements will be rotated after performing Heap's Algorithm on the first $i$ elements. Notice that, after 1 iteration of the for-loop, when performing Heap's Algorithm on $A$, $A$ is rotated to the right by 1. By the induction hypothesis, it is assumed that the first $i$ elements will rotate. After this rotation, the first element of $A$ will be swapped into the buffer which, when combined with the previous rotation operation, will in essence perform a rotation on the array. Perform this rotation operation $n$ times, and the array will revert to its original state. This is illustrated below for the case $$n = 5$$. 1,2,3,4,5 ... Original Array 4,1,2,3,5 ... 1st iteration (Permute subset/Rotate subset) 5,1,2,3,4 ... 1st iteration (Swap) 3,5,1,2,4 ... 2nd iteration (Permute subset/Rotate subset) 4,5,1,2,3 ... 2nd iteration (Swap) 2,4,5,1,3 ... 3rd iteration (Permute subset/Rotate subset) 3,4,5,1,2 ... 3rd iteration (Swap) 1,3,4,5,2 ... 4th iteration (Permute subset/Rotate subset) 2,3,4,5,1 ... 4th iteration (Swap) 5,2,3,4,1 ... 5th iteration (Permute subset/Rotate subset) 1,2,3,4,5 ... 5th iteration (Swap) ... The final state of the array is in the same order as the original

The induction proof for the claim is now complete, which will now lead to why Heap's Algorithm creates all permutations of array $A$. Once again we will prove by induction the correctness of Heap's Algorithm.

Basis: Heap's Algorithm trivially permutes an array $A$ of size $1$ as outputting $A$ is the one and only permutation of $A$.

Induction: Assume Heap's Algorithm permutes an array of size $i$. Using the results from the previous proof, every element of $A$ will be in the "buffer" once when the first $i$ elements are permuted. Because permutations of an array can be made by altering some array $A$ through the removal of an element $x$ from $A$ then tacking on $x$ to each permutation of the altered array, it follows that Heap's Algorithm permutes an array of size $$i+1$$, for the "buffer" in essence holds the removed element, being tacked onto the permutations of the subarray of size $i$. Because each iteration of Heap's Algorithm has a different element of $A$ occupying the buffer when the subarray is permuted, every permutation is generated as each element of $A$ has a chance to be tacked onto the permutations of the array $A$ without the buffer element.

Frequent mis-implementations
It is tempting to simplify the recursive version given above by reducing the instances of recursive calls. For example, as:

This implementation will succeed in producing all permutations but does not minimize movement. As the recursive call-stacks unwind, it results in additional swaps at each level. Half of these will be no-ops of $$A[i]$$ and $$A[k-1]$$ where $$i==k-1$$ but when $$k$$ is odd, it results in additional swaps of the $$kth$$ with the $$0th$$ element.

These additional swaps significantly alter the order of the $$k-1$$ prefix elements.

The additional swaps can be avoided by either adding an additional recursive call before the loop and looping $$k-1$$ times (as above) or looping $$k$$ times and checking that $$i$$ is less than $$k-1$$ as in:

The choice is primarily aesthetic but the latter results in checking the value of $$i$$ twice as often.