|
-
Feb 16th, 2011, 06:35 PM
#1
Thread Starter
Fanatic Member
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?
-
Feb 16th, 2011, 06:53 PM
#2
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.
-
Feb 16th, 2011, 09:59 PM
#3
Thread Starter
Fanatic Member
-
Feb 16th, 2011, 10:29 PM
#4
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|