Recursive approach <http://ideone.com/w14kV>:
- The set of subsets of {1} is {{}, {1}} - For {1, 2}, take {{}, {1}}, add 2 to each subset to get {{2}, {1, 2}} and take the union with {{}, {1}} to get {{}, {1}, {2}, {1, 2}} - Repeat till you reach n Iterative Approach <http://ideone.com/3C0Ry>: Implementation is based on this<http://www.mathsisfun.com/sets/power-set.html> approach. Thanks & Regards Anantha Krishnan On Mon, Jul 18, 2011 at 8:48 PM, saurabh singh <saurab...@gmail.com> wrote: > Efficient recursive approaches are also welcome. > > > On Mon, Jul 18, 2011 at 8:47 PM, saurabh singh <saurab...@gmail.com>wrote: > >> Excuse for the trivial question.I am trying to design an iterative >> approach for the same. >> My best shot has been. >> #include<iostream> >> #include<bitset> >> using namespace std; >> void printset(char a[],bitset<3> &ss) >> { >> cout<<"{ "; >> for(int i=0;i<3;i++) if(ss.test(i)) cout<<a[i]<<" "; >> cout<<" }"<<endl; >> } >> int main() >> { >> char a[]={'a','b','c'}; >> unsigned j; >> for( j=0;j<=7;j++) >> { >> bitset<3> charset(j); >> printset(a,charset); >> } >> return 0; >> } >> http://www.ideone.com/8Q5jQ >> Its really an ugly code with lots of things hardcoded breaking all rules >> of aesthetic coding,excuse me for that. >> Can anyone suggest a better algorithm.Assuming the bitset operation to be >> o(1) {which may not be the case} my algo is o(2^n) {BAD :( } >> >> -- >> Saurabh Singh >> B.Tech (Computer Science) >> MNNIT ALLAHABAD >> >> >> > > > -- > Saurabh Singh > B.Tech (Computer Science) > MNNIT ALLAHABAD > > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to algogeeks@googlegroups.com. > To unsubscribe from this group, send email to > algogeeks+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.