The dictionary sequence method to generate the full permutation algorithm diagram

Source: Internet
Author: User
Tags number strings

Algorithm definition

First look at what is called the dictionary order, as the name implies in the Order of the dictionary (A-Z, 1-9). Based on the dictionary order, we can draw the size of any two-digit string. such as "1" < "a" < "13". Is the result of comparing each digit bit by one. For a number string, "123456789", you can know the smallest string is from small to large ordered string "123456789", and the largest string is from large to small ordered string "*987654321". In this way, for all permutations of "123456789", they are sorted, i.e. you can get an ordered set of all permutations sorted by dictionary order.

So, when we know the current arrangement, to get the next permutation, we can range the next number in the ordered collection (just bigger than him). For example, when the current arrangement is "123456879", then the next arrangement that happens to be greater than his is "123456897". When the current arrangement is the largest, it indicates that all the permutations have been exhausted.

The following algorithm can then be computed for the next permutation:

Set P is a full array of 1~n: P=P1P2......PN=P1P2......PJ-1PJPJ+1......PK-1PKPK+1......PN

1) Starting from the right end of the arrangement, find the first number of numbers that are smaller than the number on the right (j), which is calculated from the left end, i.e. j=max{i|pi<pi+1}

2) in the number on the right side of PJ, find out all the smallest number pk in the number larger than PJ, i.e. K=MAX{I|PI>PJ} (the number on the right is incremented from right to left, so K is the largest of the numbers in any number greater than PJ)

3) Swap PI,PK

4) Then reverse the pj+1......pk-1pkpk+1......pn to get the arrangement P ' =p1p2.....pj-1pjpn.....pk+1pkpk-1.....pj+1, which is the next arrangement of the arrangement p.

To prove the correctness of this algorithm, we just need to prove that the next order of the generation is exactly a sequence that is larger than the current arrangement. Figure 1.11 is an excerpt from the Lukaishing Teacher's "combinatorial math" that has 1234 of the dictionary sequence trees that generate all sorts. The path from left to right of each root to the leaves is an arrangement. Below we will use this diagram as a basis to prove the correctness of the algorithm above. algorithm Step 1,The resulting substring s = {pj+1,....., pn}, are arranged from large to small. That is, pj+1 > pj+2 > > pn, because j=max{i|pi<pi+1}. Algorithm Step 2, and got the smallest number of PK larger than PJ, from N to J number, the first one larger than J. Replace PK and PJ to ensure that the replacement number is larger than the current number. The resulting sequence is p1p2...pj-1pkpj+1...pk-1pjpk-1...pn. Notice that the PK has been replaced with PK. At this time we noticed more than P1. Pj-1pk., a set of numbers that happen to be larger than P1....PJ.....PN. In this set we pick out the smallest one of the immediate required next permutation. Algorithm Step 3, that is, the number behind the PK reversal (from large to small, into a small to large. The next arrangement that is obtained by the above 3 steps is exactly the same as the current arrangement. At the same time, we notice that when all permutations are finished, the number strings are arranged from large to small. Step 1 Gets the J = 0, the algorithm ends.

The dictionary sequence method to generate the full permutation algorithm diagram

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.