Book contents
- Frontmatter
- Contents
- Preface
- 1 The smallest free number
- 2 A surpassing problem
- 3 Improving on saddleback search
- 4 A selection problem
- 5 Sorting pairwise sums
- 6 Making a century
- 7 Building a tree with minimum height
- 8 Unravelling greedy algorithms
- 9 Finding celebrities
- 10 Removing duplicates
- 11 Not the maximum segment sum
- 12 Ranking suffixes
- 13 The Burrows–Wheeler transform
- 14 The last tail
- 15 All the common prefixes
- 16 The Boyer–Moore algorithm
- 17 The Knuth–Morris–Pratt algorithm
- 18 Planning solves the Rush Hour problem
- 19 A simple Sudoku solver
- 20 The Countdown problem
- 21 Hylomorphisms and nexuses
- 22 Three ways of computing determinants
- 23 Inside the convex hull
- 24 Rational arithmetic coding
- 25 Integer arithmetic coding
- 26 The Schorr–Waite algorithm
- 27 Orderly insertion
- 28 Loopless functional algorithms
- 29 The Johnson–Trotter algorithm
- 30 Spider spinning for dummies
- Index
29 - The Johnson–Trotter algorithm
Published online by Cambridge University Press: 05 March 2013
- Frontmatter
- Contents
- Preface
- 1 The smallest free number
- 2 A surpassing problem
- 3 Improving on saddleback search
- 4 A selection problem
- 5 Sorting pairwise sums
- 6 Making a century
- 7 Building a tree with minimum height
- 8 Unravelling greedy algorithms
- 9 Finding celebrities
- 10 Removing duplicates
- 11 Not the maximum segment sum
- 12 Ranking suffixes
- 13 The Burrows–Wheeler transform
- 14 The last tail
- 15 All the common prefixes
- 16 The Boyer–Moore algorithm
- 17 The Knuth–Morris–Pratt algorithm
- 18 Planning solves the Rush Hour problem
- 19 A simple Sudoku solver
- 20 The Countdown problem
- 21 Hylomorphisms and nexuses
- 22 Three ways of computing determinants
- 23 Inside the convex hull
- 24 Rational arithmetic coding
- 25 Integer arithmetic coding
- 26 The Schorr–Waite algorithm
- 27 Orderly insertion
- 28 Loopless functional algorithms
- 29 The Johnson–Trotter algorithm
- 30 Spider spinning for dummies
- Index
Summary
Introduction
The Johnson–Trotter algorithm is a method for producing all the permutations of a given list in such a way that the transition from one permutation to the next is accomplished by a single transposition of adjacent elements. In this pearl we calculate a loopless version of the algorithm. The main idea is to make use of one of the loopless programs for the generalised boustrophedon product boxall developed in the previous pearl.
A recursive formulation
In the Johnson–Trotter permutation algorithm the transitions for a list of length n of length greater than one are defined recursively in terms of the transitions for a list of length n−1. Label the elements of the list with positions 0 through n−1 and let the list itself be denoted by xs [x]. Begin with a downward run [n−1, n−2,…, 1], where transition i means “interchange the element at position i with the element at position i−1”. The effect is to move x from the last position to the first, resulting in the final permutation [x] xs. For example, the transitions [3, 2, 1] applied to the string “abcd” result in the three permutations “abdc”, “adbc” and “dabc”. Next, suppose the transitions generating the permutations of xs are [j1, j2,…]. Apply the transition j1+1 to the current permutation [x] xs. We have to increase j1 by one because xs is now one position to the right of the “runner” x.
- Type
- Chapter
- Information
- Pearls of Functional Algorithm Design , pp. 251 - 257Publisher: Cambridge University PressPrint publication year: 2010