# 11. Power set calculation [backtracking recursive]< >

Source: Internet
Author: User

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) 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.

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), 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]< >

Related Keywords:

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.