ArticleslgStudy

computer science

Heap's algorithm

Heap's algorithm is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Heap's algorithm rather than just read about it. In short: Heap's algorithm generates all possible permutations of n objects. It was first proposed by B.

Heap's algorithm — main illustration
Heap's algorithm — illustration

Key takeaways

  • Heap's algorithm belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Heap's algorithm to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Heap's algorithm from memory before moving on to harder problems.

Reference excerpt

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).

Details of the algorithm For a collection C {\displaystyle 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 {\displaystyle k} initial elements of the collection. Initially k = n {\displaystyle k=n} and thereafter k < n {\displaystyle k<n} . Each step generates the k ! {\displaystyle k!} permutations that end with the same n − k {\displaystyle n-k} final elements. It does this by calling itself once with the k th {\displaystyle k{\text{th}}} element unaltered and then k − 1 {\displaystyle k-1} times with the ( k th {\displaystyle k{\text{th}}} ) element exchanged for each of the initial k − 1 {\displaystyle k-1} elements. The recursive calls modify the initial k − 1 {\displaystyle 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 {\displaystyle k} is even, then the final element is iteratively exchanged with each element index. If k {\displaystyle 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 below implementation as Heap's algorithm as it makes the analysis easier, and certain patterns can be easily illustrated. While it is not optimal (it does not minimize moves, which is described in the section below), the implementation is correct and will produce all permutations.

Claim: If array A has length n, then permutations(n, A) will result in either A being unchanged, if n is odd, or, if n is even, then A is rotated to the right by 1 (last element shifted in front of other elements). Base: If array A has length 1, then permutations(1, A) will output A and stop, so A is unchanged. Since 1 is odd, this is what was claimed, so the claim is true for arrays of length 1. Induction: If the claim is true for arrays of length l ≥ 1, then we show that the claim is true for arrays of length l+1 (together with the base case this proves that the claim is true for arrays of all lengths). Since the claim depends on whether l is odd or even, we prove each case separately. If l is odd, then, by the induction hypothesis, for an array A of length l, permutations(l, A) will not change A, and for the claim to hold for arrays of length l+1 (which is even), we need to show that permutations(l+1, A) rotates A to the right by 1 position. Doing permutations(l+1, A) will first do permutations(l, A) (leaving A unchanged since l is odd) and then in each iteration i of the for-loop, swap the elements in positions i and l (the last position) in A. The first swap puts element l (the last element) in position 0, and element 0 in position l. The next swap puts the element in position l (where the previous iteration put original element 0) in position 1 and element 1 in position l. In the penultimate iteration, the swap puts element l-1 in position l, and the element in position l (where the previous iteration put original element l-2) in position l-1. In the final iteration, the swap exchanges position l with itself and therefore leaves the array unchanged. To illustrate the above, look below for the case n = 4.

1,2,3,4 ... original array 1,2,3,4 ... 1st iteration (permute subarray) 4,2,3,1 ... 1st iteration (swap 1st element into last position) 4,2,3,1 ... 2nd iteration (permute subarray) 4,1,3,2 ... 2nd iteration (swap 2nd element into last position) 4,1,3,2 ... 3rd iteration (permute subarray) 4,1,2,3 ... 3rd iteration (swap 3rd element into last position) 4,1,2,3 ... 4th iteration (permute subarray) 4,1,2,3 ... 4th iteration (swap 4th element into last position) The altered array is a rotated version of the original

If l is even, then, by the induction hypothesis, for an array A of length l, permutations(l, A) rotates A to the right by 1 position, and for the claim to hold for arrays of length l+1 (which is odd), we need to show that permutations(l+1, A) leaves A unchanged. Doing permutations(l+1, A) will in each iteration i of the for-loop, first do permutations(l, A) (rotating the first l elements of A by 1 position since l is even) and then, swap the elements in positions 0 and l (the last position) in A. Rotating the first l elements and then swapping the first and last elements is equivalent to rotating the entire array. Since there are as many iterations of the loop as there are elements in the array, the entire array is rotated until each element returns to where it started. To illustrate the above, look below for the case n = 5.

… excerpt ends here. Continue reading the full article.

Illustrations

Heap's algorithm: A map of the 24 permutations and the 23 swaps used in Heap's algorithm permuting the four letters A (amber), B (blue), C (cyan) and D (dark red)
A map of the 24 permutations and the 23 swaps used in Heap's algorithm permuting the four letters A (amber), B (blue), C (cyan) and D (dark red)
Heap's algorithm: Wheel diagram of all permutations of length 
  
    
      
        n
        =
        4
      
    
    {\displaystyle n=4}
  
 generated by Heap's algorithm, where each permutation is color-coded (1=blue, 2=green, 3=yellow, 4=red).
Wheel diagram of all permutations of length n = 4 {\displaystyle n=4} generated by Heap's algorithm, where each permutation is color-coded (1=blue, 2=green, 3=yellow, 4=red).

Worked examples

Example 1 — a first encounter with Heap's algorithm

Start with the simplest possible case. Write down what Heap's algorithm claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Heap's algorithm before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Heap's algorithm ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Heap's algorithm

In research
Heap's algorithm appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Heap's algorithm in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Heap's algorithm is common in secondary-school and first-year university syllabi. It links to neighbouring topics Combinatorial algorithms, Permutations, so understanding it makes those chapters shorter.
In everyday life
Look for Heap's algorithm outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Heap's algorithm in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Heap's algorithm means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Heap's algorithm out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Heap's algorithm in simple terms?

Heap's algorithm generates all possible permutations of n objects. It was first proposed by B.

Why does Heap's algorithm matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Heap's algorithm?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Heap's algorithm.

Tags

  • Combinatorial algorithms
  • Permutations

Keep exploring