The third of the five commonly used algorithms: greedy algorithm

how are you

1. Basic concepts:
 
      The so-called greedy algorithm means that when solving a problem, it always makes the best choice at present . That is to say, instead of considering the overall optimality, what he has made is only a local optimal solution in a certain sense .
     The greedy algorithm has no fixed algorithm framework, and the key to algorithm design is the choice of greedy strategy. It must be noted that the greedy algorithm cannot obtain the overall optimal solution for all problems, and the selected greedy strategy must have no aftereffect, that is, the process after a certain state will not affect the previous state, only related to the current state.
    Therefore, the adopted greedy strategy must be carefully analyzed whether it satisfies no aftereffect.

Second, the basic idea of ​​​​the greedy algorithm:
    1. Build a mathematical model to describe the problem.
    2. Divide the problem to be solved into several sub-problems.
    3. Solve each sub-problem to obtain the local optimal solution of the sub-problem.
    4. Combine the local optimal solutions of the sub-problems into a solution of the original solution.

Third, the problem of greedy algorithm application
      The premise of the greedy strategy is that the local optimal strategy can lead to the global optimal solution.
    In practice, greedy algorithms are applicable in very few cases . Generally, to analyze whether a problem is suitable for the greedy algorithm, you can first select several actual data under the problem for analysis, and then you can make a judgment.
 
Fourth, the implementation framework of the greedy algorithm
    starting from an initial solution to the problem;
    while (can take one step towards a given overall goal)
    { 
          Use feasible decisions to find a solution element of a feasible solution;
    }
    A feasible solution to the problem is composed of all solution elements;
  
Five, the choice of greedy strategy
     Because the greedy algorithm can only achieve the global optimal solution through the strategy of solving the local optimal solution, we must pay attention to determine whether the problem is suitable for the greedy algorithm strategy, and whether the found solution must be the optimal solution of the problem.
 
Six, example analysis
    The following is a problem that can be solved by the greedy algorithm. The greedy solution is really good, but unfortunately it is not the optimal solution.
    [Backpack problem] There is a backpack, and the backpack capacity is M=150. There are 7 items and items can be split into any size.
    The requirement is to maximize the total value of items loaded into the backpack as much as possible, but not exceed the total capacity.
    Goods ABCDEFG
    Weight 35 30 60 50 40 10 25
    Value 10 40 30 50 35 40 30
    analyze:
    Objective function: ∑pi maximum
    The constraint condition is that the total weight of the loaded items does not exceed the backpack capacity: ∑wi<=M( M=150)
    (1) According to the greedy strategy, each time the item with the greatest value is selected and loaded into the backpack, is the result optimal?
    (2) Can the optimal solution be obtained by picking the item with the smallest weight each time?
    (3) Each time the item with the greatest value per unit weight is selected as the strategy for solving this problem.
    It is worth noting that the greedy algorithm is not completely unusable. Once the greedy strategy is proved, it is an efficient algorithm.
    The greedy algorithm is still one of the most common algorithms, because it is simple and easy to implement, and it is not very difficult to construct a greedy strategy.
    Unfortunately, it needs to be proved before it can actually be used in the algorithm of the problem.
    Generally speaking, the proof of greedy algorithm revolves around: the optimal solution of the whole problem must be obtained by the optimal solution of the sub-problems that exist in the greedy strategy.
    For the three greedy strategies in the example, all cannot be established (cannot be proved), and the explanation is as follows:
    (1) Greedy strategy: choose the one with the greatest value. Counter example:
    W=30
    Item: ABC
    Weight: 28 12 12
    Value: 30 20 20
    According to the strategy, choose item A first, and then you can't choose it again, but it is better to choose B and C.
    (2) Greedy strategy: choose the smallest weight. Its counterexample is similar to that of the first strategy.
    (3) Greedy strategy: select the item with the greatest value per unit weight. Counter example:
    W=30
    Item: ABC
    Weight: 28 20 10
    Value: 28 20 10
    According to the strategy, the unit weight value of the three items is the same, and the program cannot make a judgment based on the existing strategy. If A is selected, the answer is wrong.

Reference: http://www.cnblogs.com/steven_oyj/archive/2010/05/22/1741375.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324664717&siteId=291194637