Steinhaus-johnson-trotter generating a full permutation algorithm

Source: Internet
Author: User

The steinhaus-johnson-trotter algorithm is an all-permutation generation algorithm based on the minimum transform, and for permutation A[1...N], the algorithm is exchanged by a[i], a[i-1] (or a[i+1]) to generate the next permutation, Until all permutations have been generated, so that the current arrangement and its successor is only two adjacent positions of the elements have been swapped. of course, in order to prevent the recurrence of an arrangement, the algorithm does not arbitrarily swap the position between a two elements, and its generation of the full array of specific rules are as follows.
    • First, starting with the smallest sequence of dictionaries and assigning a moving direction to each element of the arrangement, the initial movement direction of all elements is left.
    • Finds such an element in an arrangement that moves in its corresponding direction, moves to a legal position, and moves in a direction that is smaller than the element, and finds the largest of the elements that satisfy the condition.
    • Swaps the element with the element that corresponds to its direction of movement.
    • For permutations, all element values are larger than the element's elements, reversing their direction of movement.

Here are a few concepts to illustrate that the so-called legal position is that the element moves in its moving direction and does not move beyond the permutation array, for example <4,<1,<2,<3, at this point, for element 4, if you continue to move to the left, the array range is exceeded, so the next moving position of 4 is an illegal position. Moreover, all elements can only move in the direction of the element smaller than themselves, as in the example above, element 2, 3, and element 1 is not able to move to the position of element 4. Each move, all the elements that can be moved to the largest of the operation, the above example, the element 1,4 can not move, 2,3 have a legitimate mobile scheme, this time need to move 3, and not move 2. After the legal move, you need to invert all elements that are larger than the moving element, the result of the move of element 3 in the previous example is 4>,1<,<3,<2, and you can see that the moving direction of element 4 has changed. Again such an example <2,<1,3>,4> for the element 2, 4, the corresponding next moving position is an illegal position, and for element 1, 3, its next moving position of the element is larger than they are, for the arrangement can not find a movable scheme, This shows that the algorithm has reached the end state, and the full array generation is finished. The following is the code for the algorithm

inline int sjtnext (unsigned int* index, size_t array_size, int* move) {unsigned int i, J, t;// Find the maximum legally moving element index for (i = array_size-1, j = array_size; I! = Uint_max; i) {if (i + Move[i] < array_size && Index[i ] > Index[i + move[i]] {if (j = = array_size) {j = i;continue;} if (Index[i] > Index[j]) {j = i;}}} No legal mobile policy found if (j = = Array_size) {return 1;} t = index[j];//the element to exchange position I = j + move[j];//The position of swap (index, I, J), Swap (move, I, j);//Reverses the direction of movement of all elements larger than t for (i = 0; i < arr Ay_size; ++i) {if (Index[i] > t) {move[i] =-move[i];}} return 0;} /* * Steinhaus–johnson–trotter algorithm based on minimum transform */void fullarray (char* array, size_t array_size) {unsigned int index[array_size] ; int move[array_size];for (unsigned int i = 0; i < array_size; ++i) {index[i] = i;move[i] =-1;} Arrayprint (array, array_size, index), while (! Sjtnext (index, array_size, move)) {arrayprint (array, array_size, index);}} 

The code uses a moving direction of the corresponding position element in the accompanying array move tag, and the corresponding element in the move array moves as the element moves. Starting with the initial arrangement of <1,<2,<3,<4, the algorithm can generate all permutations of the 4 elements until the final arrangement is <2,<1,3>,4>, and its state transitions as shown:


Not to be continued ....


Steinhaus-johnson-trotter generating a full permutation algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.