| Author |
Powerset algorithm
|
Tyler Long
Greenhorn
Joined: Mar 02, 2007
Posts: 4
|
|
Hi. I need help writing a program that will print the powerset of a set/array. I basically just need the idea/algorithm of how to print it. If I knew where to start I'm certain I could code it. Could someone please point me in the right direction?
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24040
|
|
Hi, Welcome to JavaRanch! Let's say there are N elements in the set. The powerset of a set contains 2^N elements, so a 32-element set gives rise to a 4-billion element powerset. Now imagine an N-bit integer. Let each bit in the number represent one specific element e in the original set. There is one element E in the powerset for each value this integer can take. For each value of the integer, the 1-bits correspond to the elements e of the original set that belong to this powerset element E, and the 0-bits corresponds to elements not in E. So to print the powerset, you use either a real integer or long, or an array of booleans or any other representation you like of an integer. Then you "count", one value at a time, and for each value of the integer, you compute the corresponding element E of the powerset and print it.
|
[Jess in Action][AskingGoodQuestions]
|
 |
Tyler Long
Greenhorn
Joined: Mar 02, 2007
Posts: 4
|
|
Originally posted by Ernest Friedman-Hill: Hi, Welcome to JavaRanch! Let's say there are N elements in the set. The powerset of a set contains 2^N elements, so a 32-element set gives rise to a 4-billion element powerset. Now imagine an N-bit integer. Let each bit in the number represent one specific element e in the original set. There is one element E in the powerset for each value this integer can take. For each value of the integer, the 1-bits correspond to the elements e of the original set that belong to this powerset element E, and the 0-bits corresponds to elements not in E. So to print the powerset, you use either a real integer or long, or an array of booleans or any other representation you like of an integer. Then you "count", one value at a time, and for each value of the integer, you compute the corresponding element E of the powerset and print it.
I do not understand this. Thanks for the help anyway.
|
 |
David McCombs
Ranch Hand
Joined: Oct 17, 2006
Posts: 212
|
|
|
Do you understand what a power set is?
|
"Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration."- Stan Kelly-Bootle
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9939
|
|
what Ernest is saying is that you can use the bits in an integer (or long) as "flags" for whether or not to include an element. say you have 3 elements. 2 ^ 3 = 8. so, you need to count from 0 - 7 (giving you the 8 powerset elements). as you count, you look at which bits in your counter are on. so when you get to say 3, your counter looks like this: 000000...000011 (the exact number of 0's depends on your use of an int vs. a long, but it doesn't matter). so, from this, you know to include the 1st and the 2nd elements of the original set ( the set called e) as the elements of one of your powerset (E elements.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Tyler Long
Greenhorn
Joined: Mar 02, 2007
Posts: 4
|
|
Is this right? And I do this up until 16? 2^4? Let's say I had the set {Grapes, Apples, Oranges, Pears}. 0001 {Grapes} 0010 {Apples} 0011 {Grapes, Apples} 0100 {Oranges} 0101 {Grapes, Oranges} 0110 {Apples, Oranges} 0111 {Grapes, Apples, Oranges} 1000 {Pears}
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24040
|
|
|
Yes, that's correct!
|
 |
Tyler Long
Greenhorn
Joined: Mar 02, 2007
Posts: 4
|
|
Originally posted by Ernest Friedman-Hill: Yes, that's correct!
Thank you very much. I really appreciate your help. You taught me something new.
|
 |
 |
|
|
subject: Powerset algorithm
|
|
|