Thread: compute the subsets of a set

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    27

    compute the subsets of a set

    Hi.I want to compute all the subsets of a set.For example for the set:
    {1,2,3,4}
    I want to find the following:
    {1}
    {2}
    {3}
    {4}
    {1,2}
    {1,3}
    {1,4}
    {2,3}
    {2,4}
    {3,4}
    {1,2,3}
    {1,2,4}
    {1,3,4}
    {2,3,4}
    {1,2,3,4}

    But I want to do it iteratively NOT recursively.
    Are there any suggestion?
    I'm not looking for the code.Just an algorithm or some ideas.

    Thank you in advance.

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Use nested loops. Outer loop defines the number of values in the subset. Then you'll need a couple loops to iteratively get the subsets.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    27
    The thing is that i don't know the size of the set.The size changes.The function should take the size of the set as an argument and it should succeed for every set.

  4. #4
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    You could probably store your sets in a way that you could use strlen() on them.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by k_w_s_t_a_s
    The thing is that i don't know the size of the set.The size changes.The function should take the size of the set as an argument and it should succeed for every set.
    You don't need to.

    a) Get a set.
    b) Count the members.
    c) Store the count in a variable.
    d) Use something like:
    Code:
    for( x = 0; x < count; x++ )
        for( y = x; y < count; y++ )
    That's just an illustration, not complete code naturally. But it does at least show you that it doesn't matter how long your set is here for you to get at least the first portion of your example done.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 08-19-2007, 08:10 AM
  2. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  3. The new FAQ
    By Hammer in forum A Brief History of Cprogramming.com
    Replies: 34
    Last Post: 08-30-2006, 10:05 AM
  4. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM
  5. Set default directory with GetTempDir?
    By Bajanine in forum Windows Programming
    Replies: 2
    Last Post: 05-04-2003, 11:36 PM