Recursive C/C++ and python implementation of combinatorial number algorithm

Keywords: Python

For the list = [1, 2, 3, 4, 5, 6,... ] Find out all combinations by randomly selecting x number combinations from the middle
Algorithmic description:
We have two choices for each number.
Recursive termination condition:
1. Output when there are x selected digits
2. The program ends when the code executes to the last element.
First, let's look at a question for convenience, then we confirm that the combination number is correct.
How to Find the Combination Number C(N,K) [Select the Number of K from the Number of N]
Formula 1: C(N,K)*(N-K+1)/K
Formula 2: C(N,K)=C(N-1,K)+C(N-1,K-1)
Formula interpretation:
For Formula 1: Apparently after expansion
For formula 2:
For the set of numbers {a1,a2,a3,a4,a5,a6,... }
There are two choices for AK and none for ak.
1 > select: then select K-1 from the remaining number of N-1, i.e. C(N-1,K-1)
2 > No: Then choose the number of K from the remaining number of N-1: C(N-1,K)
python code implementation:

def Combination_Method_One(ALL,CHOSE):
    if CHOSE==0 :
        return 1
    else:
        return Combination_Method_One(ALL,CHOSE-1)*(ALL-CHOSE+1)/CHOSE
def Combination_Method_Tow(ALL,CHOSE):
    if  ALL==CHOSE or CHOSE==0:
        return 1
    elif CHOSE==1:
        return ALL
    else:
        return Combination_Method_Tow(ALL-1,CHOSE)+Combination_Method_Tow(ALL-1,CHOSE-1)
def main():
    print(int(Combination_Method_One(10,3)))
    print(Combination_Method_Tow(10,3))
main()
#Output:
"""
15
15
"""

From the above, we can calculate the result of the combination number. Let's go back to the first question, how to output the combined number?
python code implementation:

Number_List=[1,2,3,4,5,6]
Need_Chosen=2
Count=0
def Combination_Method_Tow(ALL,CHOSE):#The result of the combination number is obtained by using the above function.
    if  ALL==CHOSE or CHOSE==0:#
        return 1
    elif CHOSE==1:
        return ALL
    else:
        return Combination_Method_Tow(ALL-1,CHOSE)+Combination_Method_Tow(ALL-1,CHOSE-1)
def Number_Combination(Position=0,Have_Get=[]):
    global Number_List #Declare global variables
    global Need_Chosen #Declare global variables
    global Count       #Declare global variables
    if len(Have_Get)==Need_Chosen:#Output when the number is satisfied
        Count+=1#Number of combinations + 1
        print(tuple(Have_Get))#Output combination
    elif Position==len(Number_List):#When the function comes to the last number in the list, it can't go to the next one, so it's going to return.
        return 0
    else:
        Have_Get.append(Number_List[Position])#Select this number.
        Number_Combination(Position+1,Have_In+1,Have_Get)#Recursion of Selection Time
        del Have_Get[len(Have_Get)-1]#If you don't select this number, you need to delete the number you just added.
        Number_Combination(Position+1,Have_In,Have_Get)#Recursion when not selected
def main():
    Number_Combination()
    print(Count,end=' ')
    print(Combination_Method_Tow(len(Number_List),Need_Chosen))
main()
#Output:
"""
(1, 2)
(1, 3)
(1, 4)
(1, 5)
(1, 6)
(2, 3)
(2, 4)
(2, 5)
(2, 6)
(3, 4)
(3, 5)
(3, 6)
(4, 5)
(4, 6)
(5, 6)
15 15
"""

If you haven't learned python, the above code can be seen as pseudocode
Perhaps the first language many program enthusiasts learn is the C/C++ code implementation given here by the C/C++ authors:

#include <iostream>
#include<list>
using namespace std;
int Length=7;
int Number_List[10]={1,2,3,4,5,6};
int Need_Chosen=2;
int Count=0;
int Combination_Count(int N,int K){
 if(K==1)
  return N;
 else if(N==K||K==0)
  return 1;
 else
  return Combination_Count(N-1,K-1)+Combination_Count(N-1,K);
}
int Combination_ALL(int Position,int Have_In,list<int> Have_Get){
 if(Position==Length)
  return 0;
 else if(Need_Chosen==Have_In){
  cout<<"( ";
  for(list<int>::iterator IT=Have_Get.begin();IT!=Have_Get.end();IT++)
   cout<<*IT<<" ";
  cout<<")"<<endl;
  Count++;
 }
 else{
  Have_Get.push_back(Number_List[Position]);
  Combination_ALL(Position+1,Have_In+1,Have_Get);
  Have_Get.pop_back();
  Combination_ALL(Position+1,Have_In,Have_Get);
 }
}
int main() {
 list<int> Have_Get;
 Combination_ALL(0,0,Have_Get);
 cout<<Combination_Count(6,2)<<' ';
 cout<<Count;
 return 0;
}

END...

Posted by Henaro on Sun, 06 Oct 2019 14:29:24 -0700