Non-recursive algorithm with full permutation

Source: Internet
Author: User

1. Full-array definitions and formulas:

Select the number of M (m<=n) from the n number to be a column in a certain order, called a permutation of m elements from n elements. By the definition of an arrangement, obviously different order is a different arrangement. The number of all permutations of M elements from n elements, called permutations. A permutation of n elements is taken from n elements, called a full permutation. The permutation number formula is n!, which can be obtained by multiplication principle.

2. Complexity of Time:

The total number of n (characters, objects) is n!, so the full permutation algorithm at least time O(n!) Of If you want to output the entire arrangement, then the output time is o(n? N!), because each permutation has n data. So in fact, the full permutation algorithm is not able to deal with large data, and generally does not require us to traverse the full array of large data.

3. List the initial ideas in full alignment:

To solve an algorithmic problem, I'm more accustomed to starting with basic ideas, and we'll look at how we're going to write a whole array of numbers: 1,3,5,9 (for convenience, I'll use the numbers to make all the permutations instead of the characters).
"1,3,5,9" (the first one)
First, keep the first one unchanged and the "3,5,9" in full alignment.
In the same way, we will keep 3 unchanged and arrange the "5,9" in full.
Keep 5 unchanged, the 9 pairs are all arranged, because 9 has only one, it is arranged only one: 9.
So the arrangement is "1,3,5,9"
The next 5 can not begin with 5, 5,9 exchange, get
"1,3,9,5"
At this time 5,9 the situation is finished, can not begin with 3, get
1,5,3,9
1,5,9,3
1,9,3,5
1,9,5,3
In this way, we get all the permutations beginning with 1, which is the process of generating our general permutation numbers. Then it starts with 3, 5, 9 and gets the whole arrangement.

We are now making a hypothesis that assuming that the first bits in a given sequence are not the same, then it is obvious that the sequence must not be the same sequence. With this conclusion, we can get the same thing. If the first bit is the same, but the second is different, then it must not be the same sequence in these sequences.
Well, the problem can be seen in this way. Right
T=" x1, X2,X3,X4,X5,....x n?1,x n "
After we have obtained all the cases in the first position (Note: Is all the case), for each case, the extraction sequenceT, then for the rest of the sequence it can be seen as a whole new sequence.
T1=【X2,X3,X4,X5,....x n?1,x n "
SequenceT1 Can be thought to be unrelated to the previous sequence. In the same way, we canT1 Proceed withTThe same operation, untilTOnly one element in the. So we get all the possibilities. So obviously, this is a recursive algorithm.
All of the first bits: nothing more than ax1 With the back of all the numbersx2,x3,.. . . . . . xn Swap once in turn

As follows:

4. Fully aligned non-de-redo recursive algorithm

Algorithm ideas: The whole arrangement can be regarded as a fixed pre-I position, after the first i+1 bit after the whole arrangement, such as fixed the number one, followed by the n-1 bit of the full arrangement. So the whole arrangement of solving the n-1 bit elements can solve the whole arrangement of n-bit elements, so the design can easily be implemented by recursion.

"Attached code snippet:"

1#include <iostream>2 using namespacestd;3 intarr[5]={0,1,2,3,4};4 voidSwapintXintY//two numbers for exchanging arrays5 {6     inttemp=Arr[x];7arr[x]=Arr[y];8arr[y]=temp;9 }Ten intResove (intN//Recursive Functions One { A         if(n==5)//when attempting to recursively return an array element that does not exist, it indicates that all the numbers have been sorted, and the output is complete.  -         { -              for(intI=0;i<5; i++) thecout<<Arr[i]; -cout<<Endl; -             return 0; -         } +          for(inti=n;i<5; i++)//loops for swapping and after-all permutations -{//I is starting from N I=n;swap (n,i) equivalent to the fixed current position, in the next position of the arrangement.  + swap (n,i); AResove (n+1); at swap (n,i); -         } -  - } - intMain () - { inResove (0); -}

Arranging templates

1 voidPermutation1 (Char* STR,intSbegin,intSend//a fully arranged non-de-redo recursive algorithm2     {  3         if(Sbegin = = send)//output when sbegin = Send4         {  5              for(inti =0; I <= send; i++)//output an arrangement6cout <<Str[i]; 7cout <<Endl; 8         }  9         Else  Ten         {   One              for(inti = Sbegin; I <= send; i++)//loop-through swap and Sbegin + 1 full array A             {   -Swap (Str[i],str[sbegin]);//exchange of subsections I and Sbegin -Permutation1 (Str,sbegin +1, send);  theSwap (Str[i],str[sbegin]);//"Note 1" Exchange back -             }   -         }   -}

"Note 1" swap (Str[i],str[sbegin])//Exchange back
Let's take a closer look at the code in the loop body, and when we swap the sequence, we drop the first element into the next recursive recursion, and the next loop is completed. This is the work of a loop program, and here's the problem: The sequence is changed when we go into the next loop. However, if we are going to assume all the possibilities of the first bit, then it must be in the case that the initial state of these sequences is consistent, so after each exchange, restore to ensure that the initial state is consistent.

Non-recursive algorithm with full permutation

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.