Total permutation generation algorithm in Java _java

Source: Internet
Author: User

The whole permutation algorithm is an effective method for enumerating all possible permutations without omission for a given character set. The arrangement of any n character set can correspond to the arrangement of n numbers of 1~n one by one,
Therefore, the formation method of permutation is illustrated by the arrangement of n numbers.

There is a definite linear order relationship between the whole arrangement of n characters. All permutations have a successor except the last arrangement; there is a precursor, except for the first arrangement. The successor of each arrangement can be obtained from the least variation of its predecessors, and the fully ranked generation algorithm is to generate all the permutations from the first permutation.

The full array generation method usually has the following kinds:

Dictionary order Method
Increment Decimal method
Decrement decimal Numbering method
The method of adjacent position Exchange
Recursive classification algorithm

1. Dictionary order method is efficient and sequential nature

In the dictionary order method, for the number 1, 2, 3......N arrangement, the order of the different arrangement is from left to right to compare the corresponding number of successively to decide. For example for 5 number of permutations 12354 and 12345, arrange 12345 in front, rank 12354 in the rear. According to this rule, the first of all 5 numbers is 12345, and the last is 54321.

The dictionary order algorithm is as follows:

Set p to be a full arrangement of 1~n: P=P1P2......PN=P1P2......PJ-1PJPJ+1......PK-1PKPK+1......PN
1 starting from the right side of the arrangement, find the first number of digits that are smaller than the right number J (J (from the left side), that is, j=max{i|pi<pi+1}
2 in the number on the right of PJ, find all the smallest number PK in the number that is larger than PJ, that is, K=MAX{I|PI>PJ} (the number on the right is incremented from right to left, so K is the highest number in all numbers larger than PJ).
3) Swap PI,PK
4 The PJ+1......PK-1PKPK+1PN inverted to get the permutation P ' =p1p2.....pj-1pjpn.....pk+1pkpk-1.....pj+1, which is the next permutation of the permutation p.

For example 839647521 is an arrangement of the number 1~9. The steps to generate the next arrangement from it are as follows:
From right to left, find the first number in the permutation that is smaller than the right number 4 839647521.
Find the smallest of the 4 in the number after the number 5 839647521
Swap 5 with 4 for 839657421
Turn 7421 upside down 839651247.
So the next arrangement for 839647521 is 839651247.

2. Increment numeral method

In the increment method, the number of mediations is needed to find another arrangement from one arrangement. If you use Ki to represent the number of the p1p2...pi...pn of the element pi in the permutation in the order of Pi, then the number of mediations is the corresponding arrangement k1 ... ki ... kn-1.

For example, the number of mediations to arrange 839647521 is 72642321, 7, 2, 6 、...... The numbers 8, 3, and 9 are in the permutation, respectively 、...... The number of digits on the right side of it that is smaller.

The intermediary number is the intermediate link of the calculation arrangement. Known an arrangement, requires the next arrangement, first determine its intermediary number, a successor of the arrangement, its intermediary number is the original arrangement of intermediary number plus 1, it should be noted that if the number of intermediaries to the bottom of the kn-1+1=2, you have to move forward bit, general situation, if ki+1=n-i+1, you want to carry, This is called the incremental rounding system. For example, the number of mediations to arrange 839647521 is 72642321, then the next number of mediations is 67342221+1=67342300 (because of 1+1=2, so forward bit, 2+1=3, and rounding, so the next number of mediations is 67342300).

After the number of mediations is obtained, it can be sorted according to the corresponding restore.

The algorithm is as follows:

Number of mediations K1, K2 、......、 kn-1 The numeric order represents the number of numbers n, n-1 、......、 2 in the permutation, and therefore, to determine the position of N, K1 、......、 2 by the value of K2, kn-1 、......、 n-1 from right to left, and placed in the arrangement one at a time: I put the ki+1 bit on the right, if a digit has been placed, the position is not included, the last vacancy is 1.

So you can get an arrangement of 849617523 from 67342300, which is the last permutation of 839647521. Because 9 first placed, k1=6,9 placed in the 7th place on the right, empty out 6 vacancies, and then put 8,k2=7,8 on the right up 8th, but 9 occupy one, so 8 should be put on the right up 9th, and so on.

3. Descending Rounding Method

In the increment method, the lowest digit of the intermediary number is 2 to 1, and carries frequently, which is a disadvantage. When you flip the increment number, you get the descending number of systems.

The number of mediations in 839647521 is 67342221 (k1k2...kn-1), which is inverted to 12224376 (KN-1...K2K1), which is the number of mediations for the descending binary number: Ki (i=n-1,n-2,..., 2) is in the ki-1 position in 1. The mediation number for the next permutation of a given permutation p,p is defined as the number of mediations plus 1 for p. For example, the number of mediations for the p=839647521,p is 12224376+1=12224377 for the next permutation of 12224376,p, so that the next permutation of P is 893647521.

Given the number of mediations, the arrangement can be restored by a method similar to the increment method. However, in the descending binary number, you can find the next permutation directly from one arrangement without first calculating the number of mediations. The specific algorithm is as follows:
1 if P (i) is =n and I<>n, P (i) is exchanged with P (i-1)
2 if P (n) is =n, then find a continuous descending sequence 9, 8 、......、 I, remove it from the left side of the arrangement, add it to the right end in reverse order, and then swap the i-1 with the left digit

For example the next permutation of the p=893647521 is 983647521. For the next arrangement of 983647521, because 9 is on the leftmost and 2nd bit is 8, and 3rd is not 7, so 8 and 9 are from small to large at the right end 364752189, and then the 7 to the left with the number of 983647521 to the next row is 367452189. Another example for the next arrangement of 987635421, only need to 9876 from small to large to the right and 5 to the left of the number 3 swap, get 534216789.

4. The most efficient but not natural order of the adjacent transposition method

The next arrangement in the adjacent transposition method is always the one adjacent to each other. In the case of a 4-element arrangement, the last element 4 is exchanged successively with the preceding elements, and 4 new permutations can be generated:
1 2 3 4, 1 2 4 3, 1 4 2 3, 4 1, 2 3
The two elements at the end of the last permutation are then exchanged, and then the 4 and subsequent elements of the queue are swapped, and four new permutations are generated:
4 1 3 2, 1 4 3 2, 1 3 4 2, 1 3, 2 4
Then swap the two elements at the beginning of the last permutation and move the 4 forward from the back:
3 1 2 4, 3 1 4 2, 3 4 1 2, 4 3, 1 2
This cycle 4! the entire arrangement.

5. Element Increment method (n-method) inefficient

1 from the original arrangement p=p1p2......pn, the nth bit plus n-1, if the bit value is more than N, then divide it by N, replace the bit with the remainder, the bit (the n-1 bit plus 1)
2) The same method of processing n-1 bit, n-2 bit, ..., until no more rounding, processing a permutation will produce a new arrangement
3 Remove the permutation of the same elements
4 when the value of the first element >n ends
Take 3 numbers 1, 2, 3 for example: The original arrangement is 1 2 3, starting with the 3rd element is the 3,3+2=5,5 Mod 3=2, and the 2nd element is 2,2+1=3, so the new arrangement is 1 3 2. By the increment of the element, the sequence produces
Arrangement is: 1 2 3,1 3 2,2 1 1,2 1 3,2 2 2,2 3 1,2 3 3,3 1 2,3 2 1
There are repeating elements in the underlined arrangement, discarded, and the rest is all arranged.

6. Recursive classification algorithm

It is simple to describe the whole permutation method by recursive method, and there are many ways to implement it.

1) Backtracking method
The backtracking method is usually constructed with a spanning tree. Take 3 elements as an example; the node of the tree has a data value of 1, 2, and 3. If one is 0, the value is not yet taken.
The initial state is (0,0,0), and the 1th element value can be selected separately for 1,2,3, thus extending 3 child nodes. Using the same method to find out the possible values of the 2nd element of these nodes, and so on, once the new node of the 3 data total Non-zero, then found a full array of solutions. When you try all the possible scenarios, you get the answer to the question.

2) Recursive algorithm
If p is used to represent the arrangement of n elements, and PI means not to include the permutation of the element I, (i) pi represents the permutation of the prefix I in front of the permutation pi, then the permutation of n elements is defined as:
If N=1, then arrange p only one element I
If n>1, then the permutation p is constituted by permutation (i) Pi (i=1, 2 、....、 n-1).
By definition, it is easy to see that if an arrangement of k-1 elements has been generated, the arrangement of k elements can be generated by adding element I before the permutation of each k-1 element. For example, the arrangement of 2 elements is 1 2 and 2 1, and for the elements, P1 is 2 3 and 3 2, add 1 before each arrangement to generate 1 2 3 and 1 3-22 new permutations, p2 and P3 are 1 3, 3 1 and 1 2, 2 1, the same way you can generate new permutations 2 1 3, 2 3 1 and 3 1 2, 3 2 1.

3) Cyclic Displacement method
If you have already generated an arrangement of k-1 elements, add the element k after each arrangement to make it an arrangement of k elements, and then move each arrangement loop to the left (right), each time a new arrangement is made.
For example the arrangement of 2 elements is 1 2 and 2 1. After 1 2 plus 30% for the new arrangement 1 2 3, cycle it to the left to regenerate into new permutations 2 3 1, 3 1, and likewise 2 2 to generate new permutations 1 2 1, 3 1 3 and 2 3 2.

The meaning is the whole content of this article, I hope you can enjoy it.

Related Article

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.