Five algorithms 3-Greedy Algorithms

Source: Internet
Author: User
I. Basic concepts:Greedy algorithms mean that when solving a problem, they always make the best choice for the moment. That is to say, without considering the overall optimization, what he makes is only a local optimal solution in a certain sense. Greedy algorithms do not have a fixed algorithm framework. The key to algorithm design is the choice of greedy policies. It must be noted that the greedy algorithm does not obtain the overall optimal solution for all problems. The greedy strategy selected must be ineffective, that is, the process after a certain State will not affect the previous state, it is only related to the current status. Therefore, the greedy strategy must be carefully analyzed to determine whether it meets the problem of no aftereffect.
Ii. Basic Ideas of greedy algorithms:1. Create a mathematical model to describe the problem. 2. Divide the problem to several subproblems. 3. solve each subproblem and obtain the local optimal solution of the subproblem. 4. Merge the local optimal solution of the subproblem into a solution of the original solution.
Iii. Application of greedy Algorithms The premise for greedy policies is that local optimal policies can generate global optimal solutions. In fact, greedy algorithms are rarely used. Generally, if a problem analysis is applicable to a greedy algorithm, you can select the actual data under the problem for analysis to make a judgment.   IV. Implementation Framework of greedy AlgorithmsStarting from an initial solution of the problem; while (can take a step forward to the given overall goal) {use feasible decisions to find a solution element of the feasible solution ;} combine all solution elements into a feasible solution to the problem; 5. Selection of greedy policiesBecause the greedy algorithm can only achieve the global optimal solution by solving the local optimal solution, you must be sure to determine whether the problem is suitable for adopting the greedy algorithm policy and whether the obtained solution must be the optimal solution of the problem. Vi. Example AnalysisThe following is a question that can be solved by using the greedy algorithm. The greedy solution is indeed good, but unfortunately it is not the optimal solution. [Backpack problem] There is a backpack with a capacity of M = 150. There are 7 items, which can be divided into any size. Make sure that the total value of the items in the backpack is the maximum, but the total capacity cannot be exceeded. Item a B c d e f g weight 35 30 60 40 10 25 value 10 40 30 50 40 40 analysis: objective function: the maximum constraint of Σ pi is that the total weight of the loaded item cannot exceed the size of the backpack: Σ wi <= m (M = 150) (1) According to the greedy policy, is the best result of loading the most valuable items into a backpack? (2) Can I find the optimal solution for loading the item with the minimum weight? (3) Each time an item with the largest unit weight value is selected, it becomes a solution strategy. It is worth noting that the greedy algorithm is not completely unusable. Once proven, the greedy policy is an efficient algorithm. Greedy algorithms are still one of the most common algorithms. This is because they are easy to implement and it is not very difficult to construct greedy policies. Unfortunately, it must be proved before it can be applied to the question algorithm. In general, the proof of the greedy algorithm is centered around: the optimal solution of the entire problem must be obtained from the optimal solution of the sub-problem existing in the greedy policy. The three greedy strategies in the example cannot be established (cannot be proved). The explanation is as follows: (1) greedy strategy: select the value owner. Counterexample: W = 30 items: a B C weight: 28 12 12 value: 30 20 20 according to the policy, first select item A, then you cannot select another item. However, it is better to select B and C. (2) greedy policy: minimum weight. Its counterexamples are similar to those of the first policy. (3) greedy strategy: select the item with the largest unit weight value. Counterexample: W = 30 items: a B C weight: 28 20 10 value: 28 20 10 according to strategy, three items have the same weight value, and the program cannot make judgment based on the existing strategy, if a is selected, the answer is incorrect.

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.