Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Generating permutations

In this post, we will discuss the problem of generating all permutations of a given set of elements. A permutation is an arrangement of elements in a specific order. The table below shows all permutations of the set {1, 2, 3}:

Permutations {1,2,3}
{1, 2, 3}
{1, 3, 2}
{2, 1, 3}
{2, 3, 1}
{3, 1, 2}
{3, 2, 1}
-----------------------------

For a set of n elements, there are n! permutations. The problem of generating all permutations of a set of elements is a common problem in computer science and has applications in various fields, such as combinatorial optimization, cryptography, and data analysis.

1. The Minimal changes algorithm

To generate all permutations, the minimal changes algorithm start from an empty set and add accordingly the elements one by one till we get the full set.

An example of generating all permutations for 3 elements is shown below:

  • start with one element:

    {1}\{1\}

    - add the second element to

    {1}\{1\}

    from right to left:

    {1,2},{2,1}\{1, 2\}, \{2, 1\}


    - add the third element to

    {1,2}\{1, 2\}

    from right to left:

    {1,2,3},{1,3,2},{3,1,2}\{1, 2, 3\}, \{1, 3, 2\}, \{3, 1, 2\}

    - add the third element to

    {2,1}\{2, 1\}

    from right to left:

    {2,1,3},{2,3,1},{3,2,1}\{2, 1, 3\}, \{2, 3, 1\}, \{3, 2, 1\}

2. The Johnson-Trotter algorithm

Instead of generating all permutations starting from an empty set, the Johnson-Trotter algorithm generates permutations by defining a set of rules that determines the next permutation from the current one.

Lets take the example of generating all permutations for 3 elements using the Johnson-Trotter algorithm:

  • start with the permutation

    {1,2,3}\{ \overleftarrow{1}, \overleftarrow{2}, \overleftarrow{3}\}

The following table summarizes the steps of the Johnson-Trotter algorithm for generating all permutations of 3 elements:

|Permutation|Mobile element|Direction change| |:----------------:|:----------------:|:------|------------------:| |

{1,2,3}\{ \overleftarrow{1}, \overleftarrow{2}, \overleftarrow{3}\}

|

33

|No change| |

{1,3,2}\{ \overleftarrow{1}, \overleftarrow{3}, \overleftarrow{2}\}

|

33

|No change| |

{3,1,2}\{ \overleftarrow{3}, \overleftarrow{1}, \overleftarrow{2}\}

|

22

|

{3}\{ \overrightarrow{3}\}

| |

{3,2,1}\{ \overrightarrow{3}, \overleftarrow{2}, \overleftarrow{1}\}

|

33

|No change| |

{2,3,1}\{ \overleftarrow{2}, \overrightarrow{3}, \overleftarrow{1}\}

|

33

|No change| |

{2,1,3}\{ \overleftarrow{2}, \overleftarrow{1}, \overrightarrow{3}\}

|No mobile|| |-|-|-|