Evaluate all subsets of a set

Source: Internet
Author: User

AlgorithmIdea: an element in a set is either in a sub-set or not in a sub-set;

Each time an element is retrieved from the set, the element is placed in the sub-set, and the element continues to be retrieved until the end of the Set;

Delete the element from the sub-set and continue to get the element to know the end of the Set;

When the end of the set is reached, the elements in the subset are output.


C # implementation:

/// <Summary> /// evaluate all subsets of the Set /// </Summary> /// <Param name = "A"> set </param> /// <Param name = "I"> you have selected the nth I-1 element in the set </param> // <Param name = "N"> Number of collection elements </param> /// <Param name = "B"> temporary set, used to output a subset </param> void powerset (INT [] A, int I, int N, list <int> B) {if (I> N) // output the subset element {console. write ("{"); For (Int J = 0; j <B. count; j ++) {If (j <(B. count-1) console. write (B [J] + ","); else console. write (B [J] + "}");} If (B. count = 0) {console. write ("}");} console. writeline ();} else {int x = A [I-1]; B. add (x); // obtain powerset (A, I + 1, n, B); B. remove (x); // homes powerset (A, I + 1, n, B );}}

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.