all combinations of k elements out of n

后端 未结 5 2132
星月不相逢
星月不相逢 2020-12-01 03:23

Can somebody provide me a link or pseudocode of a function for finding all combinations of k elements out of n? possibly in STL. I don\'t need to compute n choose k, I need

相关标签:
5条回答
  • 2020-12-01 03:32

    http://howardhinnant.github.io/combinations.html

    Search for "for_each_combination". If you find something faster, please let me know. Unlike other algorithms I often see, this one doesn't require the element type to be LessThanComparable.

    0 讨论(0)
  • 2020-12-01 03:32

    Here is a lazy example of pseudocode that can get the job done...

    void nChooseK(array[n],k){
        recurse("",array[n],k);      
    }
    
    void recurse(initialString,array[n],k){
        if(k == 0){
            print initialString;
            return;
         }
        for(i=0;i<n;i++){
            tmpArray = array[0...i-1]+array[i+1...];//the array without the object to remove
            recurse(initialString + array[i], tmpArray,k-1)
        }        
    }
    
    0 讨论(0)
  • 2020-12-01 03:33

    Create an auxiliary vector with n - k zeros followed by k ones. A zero means the element in the original container is not included, whereas one means the element is included.

    Now use std::next_permutation on the auxiliary vector to get the next combination.

    0 讨论(0)
  • 2020-12-01 03:37

    You could use std::next_permutation but it is n! and not n choose k. You could filter them after you created them. But this solution is O(n!), not really perfect. Here is the trial and error solution:

    int factorial(int value)
    {
        int result = 1;
    
        for(int i = 1; i <= value; i++)
        {
            result *= i;
        }
    
        return result;
    }
    
    std::set<std::set<int>> binomial_coefficient(std::vector<int> input, int k)
    {
        std::set<std::set<int>> solutions;
    
        for(unsigned int i = 0; i < factorial(input.size()); i++)
        {
            std::next_permutation(input.begin(), input.end());
    
            solutions.insert(std::set<int>(input.begin(), input.begin() + k));
        }
    
        return solutions;
    }
    
    0 讨论(0)
  • 2020-12-01 03:53

    In C++ given the following routine:

    template <typename Iterator>
    inline bool next_combination(const Iterator first, Iterator k, const Iterator last)
    {
       /* Credits: Thomas Draper */
       if ((first == last) || (first == k) || (last == k))
          return false;
       Iterator itr1 = first;
       Iterator itr2 = last;
       ++itr1;
       if (last == itr1)
          return false;
       itr1 = last;
       --itr1;
       itr1 = k;
       --itr2;
       while (first != itr1)
       {
          if (*--itr1 < *itr2)
          {
             Iterator j = k;
             while (!(*itr1 < *j)) ++j;
             std::iter_swap(itr1,j);
             ++itr1;
             ++j;
             itr2 = k;
             std::rotate(itr1,j,last);
             while (last != j)
             {
                ++j;
                ++itr2;
             }
             std::rotate(k,itr2,last);
             return true;
          }
       }
       std::rotate(first,k,last);
       return false;
    }
    

    You can then proceed to do the following:

    // 9-choose-3 
    std::string s = "123456789";
    std::size_t k = 3;
    do
    {
       std::cout << std::string(s.begin(),s.begin() + k) << std::endl;
    }
    while(next_combination(s.begin(),s.begin() + k,s.end()));
    

    Or for a std::vector of int's:

    // 5-choose-3 
    std::size_t n = 5;
    std::size_t k = 3;
    
    std::vector<int> ints;
    for (int i = 0; i < n; ints.push_back(i++));
    
    do
    {
       for (int i = 0; i < k; ++i)
       {
          std::cout << ints[i];
       }
       std::cout << "\n";
    }
    while(next_combination(ints.begin(),ints.begin() + k,ints.end()));
    
    0 讨论(0)
提交回复
热议问题