To find all non-empty subsets of a collection element

Source: Internet
Author: User

An existing set of n elements to find all the subsets of the set S?

For example: The collection s contains three elements {A, B, C}, then all its subsets are: {} (empty set), {a}, {B}, {C}, {A, b}, {A, c}, {B, C} and {A, B, C}.

it first use the idea of bit operation to solve, concrete method: Use the 2 binary bit to mark whether an element in the collection is selected, 1 is selected, and 0 is not selected. For example, all subsets of the collection {A, B, c} can be represented as follows:

{A} 0 0 1

{b} 0 1 0

{C} 1 0 0

{A, B} 0 1 1

{A, C} 1 0 1

{b, c} 1 1 0

{A, B, c} 1 1 1

From the above analysis can also be seen that a set containing N elements of 2^n-1 a non-empty set, it is very easy to think of the method is to traverse all the integers 1~2^n-1, and into the binary, according to the above thinking output all subsets.

The specific implementation is as follows:

public class Subset {/** * @param args */public static void main (string[] args) {//TODO auto-generated method stublist&lt ; list<string>> result = new arraylist<list<string>> ();//Store all subsets list<string> List = new Arraylist<string> (), List.add ("1"), List.add ("2"), List.add ("3"), List.add ("5");//Gets all the subsets of the elements in the list collection, and deposited in the result set Getsubset (List,result); for (list<string> Li:result) {for (String String:li) {System.out.print ( string+ "");} System.out.println ();}} private static void Getsubset (list<string> sourceset, list<list<string>> result) {//TODO Auto-generated method Stubint n = sourceset.size ();//num element has 2^num-1 non-empty set int num = (int) Math.pow (2, N); for (int i = 1; I & Lt Num i++) {String binary = integer.tobinarystring (i); int size = Binary.length ();//system.out.println ("size" +size); for (int k = 0; K < n-size; k++) {binary = "0" +binary;} SYSTEM.OUT.PRINTLN ("binary" +binary); list<string> set = new Arraylist<string> ();//system.ouT.println (Binary.length ()); for (int index = 0; index < sourceset.size (); index++) {if (Binary.charat (index) = = ' 1 ') {Set . Add (Sourceset.get (index));}} /*for (String string:set) {System.out.print (string+ "");} System.out.println (); */result.add (set);}}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

To find all non-empty subsets of a collection element

Related Article

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.