Sub sets of a set

Posted on 1997-07-20
Last Modified: 2012-05-05
I need a nice alogrithm to find all the sub sets of various sets of integers. Anyone know an elagent way of doing this. The set I am working with is stored simply as an integer array.
Question by:simonc

Accepted Solution

8051 earned 100 total points
ID: 1252437
The undefined "subset" means that you can not use
tricks like a bit tests and a lot of things, can
be implemented if you have a constant subset. OK,
the only thing possible in this case - to sort
subset and check each element of set for highest
and lowest range of subset.

If you can sort set - sort it, find a range where
elements can be a members of subset and check only
this elements.

Another way (if you have enough memory, for bytes
for example) to create array of mask that keeps 1
for each element that is a member.

//example for bytes

unsigned char member[0x100];

void make_subset(char * subset, int len)
   int tmp;
   for (tmp=0;tmp<0x100;tmp++) member[tmp]=0;
   for (tmp=0;tmp<len;tmp++) member[subset[tmp]]=1;

int is_member(unsigned char x)
   return member[x];

Look Borland C 3.1 RTL how to use it with maximum


LVL 84

Expert Comment

ID: 1252438
int set[]={...};
for( i=0;i < 1<<sizeof(set)/sizeof(set[0]); i++ ){
/* if this overflows, you'd never have finished finding all subsets anyway*/
        printf("subset %d:",i);
       for( j=0; j<sizeof(set)/sizeof(set[0]); j+= 1){
                if( i&(1<<j) ){ print(" %d",set[j]); }

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now