C + + output full permutation recursive algorithm detailed explanation

Source: Internet
Author: User

Central:
Set R={R1,R2,..., rn} is the n element to be arranged, Ri=r-{ri}.
Perm (x) represents the arrangement of the prefix ri, preceded by each permutation of the fully arranged Perm (x).
(1) When N=1, Perm (r) = (R), where r is the only element in set R;
(2) When n>1, Perm (R) can be composed of (R1) +perm (R1), (R2) +perm (R2),..., (RN) +perm (RN).

So how do we do it? Let's take a practical example, assuming there's a sequence of 1,2,3,4
So the 1,2,3,4 of the whole arrangement
Perm ({1,2,3,4}) =1perm ({2,3,4}) +2perm ({1,3,4}) +3perm ({1,2,4}) +4perm (+/-)
So we just have to exchange each number, and the first number is not to get the next sequence?
For example {1,2,3,4} The first and second number exchange, then do not get 2 {1,3,4}, then we use a practical example of how the program is running

Specific algorithm flow:

Sequence: {x-man} first swap with first
You can get 1 {2,3} to put the sequence {2,3} into the perm function recursively, and then

--Recursive {2,3}
Sequence {2,3} First interchange with first
Get 2{3}, Output Low=high (at this point, because the sequence {3} has only one digit, so the output list)
The sequence {2,3} is first swapped back with the first, and the result is still {2,3}
Sequence {2,3} First and second swap
Get 3{2}, Output 1,3,2
{3,2} again first swap back with second, change back to {2,3}
-–{2,3} recursive complete sequence restitution

Sequence: {x-man} First and second swap
Can get 2,{1,3}
--Recursive {1,3}
Sequence {1,3} First interchange with first
Get 1{3}, Output 2,1,3
The sequence {1,3} is first swapped back with the first, and the result is still {1,3}
Sequence {1,3} First and second swap
Get 3{1}, Output 2,3,1
{3,1} again first swap back with second, change back to {1,3}
-–{1,3} recursion complete
Sequence {2,1,3} First and second interchange
Sequence restitution {.

Series: {X-man} First and third Exchange
Can get 3,{1,2}
--Recursive {+}
Sequence {x} First and first swap
Get 1{2}, Output 3,1,2
The first exchange returned with the first one, and the result is still {
Sequence {x} First and second swap
Get 2{1}, Output 3,2,1
{2,1} again first with the second exchange back, change back to {
-–{1,2} recursion complete
Sequence {3,1,2} First and second interchange
Sequence restitution {.

The algorithm can be written in simple
Perm ({=1perm}) ({2,3}) +2perm ({1,3}) +3perm ({.})
Perm ({2,3}) =2perm ({3}) +3perm ({2})
Perm ({1,3}) =1perm ({3}) +3perm ({1})
Perm ({)}) =1perm ({2}) +2perm ({1})

C + + code:

#include <iostream>using namespace STD;voidSwapint&a,int&AMP;B) {intTemp=a;    A=b; B=temp;}voidPermint List[],intLowintHigh) {if(Low==high) {//When Low==high, the list is one of the permutations, the output list         for(intI=0; i<=low;i++)cout<<List[i];cout<<endl; }Else{ for(inti=low;i<=high;i++) {//Each element is exchanged with the first elementSwapList[I],List[Low]); PermList, low+1, high);//Exchange, get sub-sequence, using function perm to get the whole sequence of sub-sequencesSwapList[I],List[Low]);///Finally, swap elements back, restore, and then swap another element}    }}intMain () {int List[]={1,2,3};p ERM (List,0,2);return 0;}

Program results:

123132213231321312

C + + output full permutation recursive algorithm detailed explanation

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.