{a,b,c,d} => [a, {b,c,d}] {b,c,d} => [b, {c,d}] {c,d} => [c, {d}] {d} => [ d] => [ cd, dc] => [bcd, cbd, cdb, bdc, dbc, dcb] => [abcd, bacd, bcad, bcda, acbd, cabd, cbad, cbda, ...dcba]
JOHNSON_TROTTER(n)
- Initialize the first permutation to 1l2l3l...nl
- while the last permutation has a mobile element do
- find the largest mobile element k
- swap k with the adjacent elements k's arrow points to
- reverse the direction of all of the elements that are larger than k
- add the new permutation to the list
1← 2← 3← -> 1← 3← 2← 1← 3← 2← -> 3← 1← 2← 3← 1← 2← -> 3→ 2← 1← 3→ 2← 1← -> 2← 3→ 1← 2← 3→ 1← -> 2← 1← 3→ 2← 1← 3→ -> DONE
LEXICOGRAPHIC_PERMUTE(n)
- initialize the first permutation with 1234...n
- while last permutation has two consecutive elements in increasing order do
- let i be the largest index such that ai < ai+1
- find the largest index j such that ai < aj
- swap(ai, aj)
- reverse the order of ai+1 to an
- add the new permutation to the list.
123 -> 132 -> 132 132 -> 231 -> 213 213 -> 231 -> 231 231 -> 321 -> 312 312 -> 321 -> 321 321 -> DONE, exit condition met