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 (sequence A280318 in the OEIS).
Suppose we have a permutation 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. Let us describe Heap's method in a recursive way. First we set a counter i to 0. Now we perform the following steps repeatedly until i is equal to n. We use the algorithm to generate the (n−1)! permutations of the first n−1 elements, adjoining the last element to each of these. This generates all of the permutations that end with the last element. Then if n is odd, we switch the first element and the last one, while if n is even we can switch the ith element and the last one (there is no difference between n even and odd in the first iteration). We add one to the counter i and repeat. In each iteration, the algorithm will produce all of the permutations that end with the element that has just been moved to the "last" position.
Details of the algorithm
The following code outputs all permutations of a data array of length n.
# Python 3.6 def swap(array, i1, i2, swap_count): swap_count.append("") array[i1], array[i2] = array[i2], array[i1] def heaps_recursive(n, array, store, swap_count): c = 0 if n == len(array): # Original array store.append("".join(array)) while True: if n > 2: heaps_recursive(n-1, array, store, swap_count) if n <= c + 1: break elif n % 2 == 1: swap(array, 0, n-1, swap_count) else: swap(array, c, n-1, swap_count) store.append("".join(array)) c += 1
To verify the algorithm:
from itertools import permutations def main(function): string = 'asdfghj' store =  swap_count =  function(len(string), list(string), store, swap_count) reference_permute_no = len(set(permutations(string))) assert len(swap_count) == reference_permute_no - 1 assert len(store) == len(set(store)) assert len(set(store)) == reference_permute_no main(heaps_recursive)
One can also write the algorithm in a non-recursive format.
def heaps(n, a, store, swap_count): c =  * n i = 0 store.append("".join(a)) while i < n: if c[i] < i: if i % 2 == 0: swap(a, 0, i, swap_count) else: swap(a, c[i], i, swap_count) store.append("".join(a)) c[i] += 1 i = 0 else: c[i] = 0 i += 1 main(heaps)
- Heap, B. R. (1963). "Permutations by Interchanges" (PDF). The Computer Journal. 6 (3): 293–4. doi:10.1093/comjnl/6.3.293.
- Sedgewick, R. (1977). "Permutation Generation Methods". ACM Computing Surveys. 9 (2): 137–164. doi:10.1145/356689.356692.
- Sedgewick, Robert. "a talk on Permutation Generation Algorithms" (PDF).