DataSharp/Mathematics/CombinationHelper.cs
/// <summary>
/// Generate all the combinations with the specified number of total elements and the number of elements to be
/// extracted.
/// </summary>
/// <typeparam name="T">The type of elements in the array.</typeparam>
/// <param name="a">The array that contains all the candidate elements.</param>
/// <param name="numSelection">The number of elements to be extracted.</param>
/// <param name="combinations">The collection of all the combinations that are needed to be extracted.</param>
Generate all the combinations with the specified number of total elements and the number of elements to be
extracted.
Generate all the combinations with the specified number of total elements and the number of elements to be extracted .
private static void Combinations<T>(T[] a, int numSelection, LinkedList<T[]> combinations)
        {
            int[] combination = new int[a.Length];
            for (int i = 0; i < numSelection; i++)
                combination[i] = 1;
            while (HasNextCombination(combination, numSelection))
            {
                int count = 0;
                int i = 0;
                AddNextCombination(a, combination, numSelection, combinations);
                for (i = 0; i < a.Length - 1; i++)
                {
                    if ((combination[i] == 1) && (combination[i + 1] == 0))
                    {
                        combination[i] = 0;
                        combination[i + 1] = 1;
                        break;
                    }
                    if (combination[i] == 1)
                        count++;
                }
                for (int j = 0; j < i; j++)
                {
                    if (j < count)
                        combination[j] = 1;
                    else
                        combination[j] = 0;
                }
            }
            AddNextCombination(a, combination, numSelection, combinations);
        }
private static void Combinations < T > ( T [ ] a , int numSelection , LinkedList < T [ ] > combinations ) { int [ ] combination = new int [ a . Length ] ; for ( int i = 0 ; i < numSelection ; i ++ ) combination [ i ] = 1 ; while ( HasNextCombination ( combination , numSelection ) ) { int count = 0 ; int i = 0 ; AddNextCombination ( a , combination , numSelection , combinations ) ; for ( i = 0 ; i < a . Length - 1 ; i ++ ) { if ( ( combination [ i ] == 1 ) && ( combination [ i + 1 ] == 0 ) ) { combination [ i ] = 0 ; combination [ i + 1 ] = 1 ; break ; } if ( combination [ i ] == 1 ) count ++ ; } for ( int j = 0 ; j < i ; j ++ ) { if ( j < count ) combination [ j ] = 1 ; else combination [ j ] = 0 ; } } AddNextCombination ( a , combination , numSelection , combinations ) ; }
Generate all the combinations with the specified number of total elements and the number of elements to be
extracted.
Generate all the combinations with the specified number of total elements and the number of elements to be extracted .
// combination is an int array that indicates conbination. // combination[i] == 1 means that the element in the input array with index i is selected. // combination[i] == 0 means that the element in the input array with index i is not selected. // Loop until there is no combination can be generated. // A counter that stores the number of 1 that is before the 1 we swap. // The loop-counter that finds the index of the 1 we swap. // Add next combination to the linked list that stores all the combinations. // Find the first "10" pair and the make them "01". // Stores the number of 1 before the "10". // Record the number of 1 before the 1 we swap. // Move back. // Add the last combination to the linked list that stores all the combinations.
a
T[]
The array that contains all the candidate elements.
The array that contains all the candidate elements .
numSelection
int
The number of elements to be extracted.
The number of elements to be extracted .
combinations
LinkedList<T[]>
The collection of all the combinations that are needed to be extracted.
The collection of all the combinations that are needed to be extracted .
typeparam
The type of elements in the array.
The type of elements in the array .