Algorithm thinking - greedy (detailed examples to understand~)

Algorithmic thinking
Greedy algorithm
Divide and conquer (recursive thinking)
Dynamic programming
Violent exhaustion

introduce

思考:
假设有四种硬币,面值分别为50元、10元、5元和1元。
现在要找给某顾客113元,你会怎么找?

Without thinking about it, we will choose the combination of two 50 yuan, one 10 yuan and three 1 yuan.

And we subconsciously used the coin finding algorithm:
1. First select a maximum denomination not greater than 113 yuan, that is, 50 yuan, and the remaining 63 are to be found.
2. Choose another maximum denomination not greater than 63 yuan, that is, another 50 yuan, and the remaining 13 is to be found 3.
Then choose a maximum denomination not greater than 13 yuan, that is, 10 yuan, and the remaining 3 is to be found
4. The remaining is 3 1 yuan coin

Every time we choose the coin with the largest value in the range, this method is actually a greedy algorithm.

basic knowledge

  • What is greed?
    Local optimum, without considering the overall optimum, takes the best or optimal (that is, the most favorable) choice in the current state in
    each step of selection , so as to hope that the global result is the best or optimal algorithm.

  • Features
    Advantages : low computational complexity required for making decisions
    Disadvantages : greedy, short-sighted, the final solution may not be the optimal solution

如果硬币的面值改为1元、5元和11元3种,需要找给顾客15元呢?

At this time, if we use the greedy strategy, the result should be 1 11 yuan + 4 1 yuan
, but in fact the optimal solution is: 3 5 yuan!
Therefore, the final solution obtained by greedy is not necessarily the optimal solution, but it may be an approximate optimal solution.

  • When to use greedy?
    1. Optimal substructure
      The problem is to solve the optimization, and solving this kind of problem needs to be completed through a series of solving sub-problems.
    2. Greedy choice property
      The overall optimal solution of a problem can be achieved through a series of local optimal solution choices, and each choice can depend on the previous choice, but not on the subsequent choice

Typical applications : looking for coins, interval scheduling (activity scheduling) problems, ordinary knapsack problems, multi-machine scheduling problems, Dijkstra, etc.

There are no fixed routines and question types. Greedy just provides a way to solve the problem. Manually simulate it. If you can’t find a counterexample, you can try greedy~

greedy three steps

Step 1: Identify what is the optimal solution
Step 2: Identify the optimal solution to the sub-problems ( emphasis: finding a strategy )
Step 3: Find the optimal solutions to the sub-problems separately, and then stack up the global optimal solution

example

Question:
There is a backpack with a capacity of M=150.
There are currently 7 items, and it is required to maximize the total value of the items in the backpack as much as possible, but the total capacity cannot be exceeded, and the items can be divided .
insert image description here

Three steps:
the first step: clarify what is the optimal solution
According to the question, in the case of selecting certain items, the weight range (total m <= 150) is not exceeded and the total value of the items is the largest

Step 2: Identify the optimal solution to the subproblem Subproblem
: Select an item each time and put it into the backpack
Subproblem optimal solution strategy:

  1. highest value
  2. Minimum weight
  3. The highest "value density"

Step 3: Find the optimal solution of the sub-problems separately, and then stack the global optimal solution

  1. When the "highest value" strategy is selected, it is calculated according to the rules (value),
    and the order is: DBFE, weight 130, value 165
  2. When the "minimum weight" strategy is selected, the calculation is performed according to the rules (weight),
    and the order is: FGBAE, weight 140, value 155
  3. When the "value density" strategy is selected, the calculation is performed according to the rules (weight),
    and the order is: FBGDA, weight 150, value 175

In summary, "Using value density to select items" is a local optimal solution strategy for this common knapsack problem.




What if we follow the greedy optimal strategy of the above knapsack problem and put it in 0-1 knapsack problem 1 ?

Topic:
The capacity of the existing knapsack is M=50. There are the following three items. The items are inseparable . How to maximize the total value of the items in the knapsack?

Item 1 Item 2 Item 3
weight 10 20 30
value 60 100 120

Item 1: 6 yuan/kg, item 2: 5 yuan/kg, item 3: 4 yuan/kg
According to the value density rule, item 1 is the highest. However, the preferred item 1 cannot obtain the optimal solution:
insert image description here

In the face of the 0-1 knapsack problem, no matter how good the greedy strategy is, there is no way to guarantee the optimal solution.

Because there is no guarantee that the backpack will be full in the end , the idle space of some backpack space will reduce the value of each kilogram of backpack space.
Pursuing high-value (cost-effective) items is what we want, but at the same time, it is also necessary not to waste too much backpack volume. needs

Example sharing

  1. 435-Non-overlapping interval (activity scheduling problem) ( Medium )
    https://leetcode-cn.com/problems/non-overlapping-intervals/
  2. 738-Monotonically increasing digits ( Medium )
    https://leetcode-cn.com/problems/monotone-increasing-digits/
  3. 435-Non-overlapping interval ( Medium )
    https://leetcode-cn.com/problems/non-overlapping-intervals/

Problem solution: leetcode problem solution - greedy


  1. In the 0-1 knapsack problem, there are often only two results for an item, either choose 1 or give up 0.
    When considering the selection of 0-1 backpack items, you should compare the final results of 0 and 1, and then make the best choice. This leads to many overlapping sub-problems. This is another important characteristic of this problem that can be solved by dynamic programming algorithm ↩︎

Guess you like

Origin blog.csdn.net/qq_29493173/article/details/123475875