Find the powerest of a set recursively

1k Views Asked by At

I am trying to find the powerset of a set recursively and then print the results which I am very stuck on any help would be greatly appreciated.

public static ArrayList<String> getpowerset(int a[],int n, ArrayList<String> power){
    if(n<0)
    {
        return null;
    }

    if(n==0)
    {
        if(power==null)
            power=new ArrayList<String>();
        power.add(" ");
        return power;
    }

    power=getpowerset(a, n-1, power);
    ArrayList<String> tmp=new ArrayList<String>();
    for(String s:power)
    {
        if(s.equals(" "))
            tmp.add(""+a[n-1]);
        else
            tmp.add(s+a[n-1]);
    }

    power.addAll(tmp);
    return power;
for (int i = 0; i<power.size();i++){
    System.out.println(power);
    }
1

There are 1 best solutions below

0
On

Try to think recursively:

  • base case) The powerset of an empty set is an empty set
  • rec. case) The powerset of a list such [firstElemnt] + restOfTheList is the powerset of restOfTheList plus each element of the powerset of restOfTheList chained with firstElemnt

Example:

Given a list [a,b,c]:

  • the powerset of [] -> [[]] (set with an empty set)
  • the powerset of [a] -> [[], [a]] (added a to each element of previous powerset)
  • the powerset of [a,b] -> [[], [a], [b], [a,b]] (added b to each element of previous powerset)
  • the powerset of [a,b,c] -> [[], [a], [b], [a,b], [c], [a,c], [a,b,c]] (added c to each element of previous powerset)

ALGORITHM

public static List<List<String>> powerset(final LinkedList<String> originalSet) {
    final List<List<String>> powerset = new LinkedList<List<String>>();
    //Base case: empty set
    if ((originalSet == null) || (originalSet.size() == 0)) {
        final List<String> set = new ArrayList<String>();
        //System.out.println(set);
        powerset.add(set);
    } else { 
        //Recursive case:
        final String firstElement = originalSet.removeFirst();
        final List<List<String>> prevPowerset = powerset(originalSet);
        powerset.addAll(prevPowerset);
        //Add firstElement to each of the set of the previuos powerset
        for (final List<String> prevSet : prevPowerset) {
            final List<String> newSet = new ArrayList<String>(prevSet);
            newSet.add(firstElement);
            //System.out.println(newSet);
            powerset.add(newSet);
        }
    }
    return powerset;
}

TEST

public static void main(final String[] args) {
    final LinkedList<String> originalSet = new LinkedList<String>();
    originalSet.add("a");
    originalSet.add("b");
    originalSet.add("c");
    System.out.println("The powerset of " + originalSet + " is:");
    System.out.println(powerset(originalSet));
}

OUTPUT

The powerset of [a, b, c] is:
[[], [c], [b], [c, b], [a], [c, a], [b, a], [c, b, a]]

Note that i have imported the following classes:

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;