[Data Structure and Algorithm] -> Algorithm -> Greedy Algorithm

Ⅰ Preface

The idea of ​​Greed Algorithm is actually a very common idea in life. Greed algorithm itself has many classic applications, such as Huffman Coding, Prim and Kruskal minimum spanning tree algorithm, Dijkstra single source shortest path algorithm. The minimum spanning tree and the shortest path will be introduced in my later article. Huffman coding has also been discussed in my previous article. Today we will look at it from the perspective of greedy algorithms.

[Data Structure and Algorithm] -> Data Structure -> Huffman Tree -> Huffman Encoding & Decoding

Ⅱ Understanding of Greedy Algorithm

Regarding the greedy algorithm, let's look at an example first.

Suppose we have a backpack that can hold 100kg of items and can hold various items. We have the following 5 kinds of beans. The total amount and total value of each kind of beans are different. In order to maximize the value of the items in the backpack, how do we choose which beans to pack in the backpack, and how many kinds of beans should be loaded What?

Insert picture description here
In fact, this problem is not very complicated. We just need to calculate the unit price of each item first, sort them from high to low, and then install them in this order. The unit price is arranged from high to low as: black beans, mung beans, red beans, green beans, and soybeans. Therefore, we can pack 20kg of black beans, 30kg of mung beans, and 50kg of red beans in our backpack.

The idea of ​​solving this problem is relatively easy to think of, and its essence is the greedy algorithm. Combining this example, I summarize the steps of the greedy algorithm to solve the problem.

The first step , when we see this kind of problem, we must first think of the greedy algorithm: for a set of data, we define the limit value and the expected value, and hope to select a few data from them. When the limit value is met, Expect the greatest value.

Corresponding to our example, the limit value is that the weight cannot exceed 100kg, and the expected value is the total value of the item. This set of data is 5 kinds of beans, we select a part of them, the weight does not exceed 100kg, and the total value is the largest.

In the second step , we try to see if this problem can be solved with a greedy algorithm: each time we select the current situation, the data that contributes the most to the expected value under the same amount of contribution to the limit value .

Corresponding to the previous example, each time we select the beans with the highest unit price from the remaining beans, that is, the beans that contribute the most to the value under the same weight.

In the third step , we give a few examples to see if the results produced by the greedy algorithm are optimal. In most cases, just give a few examples to verify it. Strictly proving the correctness of a greedy algorithm is very complicated and requires a lot of mathematical reasoning. Moreover, from a practical point of view, most of the problems that can be solved by greedy algorithms, the correctness of greedy algorithms is obvious, and does not require strict mathematical derivation to prove.

In fact, the idea of ​​using greedy algorithms to solve problems does not always give an optimal solution. Let me give an example.

In a weighted graph, we start from vertex S and find the shortest path to vertex T (the sum of the weights of the edges in the path is the smallest). The solution of the greedy algorithm is to select an edge with the smallest weight connected to the current vertex every time until the vertex T is found. According to this idea, the shortest path we find is S->A->E->T, and the path length is 1 + 4 + 4 = 9.

Insert picture description here
However, with this greedy selection method, the final path is not the shortest path, because the path S->B->D->T is the shortest path, because the length of this path is 2 + 2 + 2 = 6. So why doesn't the greedy algorithm work on this issue?

The reason is that the previous choices will affect the subsequent choices. If we walk from vertex S to vertex A in the first step, then the vertices and edges facing next will follow the first step from vertex S to vertex B, which is completely different. Therefore, even if we choose the best move in the first step (the shortest side), it is possible that because of this choice, the choices in each subsequent step are very bad, and ultimately we will not get the optimal solution.

Ⅲ Actual analysis of greedy algorithm

For greedy algorithms, you may still be confused. It is really difficult to understand thoroughly if you die. The key to mastering the greedy algorithm is to practice more. So, let's analyze a few specific examples to help you understand the greedy algorithm more deeply.

1. Divide candy

We have m candies and n children. We are now going to distribute candies to these children, but there are fewer candies and more children (m <n), so candies can only be allocated to some children.

The size of each candy varies, and the sizes of the m candy are s1, s2, s3,..., sm. In addition, each child has different needs for the size of the candy. Only the size of the candy has a different demand for the candy. The child will be satisfied only when the size of the candy is greater than or equal to the child's need for the candy size. . Assume that the needs of the n children for the size of the candy are g1, g2, g3, ..., gn.

So, how to distribute candies to satisfy the largest number of children possible?

We can abstract this problem as: from n children, select a part of the children to allocate candies, so that the number of children (expected value) satisfied is the largest. The limit value for this problem is the number of candies m.

We now look at how to use the greedy algorithm to solve. For a child, if small candies can be satisfied, we don't need to use bigger candies, so that bigger candies can be reserved for other children who have a greater need for candies. On the other hand, children with small needs for candies are more likely to be satisfied. Therefore, we can distribute candies from children with small needs, because satisfying a child with a large need and meeting a child with a small need can contribute to our expectations. all the same.

From the remaining children each time, we find the smallest demand for the size of the candy, and then send him the smallest candy that can satisfy him among the remaining candies. The distribution plan obtained in this way is the number of satisfied children. The most programs.

2. Coin change

This problem is more common in our daily lives. Suppose we have 1 yuan, 2 yuan, 5 yuan, 10 yuan, 20 yuan, 50 yuan, and 100 yuan banknotes, the number of which are c1, c2, and c5. , c10, c50, c100. We are now going to use this money to pay K yuan. What is the minimum bill?

In life, we must first use the largest face value to pay, if it is not enough, we will continue to use a smaller face value, and so on, and finally use 1 yuan to make up the rest.

When the contribution value is the same as the expected value (number of banknotes), we hope to contribute more points so that the number of banknotes can be reduced. This is a greedy algorithm solution.

3. Interval coverage

Suppose we have n intervals, and the start and end points of the interval are [l1, r1], [l2, r2], [l3, r3],..., [ln, rn], respectively. We select a part of the interval from these n intervals. This part of the interval satisfies the disjointness of each pair (the case where the endpoints intersect is not counted as an intersection). How many intervals can be selected at most?

Insert picture description here
The idea of ​​handling this problem is not so easy to understand, but this idea is used in many greedy algorithm problems, such as task scheduling, teacher scheduling, etc., so it is still very important, I hope everyone can understand.

The solution to this problem is as follows: We assume that the leftmost end point of these n intervals is lmin, and the rightmost end point is rmax. The problem is that we select several disjoint intervals and cover [lmin, rmax] from left to right. We sort the n intervals in the order of starting endpoints from small to large.

Every time we select, the left end point does not coincide with the previously covered interval, and the right end point is as small as possible, so that the remaining uncovered interval can be made as large as possible, and more intervals can be placed. This is actually a greedy choice method.

Insert picture description here

Ⅳ Greedy algorithm in life

We mentioned Huffman coding above. In fact, in my next article, Huffman coding has been explained in detail, so I won't repeat it.

[Data Structure and Algorithm] -> Data Structure -> Huffman Tree -> Huffman Encoding & Decoding

In fact, Huffman codes and the well-known Morse cipher are all manifestations of greedy algorithms. They all hope to replace the most frequently occurring characters with the shortest codes, which can save a lot of space.

When we look at daily life, whether it is the country's macro decision-making or the investment theory in the financial market, it actually embodies the idea of ​​greedy algorithms. With a certain amount of resources, the optimal allocation is achieved to maximize social well-being. The famous Coase theorem and Pareto improvement in economics are essentially the same, with finite values, expectations, and resource allocation.

When we write programs, we must not be confined to one point. We must see life, see the reality, and break out from time to time to have a more comprehensive and clear perspective. This is my little extra opinion.

Regarding Huffman coding, I also implemented a small project, which is actually the actual application of Huffman coding. I wrote a program that can actually compress files, and the compression rate is also between 70% and 80%. , If you are interested, you can take a look.

[C language -> data structure and algorithm] -> Huffman compression & decompression -> the first stage -> the realization of Huffman encoding & decoding

[C language -> data structure and algorithm] -> Huffman compression & decompression -> final game -> how to make a unique compression software

In addition, the content of this article mainly comes from "The Beauty of Data Structure and Algorithm" by Wang Zheng, a geek.

Guess you like

Origin blog.csdn.net/qq_45627684/article/details/108590058