C Language algorithm

Source: Internet
Author: User
Tags comparison continue require

The successful use of divide and conquer strategies by monarchs and colonists can also be applied to the design of efficient computer algorithms. This chapter will first introduce how to apply this ancient strategy in algorithmic design, and then use this strategy to solve the following problems: Minimum maximum problem, matrix multiplication, incomplete chessboard, sorting, selection and calculation of a geometric problem--to find two points nearest to the two-dimensional space.

This chapter gives a mathematical method for analyzing the complexity of the divide and conquer algorithm, and proves that the divide-and-conquer algorithm is optimal for solving these two problems by deriving the minimum maximum problem and the lower complexity of the scheduling problem (because the complexity of the algorithm is consistent with the lower limit).

2.1 Algorithm thought

The divide-and-conquer approach is very similar to the modular approach to software design. In order to solve a big problem, you can: 1 to divide it into two or more smaller problems, 2 to solve each small problem, 3, the small problem of the combination of solutions, you can get the answer to the original question. Small problems are usually similar to the original problem and can be solved recursively using the divide-and-conquer strategy.

Example 2-1 [find counterfeit coins] give you a bag containing 1 6 coins. One of the 1 6 coins was forged, and the counterfeit coins were lighter than the real coins. Your task is to find this forged coin. To help you complete this task, you will be provided with an instrument that compares the weights of the two groups of coins, using this instrument to know whether the weights of the two groups of coins are the same. Compare the weight of the coin 1 with the coin 2. If the coin is 1 lighter than the 2, then the coin 1 is forged, and if the coin is 2 lighter than the coin 1, then the coin 2 is forged. This completes the task. If the two coins are of equal weight, compare the coins 3 and the coins 4. Similarly, if a coin is lighter, the task of looking for counterfeit coins is complete. If the two coins are equal in weight, continue to compare the coins 5 and the coins 6. In this way, a maximum of 8 comparisons can be used to determine the existence of counterfeit coins and to identify them.

Another approach is to use the divide-and-conquer approach. Suppose the example of 1 6 coins is a big problem. The first step is to divide the problem into two small problems. Randomly selected 8 coins as the first group called Group A, the remaining 8 coins as the second group called Group B. In this way, the problem of dividing 1 6 coins into two 8 coins is solved. The second step is to determine whether there are counterfeit coins in groups A and B. Instruments can be used to compare the weights of Group A and group B coins. If the two groups of coins are equal in weight, you can judge that counterfeit coins do not exist. If the two groups of coins are not equal in weight, there are counterfeit coins and can be judged to be in the lighter group of coins. Finally, in the third step, the answer to the original 1 6 coins was obtained with the result of the second step. If you only judge whether a coin exists, the third step is very simple. Whether there are counterfeit coins in Group A or group B, it can be inferred that there are counterfeit coins in the 1 6. Therefore, the existence of counterfeit coins can be judged only by comparison of one weight at a time.

Now suppose you need to identify the counterfeit. The case of two or three coins as a minor problem that cannot be divided. Note that if there is only one coin, then you cannot tell if it is counterfeit. In one small problem, counterfeit coins can be found by comparing a coin with two other coins, up to two times. In this way, the problem of 1 6 coins is divided into two 8 coins (Group A and Group B). By comparing the weights of the two groups of coins, the existence of counterfeit coins can be judged. If there are no counterfeit coins, the algorithm terminates. Otherwise, continue to divide the two groups of coins to find counterfeit coins. Suppose B is the lighter group, so divide it into two groups with 4 coins per group. It is said that one group is B1 and the other group is B2. Compare these two groups, there must be a group of lighter. If the B1 light, then the counterfeit coins in the B1, then the B1 into two groups, each group has two coins, said that one group of B1A, the other group for B1b. Comparing these two groups, you can get a lighter group. Since this group has only two coins, it does not have to be subdivided. Compare the weights of the two coins in the group, and you can immediately know which coin is lighter. The lighter coin is the counterfeit coins to be found.

Example 2-2 [bullion problem] There's a boss with a bag of nuggets. Each month, two employees will be rewarded with a gold nugget for their outstanding performance. By rule, the first employee will get the heaviest nugget in the bag, and the second employee will get the lightest nugget in the bag. In this way, unless a new nugget is added to the bag, the first employee gets a gold nugget that is always heavier than that of the second employee. If a new nugget is periodically added to the bag, the lightest and heaviest nuggets must be found each month. Assuming there is a weight-measuring instrument, we want to find the lightest and heaviest nuggets with the least number of comparisons.

Suppose there are N gold nuggets in the bag. You can use the function m a x (program 1-3 1) to find the heaviest nugget by n-1 comparison. After finding the heaviest nugget, a similar method can be used to find the lightest nugget from the remaining n-1 nuggets using a n-2 comparison. In this way, the total number of comparisons is 2n-3. Programs 2-2 6 and 2-2 7 are two other methods that require 2n-2 comparisons, which require a maximum of 2n-2 times.

This problem is solved by the divide-and-conquer method below. When n is an hour, say, n≤2, identify the heaviest and lightest nuggets, a comparison is sufficient. When n is larger (n>2), the first step is to divide the bag of nuggets into two small bags A and B. The second step is to find the heaviest and lightest nuggets in A and B respectively. The heaviest and lightest nuggets in A are Ha and LA, and so on, the heaviest and lightest nuggets in B are HB and LB respectively. The third step is to find the heaviest of all gold nuggets by comparing HA and HB, and by comparing LA and lb, you can find the lightest of all the nuggets. In the second step, if n>2, the divide and conquer method is applied recursively.

Suppose n= 8. The bag is divided into two bags A and B, each with 4 gold nuggets. To find the heaviest and lightest nuggets in a, the 4 gold nuggets in A are divided into two groups of A1 and A2. Each group has two gold nuggets, and a comparison can be made to find heavier nuggets HA1 and lighter gold LA1 in a. After another comparison, we can find Ha 2 and La 2. Now, by comparing HA1 and HA2, we can find the HA; Find La by comparing LA 1 and LA2. In this way, ha and La can be found through 4 comparisons. Another 4 comparisons are needed to determine HB and LB. By comparing HA and HB (LA and LB), you can find the heaviest and lightest of all the nuggets. Thus, when n= 8 o'clock, this method of divide and conquer takes 1 0 comparisons. If you use the program 1-3 1, you need to compare 1 3 times. If you use Programs 2-2 6 and 2-2 7, you need a maximum of 1 4 comparisons. Set C (n) The number of comparisons required to use the divide-and-conquer method. For simplicity, suppose N is a power of 2. When n= 2 o'clock, C (n) = 1. For the larger n,c (n) = 2c (N/2) + 2. When n is the power of 2, the iteration method is used (see example 2-2 0)

C (N) = 3N/2-2. In this case, the divide-and-conquer method uses less than 2 5% comparisons compared to one-by-one comparison.

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.