Three algorithms for enumerating all subsets-"Getting Started with algorithms"

Source: Internet
Author: User

Method One: Incremental construction method

Understanding recursion has to understand what a function does.

#include <cstdio>voidPrint_subset (intNint*a,intCur//the function of Print_subset is to print a subset of the CUR elements of the collection {0,1,..., n-1}, and the collection to be printed is already stored in the A array{printf ("{ ");  for(intI=0; i<cur;i++) printf ("%d", A[i]); printf ("}\n"); intmi=cur?a[cur-1]+1:0;//avoid duplicate printing with dictionary order     for(inti=mi;i<n;i++)//enumerate what elements the next one can add{A[cur]=i; Print_subset (N,a,cur+1);//recursively print a subset of cur+1 elements    }}intMain () {intN; inta[Ten]={}; scanf ("%d",&N); Print_subset (N,a,0);//initially a subset of 0 elements is stored in a, starting to print recursively    return 0;}

Method Two: Bit vector method

Enumeration of each bit or not, the complexity is slightly higher than the method, but better understood, because the output is the same as the whole line of thinking, full n-bit output.

#include <cstdio>inta[Ten];voidPrint_subset (intNint*b,intCur//determine cur or not selected{    if(cur==n)//if you determine the N-bit, print the result{printf ("{ ");  for(intI=0; i<n;i++)            if(B[i]) printf ("%d", A[i]); printf ("}\n"); }     Else{B[cur]=1;//This is an optionalPrint_subset (n,b,cur+1);//recursive next oneb[cur]=0;//This one does not choosePrint_subset (n,b,cur+1);//recursive next one    }}intMain () {intN; intb[Ten]={}; scanf ("%d",&N);  for(intI=0; i<n;i++) a[i]=i; Print_subset (N,b,0); return 0;}

The disadvantage is that the output is not in dictionary order.

Method 3:2 Binary method

A little thought will find that the two methods are in fact corresponding to the binary system.

#include <cstdio>inta[Ten];voidPrint_subset (intNintb//N-bit collection, B-state printing{printf ("{ ");  for(intI=0; i<n;i++)        if(b& (1<<i)) printf ("%d", A[i]); printf ("}\n");}voidp_s (intN) {     for(intI=0;i< (1<<n); i++) Print_subset (n,i);//Enumerate all States}intMain () {intN; scanf ("%d",&N);  for(intI=0; i<n;i++) a[i]=i;    p_s (n); return 0;}

The advantage of this method is that the code is simple.

Note: The output order of the above three methods is different.

Three algorithms for enumerating all subsets-"Getting Started with algorithms"

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.