Fool method to find all subsets of a set problem (Java edition)

Source: Internet
Author: User

A collection of random lengths given. Represented by an array, such as {"A", "B", "C"}, to find all its subsets. The result is {{A}, {B}, {C}, {A/b}, {a,c}, {b,c}, {a,b,c}}, and an empty set.

The following is how to use a primitive fool method (non-algorithmic) to find all its subsets.

First we know that the number of subsets is 2^length, assuming the length is 3, the subset has 2 of the 3 Parties = 8, including an empty set.

For a subset, my approach is to infer, have or not, with 1 and%, whatever.

So a length of 3 like this, in binary notation is 000, 001, 010 ...

In fact, it is from the 0-2^3, with 2 binary notation is a subset of it. Then take out the 0 corresponding children. For example, 010 corresponds to b,011 is BC.

Just need to do a loop from 0 to 2^3-1. Then the number between 0-7 is expressed in binary, and then compared with the original set.

Remove 0 characters from the corresponding position so that all subsets are obtained.

The principle is very easy, the following is the code

Package huisu;/** * Created by Wolf on 2016/3/22.    */public class Getset {private string[] origin = {"A", "B", "C"};    Private string[] Targetarray;    public static void Main (string[] args) {new Getset (). Dojob (); The private void Dojob () {///Gets the string to be decomposed assuming that the maximum is a few//as the string is 3 bits. Is 2^3.

From [0 0 0] to [1 1 1] int maxLength = (int) Math.pow (2, origin.length); Targetarray = new String[maxlength]; for (int i = 0; i < targetarray.length; i++) {//decimal to 2 binary targetarray[i] = integer.tobinarystring ( i); } buling (); Print (); }/** * To fill the vacancy 0, gather the number of bits */private void buling () {for (int i = 0; i < targetarray.length; i++) { The number of digits is complete, no need to fill 0 if (targetarray[i].length () = = Origin.length) {continue; } String temp = ""; 0,1,10,11,111 for (int j = 0; J < Origin.length-targetarray[i].length (); j + +) {temp + = " 0 "; } Targetarray[i] = temp + targetarray[i]; }} private void Print () {for (int i = 0; i < targetarray.length; i++) {String s = Targetarray [i];//such as 000,001,010 for (int j = 0; J < S.length (); j + +) {Char item = S.charat (j); if (item = = ' 1 ') {System.out.print (origin[j]); }} System.out.println (); } }}

In line 23rd, turn 10 binary 0-7 into binary, after

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center ">

There is a problem here, that is, the number of digits and dissatisfaction. Like 0, 10 and so on, the future and the original array to make the corresponding inference when a little trouble, so I did a deal, the number of digits to be filled. Remains the same as the original array number of bits.

Called Buling (forgive me if I can't remember what English to use to express the 0) method. Fill the front of the number of digits 0. Then it becomes 000,001,010 ... This makes it very convenient to infer that only 1 of the number of bits is printed. Refer to the Print method.

Summary: This approach is easier to understand. can also adapt to arbitrary length of the problem of the subset.

According to this approach, another problem can be solved--01 knapsack problem (There are five items numbered a,b,c,d,e respectively. Their weights are 2,2,6,5,4, and their value is 6,3,5,4,6. Now give you a backpack with a weight of 10. How to make the items loaded in the backpack have the maximum value sum? Believe very easy to see, the above method to find out all the subset, then for the 01 knapsack problem. is based on a subset of all. Cut off all the overweight subsets first. Then to calculate the value of the remaining subset, find the maximum OK.



Fool method to find all subsets of a set problem (Java edition)

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.