Combination Generation Algorithm Based on Lexicographic Order
01:22:52 | classification: discrete mathematics | label: discrete mathematics arrangement and combination | report | large font size, small/medium subscription
I. Problem Description
Given a non-empty set a, all the combinations of set a are generated in Lexicographic Order. The concepts of lexicographic orders are not strictly defined here. It is just a simple explanation.
The Lexicographic Order is a method for comparing strings. For example, the two strings are ABCD and ABEF. Who are the big strings? Obviously, how does ABEF> ABCD come to this conclusion? Compare each character from left to right. First, compare the first character of the two strings, which are both a and equal. Second, compare the second character of the two strings, which are both B and are still equal; compare the third character, respectively C and E, C <E; so ABEF> ABCD.
Ii. Problem Analysis
Set a = {1, 2, 3, 4, 5} to generate all three combinations of.
The number of all three combinations of A is C (5, 3) = 10 in Lexicographic Order, and the combination is:
{1, 123,124,125,134,135,145,234,235,245,345}
① (3) (4) (6) (7)
Please carefully observe the number of each combination. For the convenience of observation, I have coded each combination.
[Note: What is the rule between any two consecutive combinations ?]
For ease of analysis, two nouns are defined: the current number of combinations and the next number of combinations. You can use any combination number numbered I as the current combination number. The next combination number is the combination number numbered I + 1.
| Current combination count |
Next Combination number |
Generate the next one by the number of current combinations Number of combinations |
Thinking Methods |
| 123 |
124 |
3 + 1 of the last digit |
Each combination is in ascending order. |
| 124 |
125 |
4 + 1 of the last digit |
Abstract The concept of "location" |
| 125 |
134 |
Generating 124,125 rules is no longer applicable, and 125 of the single-digit Number 5 is the largest element in A. Do not add another 1. Consider 10. The number of digits is 2. The first 3 is 13, and the third is the number of digits. The word must be larger than the ten digits, and smaller than the ten digits. The number is 4, so the next combination of 125 is 134. |
① Abstract the concept of the maximum number at each position ② Propose preliminary assumptions (Rules) A. Change each position from left to right Number; B. If the number at a single position cannot be Change: change the number at ten locations. C. Change the number at the high position Then, the numbers at the low position are sequentially Add 1. |
| 134 |
135 |
You can add 1 directly to the last digit 4 of 134. |
Verify the preceding assumptions |
| 135 |
145 |
Same as 125 → 134 |
Verify the preceding assumptions |
| 145 |
234 |
|
Verify the preceding assumptions |
| 234 |
235 |
|
Verify the preceding assumptions |
| 235 |
245 |
|
Verify the preceding assumptions |
| 245 |
345 |
|
Verify the preceding assumptions |
Conclusion:
(1) In the lexicographically generated algorithm, set a must be a fully ordered set.
(2) Each combination is composed of r numbers;
(3) Two concepts are formed: location (everybody, ten, hundreds of places ......) , The maximum number of each position
(3) When generating the next combination from the current combination, first change the location of a single digit, then change the location of a single digit, and then change the location of a hundred digits ,......
(4) the maximum number of single digits is N, the maximum number of ten digits is n-1, the maximum number of hundred digits is N-2 ,......
Rule for generating the next combination number
A. Change the number at each position from left to right;
B. If the number at a single position cannot be changed, change the number at 10 positions.
C. After the number at the high position changes, the number at the low position increases by 1 at a time.
Iii. Algorithm Framework
For programming, you need to set some variables, that is, to define the data structure (this process is called modeling ).
Array s [] indicates the current combination; M indicates the current position; maxval indicates the maximum value at the current position
① Initial time: Current Position M = R (single digit); maxval = N (maximum value in one digit)
② Search from left to right for positions that can change numbers;
While (maxval = N ){
M = m-1;
Maxval = maxval-1;
}
After this code is executed, the position of the value can be changed to M.
③ You can add 1 to the number at the position of the number.
S [m] = s [m] + 1
④ Add 1 for each subsequent digit based on the previous Digit
For (j = m + 1; j <= r; j ++)
S [J] = s [J-1] + 1;
4. algorithm programs
# Include <iostream> # include <stdio. h> void printc (INT s [], int R) {for (INT I = 1; I <= r; I ++) printf ("% d ", s [I]); // cout <s [I]; printf ("\ n");} Long FAC (INT N) // calculate the factorial {long F, I; F = 1; for (I = 1; I <= N; I ++) F = f * I; return F;} void next_comb (INT s [], int N, int R) {Int J, M, max_val; max_val = N; M = R; while (s [m] = max_val) {M = s-1; max_val = max_val-1;} s [m] = s [m] + 1; for (j = m + 1; j <r; j ++) s [J] = s [J-1] + 1;} int main () {int s [] = {, 2, 3 }; // array s indicates each generated combination. The first combination is 123 int n = 5, r = 3, CNR = 10; printc (S, R ); CNR = FAC (N)/(FAC (r) * FAC (n-R); For (INT I = 2; I <= CNR; I ++) {next_comb (S, N, R); // generate the next printc (S, R) of the current combination; // screen output }}