Subset output of a set (bitwise operation mode)

Source: Internet
Author: User

Question: How do you find all the subsets of a collection, and how do you find all the subsets of the specified number of elements in a collection?

Idea: Mark all elements in the collection, 0 means unchecked, and 1 is selected. If there is a set of 3 elements of {000}, then the 001 indicates that one is not selected, the first element in the selected array 1,010 means that the 2nd element in the selected array 2,011 means that the selected array of 1th, 2 elements is .... As a result, all subsets of the collection {001} (ignoring the empty set) can be represented as encodings such as 111. In this way, we know the number of all subsets of the set, that is, 2^3=8. So, if we need to output all of the subsets, we just want to represent each subset in a binary encoding of 001, and then output the selected elements according to this encoding. The decimal 1->7 can be represented as a binary 001->111 encoding, so that all subsets of the collection are simple, and all subsets are: 0-> (2^ elements-1) is the choice of the set element corresponding to the binary encoding!

Realize:

 PackageCom.hdwang;Importjava.util.ArrayList;Importjava.util.Arrays;Importjava.util.List;/*** Created by Hdwang on 2016-12-29. * Outputs all subsets in the collection (except the empty set), or outputs a subset of the specified number of elements * idea: Mark each element (choose 1 not 0), exhaustive subset, exhaustive can be filtered*/ Public classCombine2 {/*** Collection*/    Private intData[] = {1, 2, 3, 4, 5, 6, 7, 8}; Combine2 (intsize) {        //Initializing a collection         This. data =New int[size];  for(inti=0;i<size;i++){             This. data[i] = i+1; } System.out.println ("Collection is:" + arrays.tostring ( This. Data)); }    /*** Combined subset*/List<String> Subcollistch =NewArraylist<string>(); /*** All subsets of the output collection (empty set ignored)*/     Public voidGenerateallsubcol () {//256 indicates the output of 8 elements         for(inti = 1; I < 256; i++)//2^8 ={System.out.print (i+ " : "); intMark = 1;  for(intj = 0; J < 8; J + +)            {                intIndex = mark & i;//Mark and I get every bit of I after                if(0! = index)//the position is 1, the state is selected, and the corresponding element is output{System.out.print (Data[j]+ " "); } Mark<<= 1;//mark in the inner loop, each time the value is 1,2,4,8,16,32,64,128} System.out.println (); }    }    /*** Outputs a subset of the specified number of elements *@paramsubsize Subset Size*/     Public voidGeneratesubcol (intsubsize) {System.out.println ("Subset size is:" +subsize); intElcount = Data.length;//Number of elements        intAllsubcolcount = (int) Math.pow (2,elcount);//number of subsets (empty set not counted)         for(inti=1;i<allsubcolcount;i++) {//I represents the output rule for a subset            intMark = 1;//used to remove each bit of IList<integer> Subcol =NewArrayList ();//Storage subset Elements             for(intj=0; j<elcount; J + +){                intIndex = mark & i;//Take out the 1th bit of I, 2nd place ...                 if(0! =index)                {Subcol.add (data[j]); } Mark<<= 1;//Mark left 1 bits to get the number of I high one digit            }            if(subcol.size () = = subsize) {//number of subsets meets requirementsSubcollistch.add (listtostring (Subcol, ', ')); }} System.out.println ("\ n Print Combination collection: size is" + This. Subcollistch.size ());  for(inti=0;i< This. Subcollistch.size (); i++) {String line=Subcollistch.get (i);        System.out.println (line); }    }    /*** List Conversion String *@paramList List *@paramSeparator Separator *@returnstring*/     PublicString listtostring (List list,Charseparator) {StringBuilder SB=NewStringBuilder ();  for(inti = 0; I < list.size (); i++) {sb.append (List.get (i)). append (separator); }        returnSb.tostring (). substring (0,sb.tostring (). Length ()-1); }}

Function call

New Combine2 (3); Combine2.generatesubcol (2);

Output results

The collection is: [1, 2, 3]
Subset size is: 2

Print Combination collection: size is 3
The
1,3
2,3





Subset output of a set (bitwise operation mode)

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.