Skip to main content Accessibility help
×
Hostname: page-component-84b7d79bbc-rnpqb Total loading time: 0 Render date: 2024-07-28T20:11:43.303Z Has data issue: false hasContentIssue false

29 - The Johnson–Trotter algorithm

Published online by Cambridge University Press:  05 March 2013

Richard Bird
Affiliation:
University of Oxford
Get access

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
Publisher: Cambridge University Press
Print publication year: 2010

Access options

Get access to the full version of this content by using one of the access options below. (Log in options will check for institutional or personal access. Content may require purchase if you do not have access.)

Save book to Kindle

To save this book to your Kindle, first ensure coreplatform@cambridge.org is added to your Approved Personal Document E-mail List under your Personal Document Settings on the Manage Your Content and Devices page of your Amazon account. Then enter the ‘name’ part of your Kindle email address below. Find out more about saving to your Kindle.

Note you can select to save to either the @free.kindle.com or @kindle.com variations. ‘@free.kindle.com’ emails are free but can only be saved to your device when it is connected to wi-fi. ‘@kindle.com’ emails can be delivered even when you are not connected to wi-fi, but note that service fees apply.

Find out more about the Kindle Personal Document Service.

Available formats
×

Save book to Dropbox

To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Dropbox.

Available formats
×

Save book to Google Drive

To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Google Drive.

Available formats
×