Permutation algorithm and combinatorial algorithm of algorithm _c language

Source: Internet
Author: User

1. Foreword

This paper introduces the commonly used permutation and combination algorithms, including full permutation algorithm, full combination algorithm, m number selection and n combination algorithm.

2. Permutation algorithm

The common permutation algorithms are:
(A) Dictionary order method
(B) The increment method of the decimal system
(C) Descending rounding Method
(D) The adjacent-position commutation method
(E) Recursive method

There are two common types of introductions:

(1) Dictionary order method

A sequence of characters in a given character set is specified, in which each permutation is produced sequentially.

[Example] Character set {1,2,3}, the smaller number is the first, so the entire order is generated in dictionary order: 123,132,213,231,312,321.

Generate the next arrangement for a given whole arrangement the next one is this one and the next. There is no adjacent string in the dictionary order. This requires that this one be prefixed as long as possible with the next, and that the change be limited to the shortest possible suffix.

Algorithm idea:

Set p to be a full arrangement of [1,n].
P=p1p2 ... Pn=p1p2 ... Pj-1pjpj+1 ... Pk-1pkpk+1 ... Pn, j=max{i| Pi<pi+1}, k=max{i| PI>PJ}, swap pj,pk, will pj+1 ... Pk-1pjpk+1 ... PN Flip, P ' = p1p2 ... PJ-1PKPN ... Pk+1pjpk-1 ... Pj+1 that P's next

Example: The next permutation of 839647521.

From the very right, find the first one smaller than the right number 4 (because 4<7, and 7>5>2>1), and then from the very right to find 4 right than 4 of the number 5 (because 4>2>1 and 4<5), Exchange 4, 5, at this time 5 right is 7421, Inverted to 1247, that is, the next arrangement: 839651247. This method is used to write out the full array of non recursive algorithms as follows

This method supports duplication of data and is adopted in C + + STL.

(2) Recursive method

Set a number of P = {r1, r2, R3, ..., RN}, all arranged as Perm (p), pn = P–{rn}. Then perm (p) = R1perm (p1), R2perm (P2), r3perm (P3), ..., Rnperm (PN). When n = 1 o'clock perm (P} = r1.

such as: {1, 2, 3, 4, 5} of the full arrangement

1, first look at the last two number 4, 5. They are all arranged in 4 5 and 5 4, that is, the full arrangement of 4 beginning with 5 and the whole arrangement of 5 beginning with 4.

Because the whole arrangement of a number is itself, the result is obtained.

2, look after the three number 3, 4, 5. They are all arranged in 3 4 5, 3 5 4, 4 3 5, 4 5, 3 5, 3, 4, 5 4 group numbers.

That is, a fully arranged combination of 3 opening and 4,5, an all-ranked combination of 4 opening and 3,5, and a fully arranged combination of 5 and 3,4.

#include <stdio.h>
 
int n = 0;
 
void swap (int *a, int *b)
 
{
 
 int m;
 
 m = *a;
 
 *a = *b;
 
 *b = m;
 
}
 
void perm (int list[], int k, int m)
 
{
 
 int i;
 
 if (K > m)
 
 {
 
  for (i = 0; I <= m; i++)
 
   printf ("%d", List[i]);
 
  printf ("\ n");
 
  n++;
 
 }
 
 else
 
 {
 
  for (i = k; I <= m; i++)
 
  {
 
   swap (&list[k], &list[i]);
 
   Perm (list, K + 1, m);
 
   Swap (&list[k], &list[i]);
 
}} int main ()
 
{
 
 int list[] = {1, 2, 3, 4, 5};
 
 Perm (list, 0, 4);
 
 printf ("total:%d\n", n);
 
 return 0;
 
}

3. Combinatorial algorithm

3.1 Full combination

In this paper, the binary transformation method, that is, each combination and a binary number corresponding to enumerate the binary, the enumeration of each combination. such as String: ABCDE

00000 <––> null
00001<––> e
00010 <––> d
...
11111 <––> ABCDE

3.2 Number of m selected from N

(1) Recursive

A. First, select the largest number from the number of N, and then choose the number of m-1 in the remaining number of n-1 until you select 1 numbers from the N (m-1) number.

B. Select a number from the number of n number of the second small, continue to perform 1 steps until the current optional number of the largest number of M.

The following is the implementation of the Recursive method:

Find all combinations of M elements from the array A[1..N].
 
///A[1..N] represents the candidate set, N is the candidate set size, n>=m>0.
 
///B[1..M] is used to store elements in the current group (where the element subscript is stored), and
 
///constant M represents the number of elements in a combination of conditions, m=m, which are used only to output the results.
 
void Combine (int a[], int n, int m, int b[], const int m)
 
{
 
 for (int i=n; i>=m; i--)  //Note the loop scope here
 
 {
 
  b[m-1] = i-1;
 
  if (M > 1)
 
   Combine (a,i-1,m-1,b,m);
 
  else           //M = = 1, output a combination
 
  {for
 
   (int j=m-1; j>=0; j--)
 
   cout << A[b[j]] << "";
 
   cout << Endl;
 
  }
 
 }
 

(2) 01 conversion method

The idea of this program is to open an array whose subscript indicates the number of 1 to N, and the value of the array element is 1 to indicate that the number represented is selected, and 0 is not selected.

Initializes the first n elements of the array to 1, indicating that the first group is the number of top N.

It then scans the "10" combination of the array element values from left to right, finds the first "10" combination and changes it to a "01" combination, and moves all "1" to the left of it to the leftmost end of the array.

When the first "1" moves to the n-m position of the array, where n "1" is all moved to the far right, the last combination is obtained.

For example, find a combination of 3 in 5:

1 1 1 0 0//1,2,3 1 1 0 1 0//1,2,4 1 0 1 1//1,3,4 0 0 1 1 1//2,3,4 0 1 1 0 0//1,2,5 1 1 0 1 0
 
// 1,3,5
 
0 1 1 0 1//2,3,5 1 0 0 1 1//1,4,5 0 1 0 1 1
 
//2,4,5 0 0 1 1
 
//3,4,5

4. Reference materials
(1) http://www.jb51.net/article/54441.htm
(2) http://www.jb51.net/article/54443.htm
(3) Combinatorial algorithm

The idea of this procedure is to open an array, its subscript represents the number of 1 to M, the array element value of 1 means that its subscript represents the number of selected, 0 is not selected.

Initializes the first n elements of the array to 1, indicating that the first group is the number of top N.

It then scans the "10" combination of the array element values from left to right, finds the first "10" combination and changes it to a "01" combination, and moves all "1" to the left of it to the leftmost end of the array.

When the first "1" moves to the m-n position of the array, where n "1" is all moved to the far right, the last combination is obtained.

For example 5 Select the combination of 3:
1 1 1 0 0//1,2,3
1 1 0 1 0//1,2,4
1 0 1 1 0//1,3,4
0 1 1 1 0//2,3,4
1 1 0 0 1 1,2,5
1 0 1 0 1//1,3,5
0 1 1 0 1//2,3,5
1 0 0 1 1//1,4,5
0 1 0 1 1//2,4,5
0 0 1 1 1//3,4,5  

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.