Understand greedy algorithms

    In this tutorial, you will learn what a greedy algorithm is. In addition, you will find an example of a greedy algorithm.
    Greedy algorithm is a method of solving a problem by choosing the best options currently available, without having to consider the results it will bring in the future. In other words, through the local best choice, the goal is to achieve the global best choice.
    This algorithm may not be the best choice for all problems. In some cases, it may produce wrong results.
    The algorithm uses a top-down approach.
    The main advantages of this algorithm are:

  1. The algorithm is easier to describe.
  2. The performance of this algorithm may be better than other algorithms (but not in all cases).
Feasible solution

    The feasible solution provides the optimal solution for solving the problem.

how are you
  1. First, the solution set containing the answer is empty.
  2. At each step, an item is added to the solution set.
  3. If the solution set is feasible, keep the current item.
  4. Otherwise, the item will be rejected and no longer considered.
Example
问题:您必须使用最少数量的硬币进行零钱找零。
金额:28美元

可用硬币:
  5美元硬币
  2美元硬币
  1美元硬币

    Solution:

  1. Create an empty solution set solution-set = {}.
  2. coins = {5, 2, 1}
  3. sum = 0
  4. When sum ≠ 28, perform the following operations.
  5. Choose a coin C from coins such that sum + C <28.
  6. If C + sum> 28, return no solution.
  7. Otherwise, sum = sum + C.
  8. Add C to the solution-set.

    After 5 iterations, the solution set contains 5 5 USD coins. Then get a 2 dollar coin, and finally get a 1 dollar coin.

Application of Greedy Algorithm
  • Select sort
  • Knapsack problem
  • Minimum spanning tree
  • Single source shortest path problem
  • Job scheduling problem
  • Prim's minimum spanning tree algorithm
  • Kruskal's minimum spanning tree algorithm
  • Dijkstra's minimum spanning tree algorithm
  • Huffman coding
  • Ford-Fulkson Algorithm
Reference documents

[1]Parewa Labs Pvt. Ltd.Greedy Algorithm[EB/OL].https://www.programiz.com/dsa/greedy-algorithm,2020-01-01.
[2]Baidu. Greedy Algorithm[EB/OL ].https://baike.baidu.com/item/greedy algorithm/5411800,2021-02-22.
[3] Big cake. Classic algorithm idea 5-greedy algorithm [EB/OL].https: //zhuanlan.zhihu.com/p/72734416, 2020-11-05.

Guess you like

Origin blog.csdn.net/zsx0728/article/details/115062787