Power Set - Generate All Sets from a Certain Set

  • 9


"a Power Set is the set of all sets of a certain set S"


what the hell does this means :D :D

let's see an example ,

for the set of numbers S = { 0, 1 , 2 , 3 , 4 , 5 }

isn't s_01 = { 0, 1, 2, 3 } is a subset of set S ?
and s_02 = { 2, 4, 5 } is a subset of set S ?
and s_03 = { 0 } is a subset of set S ?

we also define two special subsets those are:-

set s_00 = { } which is a null set or empty set.
and s_S = { 0, 1 , 2, 3, 4, 5 } that is the full set itself to be subsets of the original set.

the power set is defined to be the set of all those subsets.
that can be defined for the set S = { 7, 8, 9 }

P( S ) = { {}, {7}, {8}, {9}, {7,8}, {8,9}, {7,9}, {7, 8, 9} }

when it comes to intuition, we can generate all possible sets for a small number of S like 3 elements or possibly four.
but when it comes to large numbers (relatively) like 16 elements the no of subsets is 2^16 = 65536.
in this case : of course it will be impossible to generate all sets by direct intuition, instead we need a method to map this method, or more particularly an algorithm to compute for us all possible subsets.

let's see the following approach ;
we would think of the Set S = { 1 , 2 , 3 , 4 } as an example

may be some samples subsets will be the following

set 1 :
- we will take the 1
- we will not take the 2
- we will take the 3
- we will take the 4

doesn't this interpretation yields the set { 1, 3, 4 } ?

set 2 :
- we will not take the 1
- we will take the 2
- we will not take the 3
- we will not take the 4

and this yields the set { 2 }.

another way to represent this interpretation by the following string { 0, 1, 0, 0 }
- where a 0 represents that we will not take the indexed element
- and a 1 represent that we will take it in our consideration.

well here comes more clarification for the 2^n line we didn't get at the beginning.
if we are going to represent every set as a string where every single item is marked as taken or not.
so the no of ways we can represent this string is (2)*(2) *.....(2)*(2) n times = 2^n.
so all we have to now is to represent all numbers from 0 to 2^n -1 in binary representation
and then mask this representation into our set to decide weather to take a specific element or not :).

so we begin with a for loop that loops from 0 to 2^n -1






and through the loop we mask the current number in binary representation and check weather each element in this index exists or not.

a sample code is available here with some modifications.

and this is the puzzle of today ;) ;) whoever can break this encrypted code will have a prize ;) ;) and that itself is a surprise :)
and i am talking seriously :)



this algorithm runs in O( n *(2 ^ n) ) and that's all i can get so far .
any body has a faster algorithm ??
however, i beleive the ( 2 ^ n ) factor cannot be reduced because it is the no of subsets so it can be reduced than that to generate all possible case.
so anybody can generate something less than the ( n ) factor ?

9 comments :

  1. I think yes there is a way.I can call u to discuss it with u easier but I will just clarify its outlines.
    1) if we have a bit mask of just one (1)like 0100 we can get it's set in O(1).

    2) there is a way to get the first occurring one from the right in a mask suppose number is x then it's let's call it m = --> x & !(x-1).

    3) if we make k = x - m we get the mask without the rightmost one which will be previously computed and saved so the result of x now will be the set of m concatenated with the element list of the one removed.

    4) Example : {1,2,3,4}

    x = 0110
    x-1 = 0101
    r = !(x-1) = 1010
    m = x & r = 0010
    list of m = 3
    k = x - m = 0100
    list of x = list of(k) ++ list(m)
    = {2,3}
    I think it can reduce it to 2^n only there may be any mistake just check it :):)

    ReplyDelete
  2. aah got it ya 3amoor ;) thanks alot :) i will try to implement it and tell u of course :)
    thanks alot dear :)

    ReplyDelete
  3. Nice using of bitwise operation :)
    I didn't get what omar said ?!!

    ReplyDelete
  4. What I said that we will use a commutative way to calculate the sets in O(1) .Just the idea that we can get the elemental set of in O(1) (the elemental set is the mask containing just one 1)that's all so as I described before I will simplify every mask to just a concatenation of 2 previously calculated sets .That's All plz tell me if you don't understand to clarify more:)

    ReplyDelete
  5. Thanks a lot Omar, I got the idea (Really Nice Idea ^_^) I have implemented it , you can find the code here : http://snipt.org/Xgon
    I have used the log to get the the position of one , I don't know if there is a better way to consider.
    Still it is O(2^n).

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. Thanks 3aAmor again ;)
    yea it is perfectly clear now :)

    ReplyDelete