Bowen link Reference: https://blog.csdn.net/a358463121/article/details/45543879
Using a graph traversal algorithm: DFS depth-first traversal
Central idea:
Let R = {r1, r2, ... , rn} is arranged to be n elements, the R- {RI} = Ri of
Perm (X) represents a permutation is applied before each full array Perm (X) of the prefix obtained ri arrangement.
(1) When n = 1, Perm (R) = ( r), where r is the set R only element;
(2) when n> 1, Perm (R) by (r1) + Perm (R1) , (r2) + Perm (R2 ), ..., (rn) + Perm (Rn) configuration.
Then the specific procedures to how to achieve it? We come to a practical example, suppose you have a number of columns 1,2,3,4
full array then 1,2,3,4
perm ({1,2,3,4}) = 1perm ({2,3,4}) + 2perm ( {1,3,4}) + 3perm ({1,2,4}) + 4perm (1,2,3)
then we just each number, and the second a number of exchange can not get the next sequence it?
such as {1,2,3,4} and a second number of the first switch, then do not get 2 {1,3,4}, the next we use a practical example illustrates how the program is run
Specific algorithm flow:
Number of columns: {1,2,3} and the first first exchange
can be a sequence {2,3} {2,3} into perm recursive function, and then
- Recursive {2,3}
sequence {2,3} and the first first exchange
to give {3} 2, 1,2,3 output (in this case low = high, because only one serial number {3} , the output list list)
number {2,3} and the first switch back to the first, the result is still a {2,3}
sequence {2,3} with a first second exchange
to give 3 {2}, 1,3,2 output
{3,2} and the first and second switching back, back to {2,3}
- {2,3} recursive complete restitution sequence {1,2,3}
Number of columns: {1,2,3} and the first second switching
can be 2, {1,3}
- recursion {1,3}
sequence {1,3} and the first first exchange
to give 1 {3}, 2,1,3 output
number sequence {1,3} and the first switch back to the first, the result is still a {1,3}
sequence {1,3} with a first second exchange
to give 3 {1}, the output 2,3,1
{3,1} and the first and second switching back, back to {1,3}
- {1,3} is complete recursive
sequence {2,1,3} the first and second switching
sequence {1,2,3} restitution
Number of columns: {1,2,3} and the third first exchange
can be 3, {1,2}
- recursion {1,2}
sequence {1,2} and the first first exchange
to give 1 {2}, 3,1,2 output
number sequence {1,2} and the first switch back to the first, the result is still a {1,2}
sequence {1,2} with a first second exchange
to give 2 {1}, the output 3,2,1
{2,1} and the first and second switching back, back to {1,2}
- {1,2} is complete recursive
sequence {3,1,2} the first and second switching
sequence {1,2,3} restitution
Algorithm may simply write
Perm ({l, 2,3}) = 1 perm ({2,3}) + 2perm ({l, 3}) + 3perm ({1,2})
Perm ({2,3}) 2perm = ({}. 3) + 3perm ({2})
Perm ({l, 3}) = 1 perm ({}. 3) + 3perm ({}. 1)
Perm ({1,2}) = 1 perm ({2} ) + 2perm ({1})
c ++ Code:
#include <the iostream>
the using namespace STD;
void the swap (int & A, int & B) {
int TEMP = A;
A = B;
B = TEMP;
}
void Perm (int List [], int Low, int High) {
IF ( when low == high) {// when low == high, in which case a list is arranged, the output list
for (int I = 0; I <= Low; I ++)
COUT << list [I];
COUT << endl;
} the else {
for (int I = Low; I <= High; I ++) {// each switching element to the first element of
the swap (List [I], List [Low]);
Perm (List, Low + 1, high); // after the exchange, to give the sub-sequence to obtain full permutation sequences function with Perm
the swap (List [I], List [Low]); // Finally, the switching element back, restoring, and switching the other element
}
}
}
int main ()
{
int list[]={1,2,3};
perm(list,0,2);
return 0;
}
Program results:
123
132
213
231
321
312