[algorithm design and analysis] solving athlete matching problem by backtracking method (curriculum design)

Posted by mrdance on Mon, 03 Jan 2022 06:41:31 +0100

abstract

Aiming at the problem of the best matching of athletes, this paper uses the backtracking method to find the optimal solution of competition advantage score, and studies the best matching method of male and female athletes, so as to maximize the sum of competition advantages of both men and women in each group. To solve this problem, this problem adopts the method of male athletes choosing female athletes, forming an arrangement tree. The nodes of the tree represent female athletes, and the layers of the arrangement tree represent male athletes. After algorithm processing, the number conforming to the optimal value is output. The example results show that the combination of male No. 1 and female No. 1, male No. 2 and female No. 3, male No. 3 and female No. 2 have the greatest competition advantage. The algorithm is simple, easy to understand, and has good practicability and skill.

1. Problem description

The badminton team has n men and women. Given 2 n × N matrices P and Q. P[i][j] is the competitive advantage of male athletes I and female athletes j in mixed doubles; Q[i][j] is the competition advantage of female athletes with the cooperation of female athletes I and male athletes j. Due to various factors such as technical cooperation and psychological state, P[i][j] is not necessarily equal to Q[j][i]. The combination of male and female athletes in doubles competition is the advantage of male and female athletes [i] [j] [i] [j]. An algorithm is designed to calculate the best matching method of male and female athletes, so as to maximize the sum of competition advantages of both men and women in each group.

2. Problem analysis

The output of this problem is the collocation problem of men and women. Because athletes cannot make repeated choices, the solution space tree of this problem is an arrangement tree, and the arrangement tree backtracking method template can be used for algorithm design.
Suppose n is the number of male and female athletes in the badminton team, and P[i][j] is the competitive advantage of male athletes in mixed doubles composed of male athletes I and female athletes j; Q[i][j] is the competition advantage of female athletes with the cooperation of female athletes I and male athletes j
This question adopts the method of male athletes choosing female athletes, which forms an arrangement tree. G represents female athletes, and the number of layers of the arrangement tree represents male athletes.
Next, consider the pruning function, because it is uncertain whether the final result is better than the optimal solution when the personnel are not selected, so the pruning function is not required to prune in the backtracking process. Only when a branch runs to the leaf node, it is necessary to judge the relationship between the feasible solution and the optimal solution, and consider whether to update the optimal solution.
Finally, consider the output result. The output result should be a set containing athlete numbers, represented by x[i]. The original array stores the initial number. After algorithm processing, the number conforming to the optimal value is output.

3. Algorithm design

Firstly, the advantages of the ith male athlete and the jth female athlete are calculated into the two-dimensional array j [i] [J], and then n combinations (combinations correspond to each point (I, J)) are selected in this two-dimensional array. It is required that n points cannot be in the same row or column, sum n qualified points and record the maximum value.
Layer I backtracking corresponds to the i-th male athlete, and a female athlete who has not been selected should be selected in this layer backtracking. In order to record whether female athletes have been selected, we create an array x [i] to record and accumulate the advantages of each layer in turn. When I > N, it is the end point of backtracking. At this time, the greatest advantage and is updated.

4. Program code

```#include <stdio.h>
#include <stdlib.h>

int if_OK(int c[], int K){  // Determine whether it is a solution
int iļ¼flag;
int arr[21] = {0};
for(i = 1; i <= K; i++){
arr[c[i]] += 1;
}
for(i = 1, flag = 1; i <= K; i++){
if(arr[i] != 1){
flag = 0;
break;
}
}
return flag;
}

int is_part(int c[], int K){ // Determine whether it is partial decomposition
int i, flag;
int arr[21] = {0};
for(i = 1; i <= K; i++){
arr[c[i]] += 1;
}

for(i = 1, flag = 0; i <= K; i++){ // Here and if_OK makes a difference
if(arr[i] > 1){
flag = 0;
break;
}
else if(arr[i] == 0){
flag = 1;
}
}
return flag;
}

int func(int c[], int f[21][21], int K){ // Calculate the sum of competition advantages of male and female athletes in the case of this solution
int i, count = 0;
for(i = 1; i <= K; i++){
count += f[i][c[i]];
}
return count;
}

int main()
{    int i, j;
int N;
scanf("%d", &N);

int p[21][21], q[21][21], f[21][21]; // p records the competitive advantage of male athletes, q records the competitive advantage of female athletes, and f is after the match of male and female athletes. And the array starts with 1.
for(i = 1; i <= N; i++){
for(j = 1; j <= N; j++){
scanf("%d", &(p[i][j]));
}
}
for(i = 1; i <= N; i++){
for(j = 1; j <= N; j++){
scanf("%d", &(q[i][j]));
f[j][i] = q[i][j] * p[j][i]; // Note the array subscripts of f, p and q here!!!
}
}

int com[21] = {0}; // It is used to record the match between male and female players. I in com [i] represents male players and com [i] represents female players
int k = 1, tmp = 0, count = 0; //k player number, tmp is the dominant value of the current solution, and count is the optimal dominant value

while(k >= 1 && k <= N){ // Note K < = n
while(com[k] <= N){
com[k] = com[k] + 1;
if(if_OK(com, N)){ // Indicates that the com [] is a set of solutions
tmp = func(com, f, N);
if(tmp > count) count = tmp; // Record optimal solution
break;
}
else if(is_part(com, N)) k = k+1; // Partial solution
}
com[k] = 0;
k = k-1;
}

printf("%d" , count);
return 0;
}

```

5. Operation results

Suppose n is the number of male and female athletes in the badminton team, and P[i][j] is the competitive advantage of male athletes in mixed doubles composed of male athletes I and female athletes j; Q[i][j] is the competition advantage of female athletes with the cooperation of female athletes I and male athletes j

This question adopts the method of male athletes choosing female athletes, which forms an arrangement tree. G represents female athletes, and the number of layers of the arrangement tree represents male athletes. For example, G1=20 on the first floor indicates that the competition advantage of male athlete No. 1 and female athlete No. 1 is 20.

6. Operation result analysis

The output result should be a collection of athlete numbers, represented by x [i]. The original array stores the initial number. After algorithm processing, the number conforming to the optimal value is output.
According to the permutation tree and the output results, the solution space of the strategy of male athletes selecting female athletes is {(1,2,3), (1,3,2), (2,1,3), (2,3,1), (3,1,2), (3,2,1)} the maximum competition advantage is 20 + 20 + 12 = 52, and the optimal set number is x={1,3,2}, that is, the combination of male No. 1 and female No. 1, male No. 2 and female No. 3, male No. 3 and female No. 2. Time complexity analysis: the algorithm needs to dynamically generate the permutation tree, spend O(1) time at each node, and the number of nodes in the permutation tree is n!, Therefore, the time complexity of the algorithm is O (n!).

Topics: Algorithm AI Graph Theory