You are on page 1of 3

c++ - generate all subsets of size k from a set - Stack Overflow

http://stackoverflow.com/questions/4555565/generate-all-subsets-of-size-...

generate all subsets of size k from a set

i want to generate all the subsets of size k from a set. eg:-say i have a set of 6 elements, i have to list all the subsets in which the cardinality of elements is 3. I tried looking for solution,but those are code snippets. Its been long since I have done coding,so I find it hard to understand the code and construct a executable program around it. A complete executable program in C or C++ will be quite helpful. Hoping of an optimal solution using recursion. Thanks in advance. Kumar.
c++ c algorithm

edited Dec 29 '10 at 15:58 Neil 2,499 3 13

asked Dec 29 '10 at 15:51 Kumar 1 1 3

12 Requests for "please give me a complete program" are usually met with hostility here. You should show your
work, and illustrate exactly what you're having trouble with. John Dibling Dec 29 '10 at 15:55

This appears to be written in the form of a homework problem ... Zac Howland Dec 29 '10 at 15:59 Hint: How would you go about getting all subsets of cardinally of 3? You wouldn't just make random stabs at it, would you? Once you have that in mind, begin to write your program. Neil Dec 29 '10 at 16:01

"Recursion" and "optimal" almost never belong in the same sentence. R.. Dec 29 '10 at 16:30 Did you check the faq? stackoverflow.com/tags/algorithm/faq Aryabhatta Dec 29 '10 at 16:54

show 3 more comments feedback

5 Answers
Initialize a bit array with (1<<nbits)-1 and then use this algorithm: http://graphics.stanford.edu/~seander/bithacks.html#NextBitPermutation For sets larger than the maximum integer size, you can still apply the same algorithm to your own type.
answered Dec 29 '10 at 16:28 R.. 58.5k 5 40 133 feedback

#include <cstdio> void g(int s[],int p,int k,int t[],int q=0,int r=0) { if(q==k) {

1 of 3

01-Oct-12 18:50

c++ - generate all subsets of size k from a set - Stack Overflow

http://stackoverflow.com/questions/4555565/generate-all-subsets-of-size-...

for(int i=0;i<k;i++) printf("%d ",t[i]); printf("\n"); } else { for(int i=r;i<p;i++) { t[q]=s[i]; g(s,p,k,t,q+1,i+1); } } } main() { int s[]={1,2,3,4,5},t[5]; g(s,5,3,t); }
edited Sep 23 at 22:51 answered Dec 29 '10 at 16:08 Sanjeevakumar Hiremath marcog 3,460 8 24 22.8k 13 76 123 Why the one-liner ? :O Muggen Dec 29 '10 at 16:49 @Muggen: there are actually two of them. ruslik Dec 29 '10 at 16:53 @Muggen I think @John's comment above says the reason quite well. marcog Dec 29 '10 at 16:57 it gives me expected ;, , or ) before = token but don't worry. Since op tagged both C and C++ I don't think he cares. +1 anw. The results looks correct. Muggen Dec 29 '10 at 17:12 @Muggen Compiles fine with GCC: ideone.com/x5vFa marcog Dec 29 '10 at 17:14

feedback

Take a look at http://photon.poly.edu/~hbr/boost/combinations.html


answered Dec 29 '10 at 16:10 RedX 4,654 7 19 feedback

The most intuitive algorithm would indeed use recursion. When you have a set, we will assume you can iterate over all its elements. If I call tail(e) a set of all the elements after element e. Thus I want right now combinations(s,k) loop over each element in s and get e :: combinations(tail(e), k-1) where :: means "concatenated to each of" Of course sometimes there will be no such combinations (you are off the end of the list). You just need a master collection (set of sets) to add your combinations to and a way to create So assuming we have no globals anywhere we can have something like: getCombinations( headset [in], tailset [in], count [in], output [append] ) headset or tailset could be empty. count could be 0, in which case we write "headset" to output (the base condition) otherwise we iterate through each element in tailset, adding it (locally) to headset, make tailset (locally) the tail of our element, subtract 1 from count and "recurse" by calling the function.
answered Dec 29 '10 at 16:59 CashCow 12.2k 1 9 30

2 of 3

01-Oct-12 18:50

c++ - generate all subsets of size k from a set - Stack Overflow

http://stackoverflow.com/questions/4555565/generate-all-subsets-of-size-...

feedback

Find a working code below #include<iostream> #include<string> #include<list> using namespace std; void print( list<int> l){ for(list<int>::iterator it=l.begin(); it!=l.end() ; ++it) cout << " " << *it; cout<<endl; } void subset(int arr[], int size, int left, int index, list<int> &l){ if(left==0){ print(l); return; } for(int i=index; i<size;i++){ l.push_back(arr[i]); subset(arr,size,left-1,i+1,l); l.pop_back(); } } int main(){ int array[5]={1,2,3,4,5}; list<int> lt; subset(array,5,3,0,lt);

return 0; }
answered Dec 11 '11 at 19:35 user843453 11 2 feedback

Not the answer you're looking for? Browse other questions tagged c++ c algorithm or ask your own question.

question feed

3 of 3

01-Oct-12 18:50

You might also like