The problem of C language to implement permutation and combination algorithm

Source: Internet
Author: User

Permutation combination is the basic tool used in the algorithm, how to implement the permutation and combination in C language. Ideas are as follows:

First look at the recursive implementation, because recursion will decompose the problem, so relatively easy to understand, but need to consume a lot of stack space, if the line stacks space is not enough, then run, and function call overhead is also relatively large.

(1) Full arrangement:

The whole arrangement means that all the elements in the set are arranged in a certain order, using p (n, N) = n! to represent the total number of n elements.

For example, the full array of {1, 2, 3} is:

123;132;

213;231;

312;321;

A total of 6, that is 3. =321=6.

How does this work out?

First take an element, for example, remove 1, then the {2, 3} is left.

Then remove an element from the remaining collection, such as removing 2, then leave {3}.

And so on, take all the possibilities, and that's the whole arrangement, as shown:

Knowing the process, the algorithm is written:

Look at the array as a collection, dividing the collection into two parts: 0~s and S~e, where 0~s represents the element that has been selected, and S~e represents the element that has not been selected.

Perm (set, S, e)
{

    order from s~e to select an element in exchange with S (that is, select an element)
    call Perm (set, S + 1, e)
    until S>e, that is, the remaining set is empty, output set
}

The C language code is as follows:

void perm (int list[], int s, int e, void (*CBK) (int list[])) 
{     
    int i;
    if (S > E)     
    {
        (*CBK) (list);
    }
    else    
    {for         
        (i = s; I <= e; i++)
        {             
             swap (list, S, i);             
             Perm (list, S + 1, E, CBK);             
             Swap (list, S, i);}}

which

void Swap (int * o, int i, int j)
{
    int tmp = o[i];
    O[i] = o[j];
    O[J] = tmp;
}

void Cbk_print (int * subs)
{
    printf ("{");
    for (int i = 0; i < LEN; i++)
    {
        printf ("%d", subs[i]);
        (i = = LEN-1)? printf (""): printf (","); 
    }
    printf ("}\n");
}

(2) Combination:

A combination is a group that takes m elements out of n different elements to synthesize, and the elements in this group have no order. Use C (n, K) to represent the number of k elements that are removed from n elements.

C (n, k) = n! /(k! * (N-K)!)

For example, a combination of 2 elements removed from {1,2,3,4} is:

12;13;14;
23;24;
34

To remove an element from the collection, such as removing 1, the remaining {2,3,4}

Then remove an element from the remaining collection, such as removing 2

Then 12 forms a group, as shown in the figure.

As you can see from the above process, each time you select an element from the collection and then make a k-1 combination of the remaining set (n-1).

Comb (set, subset, N, K)
{

    reverse-selects an element from the set, putting this element into the subset.
    call Comb (set, subset, N-1, k-1)
    until only one element is selected
}

The C language code is as follows:

void combine (int s[], int n, int k, void (*CBK) (int * subset, int k))
{
    <

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.