Results 1 to 4 of 4

Thread: subsets of a list

  1. #1

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    subsets of a list

    I have a list and I need to output each subset of the list

    for example a b c d e

    would output to

    a
    b
    c
    d
    e
    ab
    ac
    ad
    ae

    abc
    abd
    abe
    bcd
    bce
    ....
    abcde
    I believe the correct term is combination no element should be duplicated on the same line

    I was going to attempt this with a series of loops but im not even sure wehre to start

    any suggestions?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: subsets of a list

    This is a perfect job for recursion. Consider all the combinations that include 'a'. They would be 'a' itself plus 'a' added to all the combinations from the rest of the list. There's your recursive step right there.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: subsets of a list

    not sure i follow

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: subsets of a list

    Think about it. To create all the combinations that start with 'a' you can create all the combinations for the rest of the list and add 'a' to the beginning. That means that to solve the problem for one list you can solve the problem for a smaller, and to do that you can solve the problem for a smaller list again and so on. That's the very definition of recursion.

    So, if we have a list L(0 to 10) then to get all the combinations for that list we would start with L(0) and then get all the combinations for L(1 to 10) and add L(0) to the beginning of them. To get all the combinations for L(1 to 10) you would start with L(1) and then get all the combinations for L(2 to 10) and add L(1) to the beginning of them. You keep solving the same problem over and over for smaller and smaller lists. Each solution uses the solutions that came before. That's recursion.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width