List all possible combinations of k integers between 1…n (n choose k)

后端 未结 4 488
萌比男神i
萌比男神i 2021-02-05 21:42

Out of no particular reason I decided to look for an algorithm that produces all possible choices of k integers between 1...n, where the order amongst the k integer doesn\'t mat

相关标签:
4条回答
  • 2021-02-05 22:20

    This guy seems to have done serious work in combinatorics using C# (CodeProject) :

    Permutations, Combinations, and Variations using C# Generics

    0 讨论(0)
  • 2021-02-05 22:26

    Here's a relatively simple/efficient nCr program I wrote a while ago in C:

    main(n,k){float t=0,r=1;for(scanf("%d, %d",&n,&k);t++<k;r*=(1+n-t)/t);printf("%.0f\n",r);}
    

    Okay ... readable version. =] (Not sure if this is 1:1 corresponding with the above.)

    void nCr(int n, int k) {
        float curK = 0, r = 1;
        while(curK < k) {
            ++curK;
            printf("%.0f\n", r);
            r *= (1 + n - curK) / curK;
        }
    }
    

    Instead of printing, you could yield or whatever (I don't know C#) into your list.

    0 讨论(0)
  • 2021-02-05 22:36

    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:

    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()));
    
    0 讨论(0)
  • 2021-02-05 22:39

    Asaf,

    You are asking us to evaluate your algorithm, but you don't explain your algorithm -- not even in code comments. So you want everyone to spend an hour or more reverse engineering the algorithm from the code, just so we can understand your question before we answer it?

    Please edit your question to explain your algorithm.

    One thing is obvious -- the memory footprint of your code is horrendous. For even modest values of n, the number of combinatations will easily be in the billions, which will require more memory than most computers have. Plus you are using dynamically grown arrays, which keep reallocating and copying themselves as they grow. Plus your program generates subsets in different arrays and merges them. All in all, your program will require many times the amount of memory that would be ideally needed to store the list, and it will spend most of it's time just copying data back and forth.

    If you must have all the values in an array at once, at least start off by computing the size of the array you need -- n! / (n-k)! / k! -- and then just filling it in.

    Even better would be code that "lazily" just computed each member of the sequence as it was needed. See this question from the related questions sidebar

    0 讨论(0)
提交回复
热议问题