The proof of the whole permutation algorithm generated by the dictionary order method

Source: Internet
Author: User
Tags number strings

The introduction makes a full arrangement of a given data, which is often used in various situations. In combinatorial mathematics, there are many methods to generate the whole arrangement, and the Lukaishing teacher's "Combinatorial mathematics" introduces three kinds: Ordinal method, dictionary order method, pro-position interchange method, etc. In which the dictionary order method because of the simple algorithm, and the use of the time can be in accordance with the current state to obtain the next state, until all the arrangement is complete, convenient in the program with the use of, widely used, STL next_permutation is also the use of this method. The algorithm defines first 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. Like what"1"<" A"<" -"。 Is the result of comparing each digit bit by one. For a number string, "123456789"To know that the smallest string is an ordered string from small to large."123456789"and the largest string is the ordered string from large to small" *987654321”。 This is for the123456789all permutations, sorting them out, that 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, the current arrangement is "123456879"And then the next arrangement that happens to be bigger than him is"123456897". When the current arrangement is the largest, it indicates that all the permutations have been exhausted. Therefore, the following algorithm can be computed for the next permutation: set P is a full arrangement of 1~n: P=p1p2......pn=p1p2......pj-1pjpj+1... pk-1pkpk+1... pn1Starting from the right end of the arrangement, find the first number of numbers that are smaller than the right number (j), which is calculated from the left end, i.e. j=max{i|pi<pi+1}2In the figure on the right of PJ, find out all the smallest numbers in the number above PJ, the 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,pk4) and then the pj+1... pk-1pkpk+1.... pn Inversion Get permutation p'=p1p2.....pj-1pjpn.....pk+1pkpk-1.....pj+1, this is the next arrangement for arranging p. Prove
To prove the correctness of this algorithm, we just need to prove that the next order of the generation is exactly the same as the current permutation. 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. Algorithm implementation
  1. /** Java language Implementation
  2. * Get the next permutation based on dictionary order method
  3. *
  4. * @param cur
  5. * @return Next permutation string, or null if cur is the last
  6. */
  7. Public static string next (String cur) {
  8. String ret = null;
  9. if (cur = = null)
  10. return ret;
  11. int strlen = Cur.length ();
  12. char[] lcur = Cur.tolowercase (). ToCharArray ();
  13. Int J = strlen- 2;
  14. While (J >= 0 && lcur[j] > lcur[j + 1]) {
  15. j--;
  16. }
  17. if (J < 0)
  18. return ret;
  19. int k = strlen- 1;
  20. While (Lcur[k] < lcur[j]) {
  21. k--;
  22. }
  23. //Swap lcur[k], Lcur[j]
  24. Char temp = lcur[k];
  25. LCUR[K] = Lcur[j];
  26. LCUR[J] = temp;
  27. //Reverse lcur[j+1, ..., strlen-1]
  28. int low = j + 1;
  29. int high = strlen- 1;
  30. While (Low < high) {
  31. temp = Lcur[low];
  32. Lcur[low] = Lcur[high];
  33. Lcur[high] = temp;
  34. low++;
  35. high--;
  36. }
  37. ret = string.valueof (lcur);
  38. return ret;
  39. }
  40. Reprinted from: http://blog.csdn.net/cpfeed/article/details/7376132

The proof of the whole permutation algorithm generated by the dictionary order method

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.