Two methods for finding all subsets of a set

Source: Internet
Author: User
Tags bit set bitset

If we have a requirement for all subsets of the collection (including the collection itself), there is a set of s, including two elements <a,b>, and all its subsets are <a,ab,b>

It is not difficult to find the relationship between the number of subsets of SN and the number of elements N of the original set: Sn=2^n-1.

This article describes two methods of implementation:

One: Bitmap method:

1) constructs an array of the same size as a set, corresponding to an element in the collection, and the elements in array a have only two states: "1" and "0", respectively, representing whether the corresponding element of the set in the subset output is to be output. This allows the array A to be seen as a token bitmap of the original collection.

2) Array A simulates the operation of "plus one" of integers, and after each "plus", it outputs all the corresponding elements in the original set with the value "1" in the array A.

Set the original set to <a,b,c,d>. When the "plus one" state of the array A is [1,0,1,1], the subset of this output is <a,c,d>.

Detailed code such as the following:

/* Using non-recursive thinking assumes that there is an array size of n then using the N-bit binary hypothesis corresponds to a bit of 1 then the output of this bit assumes that the corresponding bit is 0 then do not output this bit *//* the idea of using bitmaps constructs a bit set size and array size, assuming that the corresponding bit in the bitmap is 1, indicating the ability to output an element in this array assuming that the corresponding bit in the bitmap is 0, the corresponding bit in the array does not output the array used by the analog bitmap here, the focus here is to simulate the array plus 1 operation *//* Array to simulate the bitmap plus 1 operation array can be added 1 until all elements in the array is 1 function return value b Ool Array initialization is the highest bit for 1*/#define Max_len 10void bitmap (char str[],const int n) {bitset<max_len> count;wang.set (); int i=0; unsigned Long value = Wang.to_ulong ();cout<<wang<< "" <<value<<endl;int count=0;while (value) { Bitset<max_len> bit (value--); for (I=0;i<bit.size (); i++) if (Bit[i]) cout<<str[i];cout<<endl; count++;} Cout<<count<<endl;}

3) Time complexity: O (n*2^n). In fact, in the process of traversing the output subset. Be able to further optimize the program.

Like what. In the first M iteration. Only need to traverse the first k elements, K=log2 (m) +1. In this way, the total traversal count is sn= (n-2) regardless of the array simulation "plus one" operation *2^n+2,n>=2; Sn=1,n=1. Although the complexity is constant, the total execution time is reduced.

4) Spatial complexity: each iteration of the method is independent and has no relation to the result of the last iteration. Therefore, the memory can be reused after each output subset.

Only an array of the same size as the original collection is required. The spatial complexity is O (n).

Two: Recursive iterative method:

1) recursive iterations are used. Detailed steps such as the following,

Set The original collection S=<a,b,c,d> The subset result is R:

First iteration:

R=<a>

Second iteration:

R=<a AB b>

Third iteration:

R=<a AB b ac ABC BC c>

Fourth iteration:

R=<a AB b ac ABC BC c AD ABD BD ACD ABCD BCD CD d>

Each iteration is the result of the previous iteration + each element in the last iteration result plus the element of the current iteration + elements of the current iteration.

Detailed code such as the following:

/* The method cannot be used with explicit recursion the following is the output of a subset of the last output is the next time the output is the subset   + the element of this iteration + the elements of this iteration/#if 1void print (char* str) {/* uses two arrays. A record of the results of the last iteration   a record of the results that need to be output this time, the   VEC record is the subset of the next iteration that needs to be tested. The back  record is a new subset */int Count=0;vector<char after the reference VEC iteration. > vec;vector<char> back;int j;for (int i=0;i<strlen (str); i++) {if (i = = 0) {vec.push_back (str[i]); vec.push_ Back (', '); Back=vec;} Else{for (J=0;j<back.size (); j + +) if (back[j] = = ', ') {Back.insert (Back.begin () +j,str[i]); j + +;} Back.push_back (Str[i]); Back.push_back (', ');} For (J=0;j<back.size (); j + +) {if (back[j] = = ', ') {printf ("\ r \ n"); count++;} elseprintf ("%c", back[j]); if (i) Vec.push_back (Back[j]);} Back=vec;} printf ("Sub_set count is%d \ r \ n", count);} #endif

2) Complexity of time

According to the above process, not difficult to find. The number of iterations for the K-iteration is: 2^k-1.

N>=k>=1. Iterate n times, the total number of traversal is: 2^ (n+1)-(2+n), n>=1.

The time complexity is O (2^n).

3) Complexity of space

Because of the algorithm. The next iteration requires the result of the previous iteration, and there is no next one after the last iteration.

So if the original collection has n elements. In the iterative process, the total number of subsets that need to be saved is 2^ (n-1) -1,n>=1.

However, it is important to note that the number of subsets is considered here, and the length of each subset element is treated as 1.

Summarize:

Recursion is very time-consuming. Because it is recursive, in the first method, using the Bitset in C + +, this method is very efficient, in the second method, the purpose of using two vectors is that a vector record the iteration needs to output the set, a vector is for this iteration need to participate in the situation of the secondary output.


Two methods for finding all subsets of a set

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.