or in accordance with the old method, the problem of the greatest degree of simplification, and now the collection of A={a,b} power set, only two elements, there should be {a,b},{a},{b},{x} four possible. If we figure out the two elements, the rest will be the same, and only the number will increase.
Now with two arrays, A is the original set and B holds a subset. The key is constraints, that is, how boundaries are defined. First of all, the first in the B set into an element, and then put an element, it can no longer be put, because a set only two elements, like the hands only two balls, all take out there is no, so here the constraint is set to, as long as the number of >= hands to take out the number, return.
, here is the subscript, so the first time to take out a, i=0, (after the same), and then put one, I=1, try to put another i=2 has touched the border, because the number has been taken out the largest. Return to this, print the result, then get a set {a, b}.
Next, there was a shift. If the second character B is retracted (equivalent to not put), although not put things, but after a step, so then get i=2, and touch the boundary, the results returned, the result is set {a}.
From these two figures can be seen, this is similar to the first sequence of the tree traversal, if it is the first sequence of the idea, the rest is easy to understand, because the order of return sequence is simple, now i=1 the situation has been exhausted, that is, the left and right sub-tree has been returned, the next step to the root node, that is, the
Similarly, because it is recursive, so the A also shed, and then traverse the root of the right sub-tree, for i=1 calculation.
voidFintICharA[],intN) { Charx; intK; if(i>=N) {if(b[0]) cout<<'{'<<B<<'}'<<Endl; Elsecout<<'x'<<Endl; } Else{x=A[i]; K=strlen (B); B[K]=x; F (I+1A3); B[K]=0; F (I+1A3); }}
The i=1 is calculated as follows:
Due to a, the equivalent of set B does not put an element, the length is 0, so when entering the function f (1), a set of the second element, that is, B put b[k], that is, b[0].
Re-enter, i=2, touches the border to return. This is just F (1) of the left subtree, and then into the right subtree, likewise, put B to go, that is, b[k]=0, (b[0]=0), into F (2), because set B is completely empty, so the last to print the empty space ' X '.
By this time, F (0) called the function, Saozi right subtree has been returned, the function ends, the order of printing is, ab,a,b,x.
This backtracking algorithm, the initial contact will be some around, do not use a lot of data, simplify it to the simplest form, recursive call step number of the demonstration, it is easy to see clearly.
The key to this question is two, first, delimit the boundary, that is, under what conditions recursive end. Second, in the middle of the recursive process, the return time need to deal with what things, such as the code in the B[k]=0, is the most critical step, that is, to remove some elements!
11. Power set calculation [backtracking recursive]< >