Why does Heap's algorithm work?
If you are looking for an explanation of why Heap’s algorithm constructs all permutations, keep reading.
Heap’s algorithm constructs all permutations of a given sequence. Heap’s algorithm is efficient because it constructs each permutation from the previous by swapping two elements. Heap’s algorithm is more simple than the also efficient SteinhausJohnsonTrotter algorithm because it does not compute an offset for the pairs that swaps.
Despite its virtues, it is not evident why Heap’s algorithm constructs all permutations. Heap did give a reason why his algorithm works, but did not explain the reason. Popular references on the Internet like Wikipedia and Sedgewick do not provide an explanation either. A thorough search reveals Eric Martin’s lecture notes on Heap’s algorithm. The notes explain Heap’s algorithm using a formal approach. This article explains Heap’s algorithm by example. This article also provides a sample C implementation.
If we understand why Heap’s algorithm works, we might understand other problems like how to modify Heap’s algorithm to distribute work amongst several parallel jobs or compute only relevant permutations efficiently.
If you are interested in an iterative version of Heap’s algorithm or permutation algorithms in general, have a look at Sedgewick’s survey on permutation algorithms.
Edits
Edit 2017.07.02: An iterative algorithm. Thanks to 8BitPimp for sharing a C++ implementation of an iterative algorithm that permutes elements in the same order as Heap’s algorithm.
How does Heap’s algorithm work?
For an input sequence of $@n@$ elements, Heap’s algorithm fixes the element in the last position and constructs all permutations for the rest of the elements in place. After that, the algorithm swaps the element in the last position with one of the rest and repeats the process.
For example, for sequence 123
, Heap’s algorithm proceeds in the following way.
Given sequence 123
, swap 1 constructs the other permutation where 3
is in the last position.
Swap 2 moves 2
to the last position to construct one permutation with 2
in the last position and swap 3 constructs the other permutation.
Swap 4 moves 1
to the last position to construct one permutation with 1
in the last position and swap 5 constructs the other.
By moving each element to the last position and constructing the remaining permutations, Heap’s algorithm adjoins each element to all permutations of the other two elements.
Heap’s algorithm as explained in his original work consists of the following steps.
For given input sequence 12...n
do the following.
 Permute the elements in positions $@1@$ to $@n  1@$ by applying this algorithm to those elements.

Apply the following steps $@n  1@$ times. Increment counter $@i = 1@$ after each iteration.

Swap element in position $@n@$ with one of the other elements in the following way.
 If $@n@$ is odd, swap elements in positions $@1@$ and $@n@$.
 If $@n@$ is even, swap elements in position $@i@$ and $@n@$.
 Permute the elements in positions $@1@$ to $@n  1@$ by applying this algorithm to those elements.

Swap element in position $@n@$ with one of the other elements in the following way.
The algorithm applies $@n!  1@$ swaps. The reason is that step 1 applies $@(n  1)!  1@$ swaps and the loop applies $@(n  1)(n  1)!@$ swaps. For example, for input sequence 123
, step 1 corresponds to swap 1, the first iteration of the loop corresponds to swaps 2 and 3, and the second iteration of the loop corresponds to swaps 4 and 5.
Given a permutation where the element in position $@n@$ is $@e@$, recursive steps 1 and 2.2 construct the remaining $@(n  1)!  1@$ permutations where $@e@$ is in the last position by adjoining $@e@$ to the remaining $@(n  1)!  1@$ permutations of the rest of the elements.
To construct all permutations of $@n@$ elements, it is crucial that step 2.1 places a different element in position $@n@$ every time, which it does.
Understanding this property is fundamental to understanding why Heap’s algorithm works.
We explain this property in the next section.
The corresponding pseudocode is the following.
Given input sequence S
and its corresponding length n
, procedure Heap
constructs the remaining $@n!  1@$ permutations of S
.
Lines 10 and 20 correspond to step 1 of the algorithm.
Lines 30 to 100 correspond to the loop of the algorithm.
Line 80 is a call to a procedure that processes the current permutation to solve a particular problem, for example brute search all solution candidates of another problem.
Line 80 applies process
to $@n!  1@$ permutations of input sequence S
but not to S
.
To apply procedure process
to all permutations of S
, apply process
to S
before or after calling Heap
.
Consider the following C implementation for reference.
Why does Heap’s algorithm construct all permutations?
Heap’s algorithm constructs all permutations because it adjoins each element to each permutation of the rest of the elements. Steps 1 and 2.2 of the algorithm take care of adjoining. Step 2.1 takes care of placing a different element in the last position each time.
It may not be evident that Heap’s algorithm places a different element in the last position each time.
There are two cases for a given input sequence 12...n
, either n
is odd or even.
When n
is odd, the sequence of elements for position n
is the following.
The sequence consists of all elements.
For example, consider the execution of Heap’s algorithm for sequence 12345
.
The sequence of elements for the last position is 5, 2, 3, 4, 1
.
When n
is even, the sequence of elements for position n
is the following.
This sequence also consists of all elements.
For example, consider the execution of Heap’s algorithm for input 123456
.
The sequence of elements for the last position is 6, 5, 2, 3, 4, 1
.
In each case the sequence of elements for the last position is determined by two aspects of the algorithm.
One is the rule applied to each swap over the last position and the other is the last permutation given by each corresponding recursive application of the algorithm.
For example, consider again the execution of Heap’s algorithm for input 12345
.
In this case the rule indicates that we always swap the first and last position, so the progres in the sequence of elements in the last position 5, 2, 3, 4, 1
is determined by the corresponding recursive application of the algorithm.
Swaps 24, 48, 72, and 96 are the last swaps of the recursive applications.
They place elements 2, 3, 4, 1
in the first position thus determining the sequence for the last position.
The sequence for the first position of 24, 48, 72, and 96 is given by the following rule.
For sequence 12...n
of even length, the last permutation is the following.
For the case where the input sequence is even consider again the execution for input 123456
.
In this case, the sequence of elements in the last position is determined by the swap rule applied in permutations 121, 241, 361, 481, and 601.
The reason is that the last permutation given by each recusive call 120, 240, 360, 480, and 600 preserves the original position of all elements except the first and the last.
The rule is, for sequence 12...n
of odd length, the last permutation is the following.
The last permutation given by each recursive call and the swap rule over the last element satisfy together the following invariants for input sequence 12...n
.
 When
n
is even, the last permutation is(n2) (n1) 2 3 ... (n3) n 1
.  When
n
is odd, the last permutation isn 2 3 ... (n1) 1
.  After every swap involving position
n
, a different element is placed in positionn
.
For the first invariant consider the sequence of even length 123...n
and observe that the last permutation is (n2) (n1) 2 3 ... (n3) n 1
.
Also observe that after every swap involving position n
, a different element is placed in that position because the sequence of elements for the position is n, (n1), 2, 3, 4, ..., (n2), 1
.
The diagram shows the first four swaps over the last position and also the last two.
We know that the swap marked n2
has n
in the first position and 1
in the second to last position because n2
is an even swap and all even swaps have that property.
We know that the swap marked n2
swaps n2
and n3
because each even swap corresponding to swap count i
takes a current permutation A
and produces permutation B
by swapping the i
th element with the last as follows.
For the second invariant consider the sequence of odd length 12...n
and observe that the last permutation is n 2 3 ... (n1) 1
.
Also observe that after every swap involving psoition n
, a different element is placed in that position because the sequence of elements for the position is n, (n3), (n4), (n5), ..., 2, (n2), (n1), 1
.
We know that swap marked as n4
is as indicated because the sequence of elements for each position and each swap is the following.
Summary
Heap’s algorithm is efficient and has a simple formulation. It may not be evident why it constructs all permutations of a given sequence. We explained by example the reason Heap’s algorithm works, which is that it adjoins each element to the permutations of the rest of the elements.
Do you have any questions, comments, suggestions? Let me know in the comments.
Want to read more?
I love to explain and answer questions on programming problems, the kind you find in coding interviews. I publish a new programming problem and its solution every month. Did I mention that I love to answer questions?
If you would like to get the latest problem + solution, subscribe to the newsletter or subscribe via RSS. You can also follow me on Twitter, GitHub, and LinkedIn.
References
 Heap
 Permutations by interchanges. B. R. Heap, The Computer Journal, 6(3) (1963), pp. 293298
 Martin
 Martin, Eric. "Notes on Cryptarithm Solver and Permutations." Principles of Programming. University of South Wales, n.d. Web. 11 June 2016. https://webcms3.cse.unsw.edu.au/COMP9021/15s2/resources/1544.
 Sedgewick
 Permutation Generation Methods. R. Sedgewick, Computing Surveys, 9(2) (1977), pp. 137164