• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

How do i get all subsets?

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there a way of getting all subsets from a Set? I've tried writing it myself but its doing me head in, and making me think I'm not cut out for this programming

Any help would be greatly appreciated.
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

are you sure you need the power set of a set? Be aware that the power set will get huge very quickly. For a set with more than 20 to 30 entires, it will definitely not be representable with any normal Java data structure, and you'll run out of memory long, long before then.
But if you're sure that you need the power set, you might try it with recursion...

Stefan
 
Charlie Goth
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes I need it, its for calculating the winnings of 'canadian' style bets, that is bets on A, B, C, AB, AC, BC & ABC.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're willing to use iteration here's a technique that often works nicely:

There is a correspondence between binary numbers and subsets of a given set. Suppose I have a set of things {A, B, C}. I'll say that the subset {A, B, C} corresponds to the binary number 111, {A, C} corresponds to 101, {} corresponds to 000. If I've got an ordering of the items I can just say that a 1 means the item is in a set and 0 means it isn't simple enough.

Now if we want to iterate through all subsets of {A, B, C} we can just count the numbers from 000 to 111. Here's what the code might look like (I haven't tested it, so assume there are bugs):


[ August 08, 2004: Message edited by: David Weitzman ]
 
I don't even know how to spell CIA. But this tiny ad does:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic