Exponentiation
Computing the exponentiation of a number is a classic problem in computer science. The problem is to find the value of a number raised to the power of anothe...
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.
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\}\)
- add the second element to \(\{1\}\) from right to left: \(\{1, 2\}, \{2, 1\}\)
- add the third element to \(\{1, 2\}\) from right to left: \(\{1, 2, 3\}, \{1, 3, 2\}, \{3, 1, 2\}\)
- add the third element to \(\{2, 1\}\) from right to left: \(\{2, 1, 3\}, \{2, 3, 1\}, \{3, 2, 1\}\)
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 \(\{ \overleftarrow{1}, \overleftarrow{2}, \overleftarrow{3}\}\)
- find the largest mobile element: \(3\) (Note that there is two mobile elements: \(3\) and \(2\) and we should take the largest one)
- swap \(3\) with the element it is pointing to: \(\{ \overleftarrow{1}, \overleftarrow{3}, \overleftarrow{2}\}\)
- change the direction of all elements greater than \(3\): No change since there is no element greater than \(3\)
- Repeat the process until there is no mobile element.
The following table summarizes the steps of the Johnson-Trotter algorithm for generating all permutations of 3 elements:
Permutation | Mobile element | Direction change |
---|---|---|
\(\{ \overleftarrow{1}, \overleftarrow{2}, \overleftarrow{3}\}\) | \(3\) | No change |
\(\{ \overleftarrow{1}, \overleftarrow{3}, \overleftarrow{2}\}\) | \(3\) | No change |
\(\{ \overleftarrow{3}, \overleftarrow{1}, \overleftarrow{2}\}\) | \(2\) | \(\{ \overrightarrow{3}\}\) |
\(\{ \overrightarrow{3}, \overleftarrow{2}, \overleftarrow{1}\}\) | \(3\) | No change |
\(\{ \overleftarrow{2}, \overrightarrow{3}, \overleftarrow{1}\}\) | \(3\) | No change |
\(\{ \overleftarrow{2}, \overleftarrow{1}, \overrightarrow{3}\}\) | No mobile |
Computing the exponentiation of a number is a classic problem in computer science. The problem is to find the value of a number raised to the power of anothe...
The maximum subsequence sum algorithm is a classic problem in computer science. The problem is to find the maximum sum of a contiguous subsequence in an arra...
In this post, I will explain a very indispensable tool for developers. Docker is a tool that allows developers to build, deploy, and run applications using c...
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 specifi...
The convex hull problem is a problem in computational geometry. It is about finding the smallest convex polygon that contains a given set of points. The conv...
Project 1: Project management system The aim of this project is to implement a program (java or python) that manages a list of tasks in a project. The pr...
Use the form link to choose a project :