Problem Description
This article presents an algorithm for the problem of selecting combinations of elements C(n,m).
Given a set {a1,a2,a3,...,an} of size n where n>0, extract m elements (0<m≤n) as a combination and output all possible combination schemes.
Model Establishment
According to the combination formula, the number of ways to select n elements from a set of m elements is given by (nm)=PmPnm=m!(n−m)!n!.
For a set S={a1,a2,a3,...,an}, an auxiliary marking sequence A=[1,1,1,...,0,0,0] is established to indicate whether the corresponding elements are included in the combination: A[i]=1 indicates that element ai is included in the combination, where A[i]=0 indicates that element ai is not included in the combination.
For example, for the set S={a1,a2,a3,a4} and the combination C(4,2), all possible combinations are {a1,a2}, {a1,a3}, {a1,a4}, {a2,a3}, {a2,a4}, {a3,a4}. The corresponding auxiliary marking sequences are {a1,a2}→[1,1,0,0], {a1,a3}→[1,0,1,0], {a1,a4}→[1,0,0,1], {a2,a3}→[0,1,1,0], {a2,a4}→[0,1,0,1], {a3,a4}→[0,0,1,1].
The content expressed by the auxiliary marking sequence is the elements corresponding to the items marked as 1 in the sequence, selected in the set and included in the combination. This auxiliary sequence facilitates the computation of combinations.
The concept of the auxiliary marking sequence has been introduced above. Now, two special auxiliary marking sequences are defined.
Definition A(S) represents the initial state auxiliary marking sequence for the combination problem of selecting C(n,m) elements from the set S={a1,a2,a3,...,an}, denoted as A(S)=[a1,a2,...,an,an+1,an+2,...,am], where ai=1(0<i≤n) and aj=0(n+1≤j≤m). It can be considered as the first combination method among all combination methods for the combination problem.
Example The sequence A=[1,1,1,1,0,0,0,0,0] is the initial state.
The initial state auxiliary marking sequence has been introduced above. Now, the terminal state auxiliary marking sequence is introduced.
Definition For the combination problem of selecting C(n,m) elements from the set S={a1,a2,a3,...,an}, the auxiliary marking sequence A=[a1,a2,...,an−m,...,am] is defined, where ai=0(0<i≤n−m) and aj=1(n−m<j≤m). This sequence is called the terminal state auxiliary marking sequence of this set.
Example The sequence A=[0,0,0,0,1,1,1,1,1] is the terminal state.
Definition The operation function Countf(A,i) for the auxiliary marking sequence A=[a1,a2,...,am] is defined as the number of elements satisfying aj=1(0<j<i) in the subsequence A=[a1,a2,...,ai].
Example If A=[1,0,0,1,0,1,1,1], find Countf(A,i) for i=7. Since before A[7] (excluding A[7]), there are a total of 3 elements 1 at A[1], A[4], and A[6], so the value of Countf(A,i) for i=7 is 3.
Definition The operation function Exch(A) for the auxiliary marking sequence A=[a1,a2,...,am] is defined as follows: find the smallest index i satisfying ai=1 and ai+1=0, where 0<i≤m−1. Then exchange their values, i.e., set ai=0 and ai+1=1, obtaining the new sequence A′.
Example If A=[1,0,0,1,0,1,1,1], find Exch(A). Obviously, find the minimum i=1, and get a1=1,a2=0. Swap them to get the new sequence A′=Exch(A)=[0,1,0,1,0,1,1,1]
Definition The operation function Movb(A,i) for the auxiliary marking sequence A=[a1,a2,...,am] is defined as follows: let p=Countf(A,i) and q=i−p−1. Transform A to get the new sequence A′=[a1,a2,...,ap,ap+1,...,ai−1,ai,...,am], where ai=1(1<i≤p) and aj=0(p<j<i).
Example If A=[1,0,0,1,0,1,1,1], find Movb(A,i) for i=5. The new sequence is A′=Movb(A,5)=[1,1,0,0,0,1,1,1].
Definition Based on the auxiliary marking sequence A, output a combination scheme for the set S, called a restoration for sequence A. Denoted as C(A)={ai∈S∣A[i]=1,0<i≤m}.
Example For the set S={a1,a2,a3,a4,a5,a6} and the sequence A=[1,1,0,0,1,0], a restoration for S with respect to A is {a1,a2,a5}.
Algorithm Method
- Step 1: Obtain A=A(S) from the set S, i.e., the initial state auxiliary marking sequence.
- Step 2: Output C(A).
- Step 3: Check if the obtained auxiliary marking sequence is in the terminal state. If yes, terminate. If no, proceed to the next step.
- Step 4: Perform the transformation A=Exch(Movb(A)), and go back to Step 2.
With the above method, all combination schemes for the combination problem of selecting C(n,m) elements from the set S can be output.