# Recursive and Non-Recursive powersets?

• 04-20-2011, 02:06 AM
csisdifficult
Recursive and Non-Recursive powersets?
Hello all,

I need some help creating codes to make a powerset...

Here is the skeleton:

public static StringList powerRecursive (String s) {

}

public static StringList powerLooping (String s) {

}

Thanks!
• 04-20-2011, 02:14 AM
ozzyman
If S is the set {x, y, z}, then the subsets of S are:

{} (also denoted , the empty set)
{x}
{y}
{z}
{x, y}
{x, z}
{y, z}
{x, y, z}

Is this what you are trying to do?
• 04-20-2011, 02:16 AM
csisdifficult
Yup!
Basically, but all in one string:

["", a, b, ab, ba, abab, baba]
• 04-20-2011, 02:34 AM
csisdifficult
• 04-20-2011, 02:40 AM
ozzyman
In one List, of course.

Something to start you off:

Code:

```//define a List because you don't know how many items you need to store in it List<String> powerSet = new ArrayList<>(); //define set to make powerSet from char[] aSet = new char[] {'a', 'b', 'c'}; //loop through each character     //define a String to store full set     String fullSet = "";     //add blank to List     powerSet.add(fullSet); for (int i=0; i<aSet.length; i++) {     //add each item in set     powerSet.add(aSet[i]);     //compile the full set     fullSet += aSet[i]; }     //add full set     powerSet.add(fullSet);```

So far this would get us everything except the 'ab', 'ac' and 'bc'.

ab, ac, bc... is like this: aa, ab, ac, ba, bb, bc, ca, cb, cc
but with same letter combos removed (aa, bb, cc)
and doubles removed (ab = ba, ac = ca, bc = cb)
• 04-20-2011, 02:45 AM
csisdifficult
That sort of helps, I think I got it. Thanks!