generate all subsets of size k from a set

后端 未结 9 1369
南旧
南旧 2020-12-08 03:30

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

9条回答
  •  广开言路
    2020-12-08 03:55

    Here's some pseudocode. You can cut same recursive calls by storing the values for each call as you go and before recursive call checking if the call value is already present.

    The following algorithm will have all the subsets excluding the empty set.

    list * subsets(string s, list * v){
        if(s.length() == 1){
            list.add(s);    
            return v;
        }
        else
        {
            list * temp = subsets(s[1 to length-1], v);     
            int length = temp->size();
    
            for(int i=0;i

提交回复
热议问题