简体   繁体   中英

How to make subset or powerSet quickly and efficiently in java from a list?

I'm going through a problem of making subsets or powersets from an arrayList with may be 70 or 80 or more String elements and then further process those subsets(or powersets). For example I have some arrayLists which have non duplicate strings as element like the following (strings actually have more than one characters):

List<String> list = new ArrayList<>();
list.add("a");
list.add("c");
list.add("g");
list.add("n");
list.add("f");
and so on...

Then I need to get the subsets(or powersets) contains the 1st element. for example: {a,g}, {a,b}, {a,n,f}, {a,g,n,f}....

I've done the job by getting the powersets for the list elements except the first one and then adding the fist element with those subsets. But this works for few elements like 7 or 8 but could not work if the list grows. Is it required to do a powerSet algorithm like I'm doing for this job? If not then how can I get the expected results. And there have been a matter of memory-leak I think. As I need to store those results for some further processing.

I've done the job by following some of the suggestions regarding to the following links:

Obtaining a powerset of a set in Java
Calculating all of the subsets of a set of numbers
how to make all possible power set(or subset) from arrayList objects?

But those takes many hours for large list and could not get results at last because my netbeans IDE get hanged!

I need some suggestions or sample code related to this scenario.

Thanks!

Since the cardinality of a power set is 2^n where n is the number of elements in the beginning set I'd say that you are not going to compute a set of 2^70 elements.

My suggestion is to build elements of the power set on the fly and process them as you go, so you don't need to store all of them anywere.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM