Chapter 2 Evaluation Algorithm

xiaoxiao2021-03-06  80

The strategy for the successful use of the monarch and the colonists can also use the design process of high efficiency computer algorithms. This chapter will first introduce how to apply this ancient strategy in the algorithm design area, and then use this strategy to solve the following problems: minimum and largest issues, matrix multiplication, incontinence chessboard, sort, selection, and calculate a geometric issue - find 2D The nearest point in the space. This chapter gives a mathematical method for the complexity of algorithms to analyze points, and the algorithm for solving these two problems by deriving the minimum and biggest problems and sorting problems, the algorithm for solving these two problems (because the complexity and lower limit of algorithm) Consistency).

2.1 Algorithm Thoughts

The method of separation and treatment is very similar to the modular method of software design. In order to solve a big problem, you can: 1) divide it into two or more smaller issues; 2) Solve each small problem; 3) combination of the solution to each small problem, you can get the original problem answer. Small problems are usually similar to the original problems, and can be resolved in recursive use of dividends. Example 2-1 [Find a false coin] Give you a bag with 1 6 coins. One of the 6 coins is forged, and the forged coin is more than the real coin. Your task is to find this forged coin. To help you complete this task, you will provide an instrument that can be used to compare the weight of the two groups, using this instrument, you can know whether the weight of the two sets of coins is the same. Compare the weight of the coin 1 and the coin 2. If the coin 1 is light than the coin 2, the coin 1 is forged; if the coin 2 is light than the coin 1, the coin 2 is forged. This completes the task. If the weight is equal to the weight of the two coins, the coin 3 and the coin 4 are compared. Similarly, if there is a hard coin, find the task of counterfeit currency. If the weight is equal to the weight of the two coins, the coin 5 and the coin 6 continue to be compared. According to this way, you can judge the presence of the countercort with up to 8 comparisons and find out this counter. Another method is to use the method of separation and treatment. If you see an example of 1 6 coins as a big problem. The first step is divided into two small problems. Eight coins were randomly selected as the first set as a group A, and the remaining 8 coins were called Group B as the second group. In this way, the problem of 1 6 coins is divided into two 8 coins. In the second step, it is determined whether there is a counterfeit coin in the A and B groups. The instrument can be used to compare the weight of the group A coins and B coins. If the weight of the two groups is equal, it can be judged that the counterfeit currency does not exist. If the weight of the two groups is not equal, there is a counterfeit currency, and it can be judged in a lighter set of coins. Finally, in the third step, the result of the original 1 6 coin issues was obtained with the result of the second step. If only the coins are exist, the third step is very simple. Regardless of the group A or counter group, there are counterfeit coins in this 1 6 coins. Therefore, only the 伪 币 is exist only through a comparison of one weight. Now suppose you need to identify this pseudo coin. Use the situation of two or three coins as a non-scaible small problem. Note If there is only one coin, it cannot be judged whether it is counterfeit. In a small problem, you can find a countercurian by comparing a coin with other two coins, you can find a counter. In this way, the problem of 1 6 coins is divided into two 8 coins (group A and Group B). By comparing the weight of these two sets of coins, it can be judged whether or not the counterfeit coin exists. If there is no counterfeit coin, the algorithm terminates. Otherwise, continue to divide these two groups of coins to find counterfeit coins. Suppose B is a light group, so dividing it into two groups, with 4 coins per group. The group is called B1, and the other group is B2. Comparing these two groups, there must be a set of light. If B1 is light, the counterfeit coin is in B1, and B1 is divided into two groups, each group has two coins, called one of the groups of B1A, and another group is B1B. Compare these two groups, you can get a lighter group. Since this group only has two coins, it is no longer necessary. If the weight of the two coins in the group, you can immediately know which coin is light. Lighter coins are the counterfeit coins to find. Example 2-2 [Gold block problem] There is a boss with a bag of gold. Two employees will be rewarded a gold block for each month. According to the rules, ranking first employees will get the heaviest gold block in the bag, and the second employee will get the lightest gold block in the bag. According to this approach, the gold block obtained by the first employee is always a gold block that is better than the second employee. If there is a new golden block periodic joining bag, you must find the lightest and heaviest gold blocks every month. Suppose there is a comparative weight instrument, we want to find the most lightweight gold block with the least comparison. Assume that there is n gold block in the bag.

The heaviest gold block can be found with the function M a x (program 1 - 3 1) by n-1 time. After finding the heaviest gold block, you can find the lightest gold block by N-2 comparison from the remaining N-1 gold block. In this way, the total number of compared is 2N-3. Procedures 2 - 2 6 and 2 - 2 7 are other methods, the former needs 2n-2 comparisons, and the latter requires up to 2N-2 comparisons. The following is solved by the method of dividing the way. When N is small, for example, n ≤ 2, identify the heaviest and the lightest gold block, it is enough. When N is large (n> 2), the first step is divided into two pouchs A and B. In the second step, identify the most heavy and lightest gold blocks in A and B. The maximum and lightest gold blocks in A are HA and LA, respectively, in such push, the heavier and the lightest gold blocks in B are HB and LB. In the third step, by comparing HA and HB, you can find the most important of all gold blocks; by comparing LA and LB, you can find the lightest in all gold blocks. In the second step, if n> 2, the method of recursively apply a division. Assume n = 8. This bag is fracted two bags A and B with 4 gold blocks. In order to find the most heavy and lightest gold blocks in A, the four gold blocks in A are divided into two groups A1 and A2. Each group has two gold blocks that can be used in a comparison to find a heavier gold block HA1 and a lighter gold block LA1 in A. After another comparison, HA 2 and LA 2 can be found. It is now possible to find HA2, and LA can be found by comparing La 1 and La2. Thus, HA and LA can be found by 4 comparisons. Also need four comparisons to determine HB and LB. By comparing HA and HB (LA and LB), you can find the most weight and lightest in all gold blocks. Therefore, when n = 8, this method of specifically consists of 1 0 times. If you use programs 1 - 31, you will need 1 3 comparisons. If you use programs 2 - 2 6 and 2 - 2 7, you will need to 1 4 times comparisons. Set C (N) to compare the number of times required for the use of dividends. For the sake of easy, it is assumed that n is 2 power. When n = 2, c (n) = 1. For larger N, C (N) = 2c (n / 2) 2. When N is a power of 2, C (N) = 3 N / 2 - 2 can be used using an iterative method (see Example 2 - 2 0). In this example, the method of using a division is less than a comparison of 2 5% less than the method of comparing one by one. Example 2-3 [Matrix Multiplication] The product of the two N × N-order matrices A and B is another N × N-order matrix C, C can be represented as if each C (I, J) is calculated using this formula. Then calculate the number of operations required to calculate N3 m N2 (N- 1) a, where m represents a multiplication, and a represents an addition or subtraction. In order to obtain two matrix multiply, the algorithm is required, it is required: 1) Define a small problem and indicate how the small problem is multiplied by multiplication; 2) Determine how to divide a large problem into a smaller problem, and indicate how Multiplication of these smaller issues; 3) Finally, it is pointed out how to get a big problem based on the results of small problems. In order to make the discussion, it is assumed that n is 2 power (that is, N is 1, 2, 4, 8, 1 6 ,.). First, assume that n = 1 is a small problem, n> 1 is a big problem. This hypothesis will be modified at any time as needed. For a 1 × 1-order small matrix, it can be obtained by multiplying two elements in the two matrices.

Expected a big problem with n> 1. Such a matrix can be divided into four N / 2 × N / 2 order matrices A1, A2, A3, and A4. When N is greater than 1 and N is power, n / 2 is also a power of 2. Therefore, the smaller matrix also meets the assumptions of the front pair of matrices. The definition of matrices BI and CI is similar to this. According to the above formula, the addition of the N / 2 × N / 2-order matrix multiplication and 4 N / 2 × N / 2-order matrix can be calculated, and the A and B can be calculated. product. Therefore, these formulas can help us achieve the algorithm of university. In the second step of the algorithm, the recursive use is divided into the algorithm to re-subdivide 8 small matrices (see programs 2 - 1 9). The complexity of the algorithm is (N3), which is the same as the complexity obtained by the procedure 2 - 2 4 directly using the formula (2 - 1). In fact, due to the additional overhead of matrix segmentation and re-combination, the calculation of the calculation of the calculation is taken for the results of the results of the procedure 2 - 2 4. To get a faster algorithm, you need to simplify the matrix segmentation and re-combination of these two steps. One solution is to obtain seven small matrices using the S T R A S e n method. These seven small matrices are matrices D, E,., J, Matrix D to J can be calculated by 7 matrix multiplication, 6 matrix plus method, and 4 matrix subtraction. The four small matrices described above can be derived from matrix D to J by 6 matrix addition and twice matrix subtraction. The matrix multiplication of n = 2 is solved by the above scheme. The result of the result of a matrix A and B, as shown below: Because n> 1, the two matrices of A, B are divided into four small matrices, each matrix is ​​1 × 1 order, only one element contains only one element. The multiplication of 1 × 1st order matrix is ​​a small problem, so it can be used directly. Util D ~ J formula, obtained: D = 1 (6-8) = - 2e = 4 (7-5) = 8f = (3 4) 5 = 3 5g = (1 2) 8 = 2 4h = (3-1) (5 6) = 2 2i = (2-4) (7 8) = - 3 0J = (1 4) (5 8) = 6 5 According to the above results: for The 2 × 2 example above, the algorithm of using the division is required for 7 multiplication and 1 8 addition / subtraction operations. The formula (2 - 1) is used directly, it takes 8 multiplication and 7 addition / subtraction. To make the algorithm of separation, the time spent in one multiplication must be longer than the time of 11 addition / subtraction. It is assumed that the S t R A S S e n matrix segmentation scheme is only used for matrix multiplication of N ≥ 8, and the matrix multiplication of N <8 is calculated directly by formula (2 - 1). At n = 8, 8 × 8 matrix will take 7 times 4 × 4 matrix multiplication and 1 8 times 4 × 4 matrix plus / subtraction. Each matrix multiplication requires 6 4m 4 8A operation, each matrix addition or subtraction takes 1 6A operation. Therefore, the total number of operations is 7 (6 4m 4 8a) 1 8 (1 6a) = 4 4 8m 6 2 4A. The direct calculation method is used, then 5 1 2m 4 4 8A operation is required. To make the S t R A S S E n method is fast than the direct calculation method, at least 5 1 2-4 4 8 multiplication overhead ratios 6 2 4-4 4 8 times / subtraction is large. Or the overhead of a multiplication should be greater than approximately 2. 7 5 times / subtraction overhead.

It is assumed that the matrix of N <1 6 is a "small" problem, and the decomposition scheme of S T R a S e n is for only N ≥ 1 6, and the matrix of N <1 6 is multiplied, and the formula (2 - 1) is directly utilized. The algorithm is used when n = 1 6 is used to treat 7 (5 1 2M 4 4 8A) 1 8 (6 4A) = 3 5 8 4m 4 2 8 8A operation. Directly calculate 4 0 9 6m 3 8 4 0A operation. If the overhead of a multiplication is the same as the overhead of the plus / subtraction, the S TRASSEN method requires 7 8 7 2 operations and an additional time for problem decomposition, and the direct calculation method requires 7 9 3 6 times operation plus program. Execute the time spending for the FOR loop and other statements. Even if the number of operations required by the direct calculation method is less than the ST R A S e n method, but due to the direct calculation method requires more additional overhead, it is not necessarily fast than S T R a S e n method. The larger the value of n, the larger the number of operations used by the Strassen method and the direct calculation method, so there is a large enough N, the Strassen method will be faster. Set T (n) means the time required to use Strassen divide the method. Since the large matrix is ​​recursively divided into a small matrix until the size of each matrix is ​​smaller than or equal to k (k at least 8, maybe more, the specific value is determined by the performance of the computer). Calculate it with an itery method, available T ( n) = (NL OG27). Because L og27 ≈2. 8 1, the matrix multiplier algorithm of separation and treatment is larger than the complexity (N3) of the direct calculation method. Precautions is naturally caused by the use of recursive algorithms. In many examples, these recursive algorithms have been well used in the recursive program. In fact, in many cases, all attempts to get a non-recursive program will result in analog recursive stack. However, in some cases, it is also possible to use a non-recursive program to accomplish points without using such a rendering stack, and in this manner, the speed of the program is faster than the recursive mode. Separate the separation of gold block problems (Example 2 - 2) and Mental Sort Method (2.3) The recursive procedure can be done faster through a non-recursive program. Example 2-4 [Gold Block Problem] Use Example 2 - 2 Algorithm to find the work of the lightest and maximum gold block in 8 gold blocks can be represented by a binary tree. The leaves of this tree represent eight gold blocks (A, B,., H), each shaded node representing a problem with all leaves in their subtots. Therefore, root node a means the problem of looking for the lightest and most heavy gold blocks in 8 gold blocks, and Node B represents the lightest and most heavy gold blocks in 4 gold blocks of A, B, C, and D. The algorithm starts from the root node. 8 gold block issues represented by root nodes are divided into two 4 gold block problems represented by nodes B and C. At the node, 4 gold block issues are divided into two gold block problems represented by D and E. Can solve the 2 gold block issues represented by the D node by comparing the gold blocks A and B. After solving the problems represented by D and E, the problem represents B expressed by comparing the light gold blocks and heavy gold blocks found in D and E. The process then repeats this process on F, G, and C, and finally solve the problem A. The algorithm that can be subjected to recursive division can be divided into the following steps: 1) From the binary tree in Figures 2 - 2, a large problem is divided into many small problems in the process of root to the leaves, and the size of the small problem is 1 or 2.2. Compare the gold blocks in each size of 2, which is determined which one is heavy and lighter. This comparison is done on node D, E, F and G. There is only one gold block in the problem of 1, which is both the lightest gold block and the heaviest gold block. 3) Compare the lighter gold block to determine which gold block is lighter, compare heavier gold blocks to determine which gold block is the most. This comparison is performed on nodes A to C.

According to the above steps, the non-recursive code of the program 1 4 - 1 can be obtained. The program is used to find the minimum and maximum number in the array W [0: N - 1], if n <1, then the program returns F A L s E, otherwise returns T R u e. When N ≥ 1, the program 1 4 - 1 gives M i N and M A X initial value to make W [m i n] are minimal weight, W [M A x] is the maximum weight. Firstly, the case of N ≤ 1 is processed. If n> 1 is odd, the first weight W [0] will become a minimum and maximum value of the candidate value, so there will be even number of weight values ​​W [1: n - 1] to participate in the F O R loop. When N is an even number, the two weight values ​​are first placed outside the FOR cycle, and the smaller and large weight values ​​are set to MIN and MAX, so there is also an even number of weight values ​​W [2: N-1] participation. FOR cycle. In the FOR cycle, the outer IF is larger and smaller in the comparison (W [i], W [i 1]). This work corresponds to 2 in the algorithm step mentioned in the previously mentioned algorithm step, and the inner layer I F is responsible for identifying the minimum and maximum value in a smaller weight value and a large weight value, which corresponds to 3). The FOR cycle compares the smaller value and larger values ​​in each pair of weight values ​​and the current minimum W [m in] and maximum value W [m ax], modify M IN and M AX depending on the comparison result. necessary). The complexity analysis is performed below. It is noted that when N is an even number, 3 (n / 2 - 1) time is performed inside the F O r cycle outside the FO-cycle, and the total number of times is 3 N / 2 - 2. When N is odd, the F O R cycle is not performed, and 3 (N-1) / 2 comparisons are performed inside. Therefore, regardless of N is an odd or even, when N> 0, the total number of comparisons is "3N / 2ù-2 times. Program 14-1 finds the minimum and maximum non-recursive program TemplateBool MinMax (T W [], INT N, T & Min, T & MAX

{// Looking for the minimum and maximum values ​​in W [0: N - 1]

// If less than one element, return F a L S e

// Special circumstances: n <= 1

IF (n <1) returnaf false;

IF (n == 1) {min = max = 0;

Return True;}

/ / Initialize MIN and M A X

INT S; // cycle starting point

IF (n% 2) {// n is odd

Min = max = 0;

S = 1;}

Else {// n is an even number, compared to the first pair

IF (w [0]> w [1]) {

Min = 1;

MAX = 0;}

Else {min = 0;

MAX = 1;}

S = 2;}

/ / More than the remaining number

For (int i = S; i

// Looking for a larger in W [i] and W [i 1]

/ / Then compare the larger with W [m a x]

// Compare more than W [m i n]

IF (w [i]> w [i 1]) {

IF (w [i]> w [max]) max = i;

IF (w [i 1]

Else {

IF (W [i 1]> w [max]) max = i 1;

IF (W [i]

}

Return True;

}

Exercise

1. Expand the algorithm of the division of Example 1 4 - 1 to N> 1 coin. How many times is needed? 2. Consider the counterfeit coin problem of Example 1 4 - 1. Assume that the condition "counterfeit currency is light" to "counter the weight of the counter" counterfeit coins and the solids ", and there are N coins in the form of fixed bags.

1) The formal description of the algorithm for the corresponding scoring is given, the algorithm can output information "There is no counterfeit currency" or find a counterfeit currency. The algorithm should be recursively divided into two smaller issues. How many times needed to find countercoin (if there is a counterfeit currency)?

2) Repeat 1), but divide the big problem into three smaller problems.

3. 1) Write a C program, implement two options for the maximum and minimum values ​​in n elements in Example 1 4 - 2. Use recursive to complete the scope.

2) Program 2 - 2 6 and 2 7 is the other two code to find the maximum and minimum value in n elements. Try each program requires the minimum and maximum number of times required for each program.

3) In the case where n is equal to 1 0 0, 1 0 0 or 10,000, the runtime of the programs and procedures 1 4 - 1 in comparison 1), 2), respectively. For programs 2 - 2 7, the average time and the worst time are used. 1) The programs and procedures 2 - 2 6 should have the same average time and the worst time.

4) Note that if the overhead of the comparison operation is not very high, the algorithm of divide and the algorithm is not superior to other algorithms in worst cases? Is it average time than the program 2 - 2 7? why?

4. Proof of directly using formula (1 4-2) to (1 4 - 5) to result in the result of the result of the resulting matrix multiplication to the algorithm (N3). Therefore, the corresponding division can be slower than the program 2 - 2 4.

5. Use iterative methods to prove that the recursive value of the formula (1 4 - 6) is (N L OG27).

* 6. Write the S T R A S S e n matrix multiplication program. Experiments were performed using different K values ​​(see formula (1 4 - 6)) to determine how the procedural performance is optimal when K is. Comparison procedures and runtime 2 - 2 4. It can be compared to the power of N is 2.

7. When n is not a power of 2, a matrix of a power of 2 can be obtained by increasing the rows of the matrix and columns. Assume that the number of rows and columns use the number of columns into M-order matrix, where M is 2 power.

1) Ask M / N.

2) Which matrix items can be used to form new rows and columns to multiply the new matrices A 'and B', the result of the original matrix A and B multiply the top left corner of C '?

3) Calculate the time required for A '* b' (m2.81) using the S T R a S S E n method. A runtime expression with N is variable is given.

Reprinted from Shada Studio

转载请注明原文地址:https://www.9cbs.com/read-108014.html

New Post(0)