Power calculation algorithm

I just discovered a power-up search algorithm. I searched google after solutions, but did not find any that worked well, so I figured it out myself. But I wonder what algorithm it is, because I can not find it on the network or in any books. I mean, does he have a name? Compared to the algorithms that I found on some sites for calculating power gains, I think mine is much better and wonders why no one is using it?

This is the algorithm:

R <- []
L <- [ e1, e2 ... en ]
c <- 0
function: powerSet(L, c)
  R <- R union L
  for e in L starting at c
    powerSet(L\{e}, c)
  end
  return R
end

And here it is implemented in Java:

public static void powerSet(List<String> list, int count)
{
  result.add(list);

  for(int i = count; i < list.size(); i++)
  {
    List<String> temp = new ArrayList<String>(list);
    temp.remove(i);

    powerSet(temp, i);
  }
}
+3
source share
3 answers

Mainly for two reasons:

  • It uses global variables ;
  • , , O(2^n).
+3

Poweret Power Set. ( Java). , .

+3
public final static Set<Set<Character>> powerSet(Set<Character> s){
    Set<Set<Character>> result = new HashSet<Set<Character>>();
    result.add(s);
    for (Character c:s){
        Set<Character> subSet = new HashSet<Character>(s);
        subSet.remove(c);
        result.addAll(powerSet(subSet));
    }
    return result;
}
0
source

Source: https://habr.com/ru/post/1717670/


All Articles