Problem: Element Combination Using Marking Method

    Nov 17, 2023

    This article presents an algorithm for the problem of selecting combinations of elements.

    Problem Description

    This article presents an algorithm for the problem of selecting combinations of elements C(n,m)C(n, m).

    Given a set {a1,a2,a3,...,an}\left\{ a_{1},a_{2},a_{3},...,a_{n} \right\} of size nn where n>0n>0, extract mm elements (0<mn)(0<m\leq n) as a combination and output all possible combination schemes.

    Model Establishment

    According to the combination formula, the number of ways to select nn elements from a set of mm elements is given by (nm)=PnmPm=n!m!(nm)!\left( \begin{array}{c} n\\ m\\ \end{array} \right) =\frac{P_{n}^{m}}{P_m}=\frac{n!}{m!\left( n-m \right) !}.

    For a set S={a1,a2,a3,...,an}S=\left\{ a_{1},a_{2},a_{3},...,a_{n} \right\}, an auxiliary marking sequence A=[1,1,1,...,0,0,0]A=\left[ 1,1,1,...,0,0,0 \right] is established to indicate whether the corresponding elements are included in the combination: A[i]=1A[i]=1 indicates that element aia_{i} is included in the combination, where A[i]=0A[i]=0 indicates that element aia_{i} is not included in the combination.

    For example, for the set S={a1,a2,a3,a4}S=\left\{ a_{1},a_{2},a_{3},a_{4} \right\} and the combination C(4,2)C(4,2), all possible combinations are {a1,a2}\left\{ a_{1},a_{2} \right\}, {a1,a3}\left\{ a_{1},a_{3} \right\}, {a1,a4}\left\{ a_{1},a_{4} \right\}, {a2,a3}\left\{ a_{2},a_{3} \right\}, {a2,a4}\left\{ a_{2},a_{4} \right\}, {a3,a4}\left\{ a_{3},a_{4} \right\}. The corresponding auxiliary marking sequences are {a1,a2}[1,1,0,0]\left\{ a_{1},a_{2} \right\}\rightarrow\left[ 1,1,0,0 \right], {a1,a3}[1,0,1,0]\left\{ a_{1},a_{3} \right\}\rightarrow\left[ 1,0,1,0 \right], {a1,a4}[1,0,0,1]\left\{ a_{1},a_{4} \right\}\rightarrow\left[ 1,0,0,1 \right], {a2,a3}[0,1,1,0]\left\{ a_{2},a_{3} \right\}\rightarrow\left[ 0,1,1,0 \right], {a2,a4}[0,1,0,1]\left\{ a_{2},a_{4} \right\}\rightarrow\left[ 0,1,0,1 \right], {a3,a4}[0,0,1,1]\left\{ a_{3},a_{4} \right\}\rightarrow\left[ 0,0,1,1 \right].

    The content expressed by the auxiliary marking sequence is the elements corresponding to the items marked as 11 in the sequence, selected in the set and included in the combination. This auxiliary sequence facilitates the computation of combinations.

    Auxiliary Sequence Transformation

    The concept of the auxiliary marking sequence has been introduced above. Now, two special auxiliary marking sequences are defined.

    Definition A(S)A(S) represents the initial state auxiliary marking sequence for the combination problem of selecting C(n,m)C(n, m) elements from the set S={a1,a2,a3,...,an}S=\left\{ a_{1},a_{2},a_{3},...,a_{n} \right\}, denoted as A(S)=[a1,a2,...,an,an+1,an+2,...,am]A(S)=\left[ a_{1},a_{2},...,a_{n},a_{n+1},a_{n+2},...,a_{m} \right], where ai=1(0<in)a_{i}=1(0<i\leq n) and aj=0(n+1jm)a_{j}=0(n+1 \leq j\leq 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]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)C(n, m) elements from the set S={a1,a2,a3,...,an}S=\left\{ a_{1},a_{2},a_{3},...,a_{n} \right\}, the auxiliary marking sequence A=[a1,a2,...,anm,...,am]A=[a_{1},a_{2},...,a_{n-m},...,a_{m}] is defined, where ai=0(0<inm)a_{i}=0(0<i\leq n-m) and aj=1(nm<jm)a_{j}=1(n-m<j\leq 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]A=[0,0,0,0,1,1,1,1,1] is the terminal state.

    Definition The operation function Countf(A,i)Countf(A,i) for the auxiliary marking sequence A=[a1,a2,...,am]A=\left[ a_{1},a_{2},...,a_{m} \right] is defined as the number of elements satisfying aj=1(0<j<i)a_{j}=1(0<j< i) in the subsequence A=[a1,a2,...,ai]A=\left[ a_{1},a_{2},...,a_{i} \right].

    Example If A=[1,0,0,1,0,1,1,1]A=\left[ 1,0,0,1,0,1,1,1 \right], find Countf(A,i)Countf(A,i) for i=7i=7. Since before A[7]A[7] (excluding A[7]A[7]), there are a total of 33 elements 11 at A[1]A[1], A[4]A[4], and A[6]A[6], so the value of Countf(A,i)Countf(A,i) for i=7i=7 is 33.

    Definition The operation function Exch(A)Exch(A) for the auxiliary marking sequence A=[a1,a2,...,am]A=\left[ a_{1},a_{2},...,a_{m} \right] is defined as follows: find the smallest index ii satisfying ai=1a_{i}=1 and ai+1=0a_{i+1}=0, where 0<im10<i\leq m-1. Then exchange their values, i.e., set ai=0a_{i}=0 and ai+1=1a_{i+1}=1, obtaining the new sequence AA'.

    Example If A=[1,0,0,1,0,1,1,1]A=\left[ 1,0,0,1,0,1,1,1 \right], find Exch(A)Exch(A). Obviously, find the minimum i=1i=1, and get a1=1,a2=0a_{1}=1,a_{2}=0. Swap them to get the new sequence A=Exch(A)=[0,1,0,1,0,1,1,1]A'=Exch(A)=\left[ 0,1,0,1,0,1,1,1 \right]

    Definition The operation function Movb(A,i)Movb(A,i) for the auxiliary marking sequence A=[a1,a2,...,am]A=\left[ a_{1},a_{2},...,a_{m} \right] is defined as follows: let p=Countf(A,i)p=Countf(A,i) and q=ip1q=i-p-1. Transform AA to get the new sequence A=[a1,a2,...,ap,ap+1,...,ai1,ai,...,am]A'=[a_{1},a_{2},...,a_{p},a_{p+1},...,a_{i-1},a_{i},...,a_{m}], where ai=1(1<ip)a_{i}=1(1<i\leq p) and aj=0(p<j<i)a_{j}=0(p<j<i).

    Example If A=[1,0,0,1,0,1,1,1]A=\left[ 1,0,0,1,0,1,1,1 \right], find Movb(A,i)Movb(A,i) for i=5i=5. The new sequence is A=Movb(A,5)=[1,1,0,0,0,1,1,1]A'=Movb(A,5)=\left[ 1,1,0,0,0,1,1,1 \right].

    Definition Based on the auxiliary marking sequence AA, output a combination scheme for the set SS, called a restoration for sequence AA. Denoted as C(A)={aiSA[i]=1,0<im}C(A)=\left\{ a_{i}\in S|A[i]=1 ,0<i\leq m \right\}.

    Example For the set S={a1,a2,a3,a4,a5,a6}S=\left\{ a_{1},a_{2},a_{3},a_{4},a_{5},a_{6} \right\} and the sequence A=[1,1,0,0,1,0]A=[1,1,0,0,1,0], a restoration for SS with respect to AA is {a1,a2,a5}\left\{ a_{1},a_{2},a_{5} \right\}.

    Algorithm Method

    • Step 1: Obtain A=A(S)A=A(S) from the set SS, i.e., the initial state auxiliary marking sequence.
    • Step 2: Output C(A)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))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)C(n,m) elements from the set SS can be output.