Quick Start - Greedy Algorithms

Table of contents

​edit

foreword

text

The basic steps

Case: The problem of changing wine

topic

Example 1

Example 2

analyze

code

Summarize


foreword

The Greedy algorithm, also known as the greedy algorithm, seeks to take the best or optimal (that is, the most favorable) choice in the current state in each step of selection, so as to hope to lead to the best or optimal algorithm.

This article will quickly get you started with greedy algorithms.


For example, in the traveling salesman problem, if the traveler chooses the nearest city every time, then this is a greedy algorithm.

Greedy algorithms are particularly effective in problems with optimal substructure. The optimal substructure means that the local optimal solution can determine the global optimal solution. Simply put, the problem can be solved by decomposing it into sub-problems, and the optimal solutions of the sub-problems can be recursive to the optimal solution of the final problem.

The greedy algorithm differs from dynamic programming in that it makes a choice for the solution to each subproblem and cannot go back. Dynamic programming will save the previous operation results, and select the current according to the previous results, with a fallback function.

In the application, the greedy method can solve some optimization problems, such as: finding the minimum spanning tree in the graph, finding Huffman coding...

For other questions, greedy methods generally cannot get the answers we ask for. Once a problem can be solved by the greedy method, then the greedy method is generally the best way to solve the problem.

Due to the high efficiency of the greedy method and the fact that the obtained answer is relatively close to the optimal result, the greedy method can also be used as an auxiliary algorithm or directly solve some problems that require inaccurate results. In different situations, choosing the optimal solution may lead to Simpson's Paradox, and the optimal solution may not appear.

text

The basic steps

Step 1 : Start from an initial solution;

Step 2 : Using an iterative process, when it is possible to move forward to the goal, a partial solution is obtained according to the local optimal strategy to reduce the scale of the problem;

Step 3 : Synthesize all solutions.


Case: The problem of changing wine

topic

The convenience store in the neighborhood is on sale, and  you can exchange numExchange   empty wine bottles for a new bottle of wine. You bought   numBottles  of  wine; if you drink the wine in the bottle, the bottle will be empty. Please calculate the maximum number of bottles you can drink.


Example 1

Input: numBottles = 9, numExchange = 3 Output: 13 Explanation: You can exchange 3 empty wine bottles for 1 bottle of wine. So you can drink up to 9 + 3 + 1 = 13 bottles of wine.

Example 2

Input: numBottles = 15, numExchange = 4 Output: 19 Explanation: You can exchange 4 empty wine bottles for 1 bottle of wine. So you can drink up to 15 + 3 + 1 = 19 bottles of wine.


analyze

The dimension of greed in this question is very obvious, and it is directly exposed on the surface of the question.

That is to say, after drinking all the drinks, it becomes an empty bottle and after the existing empty bottle, the maximum and greedy exchange of drinks, and so on, until the empty bottle in hand is not enough to exchange a bottle of drink.


code

class Solution(object):
    def numWaterBottles(self, numBottles, numExchange):
        """
        :type numBottles: int
        :type numExchange: int
        :rtype: int
        """
        sumb = numBottles
        empty = numBottles
        while empty // numExchange:
            bottle = empty // numExchange # 兑酒数
            empty = bottle + empty % numExchange # 空瓶子数
            sumb += bottle

        return sumb

Summarize

The so-called greedy algorithm means that when solving a problem, it always makes the best choice at present. That is to say, without considering the overall optimality, what is made is only a local optimal solution in a certain sense.

The greedy algorithm does not have a fixed algorithm framework. The key to the algorithm design is the choice of the greedy strategy. The premise of using the greedy strategy is that the local optimum can lead to the global optimum.

It must be noted that the greedy algorithm cannot obtain the overall optimal solution for all problems, and the greedy strategy selected must have no aftereffect, that is , the process after a certain state will not affect the previous state, only related to the current state ( different from dynamic programming) . Therefore, the adopted greedy strategy must be carefully analyzed whether it satisfies no aftereffect.

Guess you like

Origin blog.csdn.net/flyTie/article/details/127130941