-
Subset problem
Hi. I need a c/c++ code, which generates the subsets of a given set. i could not find anywhere on the internet. If you help me, i will be glad.
i have an array which is wArray[] that has length 5 for example.
wArray = {1,2,3,4,5}
this array is the weight array (it contains the weights of some items), so the subsets of it will be:
Code:
sArray = {1,2,3,4,5,(1+2),(1+3),(1+4)....,(1+2+4),....(1+2+3+4+5)}
how can i generate subsets of this set and assign it to a different array?
Thank you...
-
-
i thought that it will be by recursion and also i could not do it by recursion.
how can i do it by a for loop?
-
http://www2.toki.or.id/book/AlgDesig...eratingsubsets
From that source it should be simple to devise an appropriate algorithm.
I have the code.
But the if this is homework, then I won't be posting.
:confused:
He he...
Code:
Enter n: 5
Generating subsets...
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 5
1 2 4
1 2 4 5
1 2 5
1 3
1 3 4
1 3 4 5
1 3 5
1 4
1 4 5
1 5
2
2 3
2 3 4
2 3 4 5
2 3 5
2 4
2 4 5
2 5
3
3 4
3 4 5
3 5
4
4 5
5