Greedy algorithm (greedy algorithm)

greedy strategy

definition

The greedy strategy refers to that when solving the problem, the best or optimal (that is, the most favorable) choice is taken in each step of the selection, so as to hope to lead to the best or optimal algorithm.
Using a greedy strategy generally involves optimal substructure, and computing the best partial solution so far for each subproblem top-down . This also means that the solution of the greedy algorithm is not necessarily the optimal solution , and what is actually obtained may be a relatively approximate optimal solution.

When applying the greedy algorithm, we must first sort the problems according to the goals. Select the optimal subset according to the ordering.

for example

Such as the common station problem: insert image description here
Goal: The least number of stations, covering the largest area.
Define a coverage target set Y=[Beijing, Shanghai, Tianjin, Guangzhou, Shenzhen, Chengdu, Hangzhou, Dalian]
1. First, K1-K5, traverse how many areas are covered by these five stations: K1, K2, and K3 respectively cover 3 areas, and K4 and K5 respectively cover 2 areas. Then choose K1 to cover [Beijing, Shanghai, Tianjin], leaving Y=[Guangzhou, Shenzhen, Chengdu, Hangzhou, Dalian] not covered.
2. According to Y=[Guangzhou, Shenzhen, Chengdu, Hangzhou, Dalian], traverse how many areas are covered by the remaining K2-K5 stations: at this time, K2, K3, and K5 respectively cover 2 areas, and K4 covers 0 areas. Selecting K2 covers [Guangzhou, Shenzhen], leaving Y=[Chengdu, Hangzhou, Dalian].
Repeat the above operations until all areas are covered.

It can be seen from the above that only the optimal solution that can be obtained at a certain stage is selected.
Although the greedy algorithm may not necessarily obtain the optimal solution, it can at least obtain an approximate solution, which is simple and efficient.

Guess you like

Origin blog.csdn.net/qq_43842886/article/details/122531559