Data Structures Algorithms 简明教程

Greedy Algorithms

在所有算法方法中,最简单最直接的方法是贪心方法。在这种方法中,在不考虑当前决策未来影响的情况下,仅基于当前可用信息做出决策。

Among all the algorithmic approaches, the simplest and straightforward approach is the Greedy method. In this approach, the decision is taken on the basis of current available information without worrying about the effect of the current decision in future.

贪心算法部分地构建解决方案,以这样的方式选择下一部分,使其立即受益。此方法从不重新考虑先前做出的选择。此方法主要用于解决优化问题。贪心方法易于实现,并且在大多数情况下效率很高。因此,我们可以说贪心算法是一种启发式算法范例,它希望找到全局最优解,在每一步都遵循局部最优选择。

Greedy algorithms build a solution part by part, choosing the next part in such a way, that it gives an immediate benefit. This approach never reconsiders the choices taken previously. This approach is mainly used to solve optimization problems. Greedy method is easy to implement and quite efficient in most of the cases. Hence, we can say that Greedy algorithm is an algorithmic paradigm based on heuristic that follows local optimal choice at each step with the hope of finding global optimal solution.

在许多问题中,它虽然在合理的时间内提供了近似(接近最优)的解决方案,但并不能产生最优解。

In many problems, it does not produce an optimal solution though it gives an approximate (near optimal) solution in a reasonable time.

Components of Greedy Algorithm

贪心算法具有以下五个组成部分 -

Greedy algorithms have the following five components −

  1. A candidate set − A solution is created from this set.

  2. A selection function − Used to choose the best candidate to be added to the solution.

  3. A feasibility function − Used to determine whether a candidate can be used to contribute to the solution.

  4. An objective function − Used to assign a value to a solution or a partial solution.

  5. A solution function − Used to indicate whether a complete solution has been reached.

Areas of Application

贪心法用于解决许多问题,例如:

Greedy approach is used to solve many problems, such as

  1. Finding the shortest path between two vertices using Dijkstra’s algorithm.

  2. Finding the minimal spanning tree in a graph using Prim’s /Kruskal’s algorithm, etc.

Counting Coins Problem

计数硬币问题是通过选择最少的硬币来计算所需值,而贪心法迫使算法选择最大的硬币。如果我们提供1、2、5和10的硬币,并要求我们计算18,那么贪心程序将为−

The Counting Coins problem is to count to a desired value by choosing the least possible coins and the greedy approach forces the algorithm to pick the largest possible coin. If we are provided coins of 1, 2, 5 and 10 and we are asked to count 18 then the greedy procedure will be −

  1. 1 − Select one 10 coin, the remaining count is 8

  2. 2 − Then select one 5 coin, the remaining count is 3

  3. 3 − Then select one 2 coin, the remaining count is 1

  4. 4 − And finally, the selection of one 1 coins solves the problem

尽管它似乎工作得很好,但对于这个计数,我们只需要选择4枚硬币。但是,如果我们稍微改变一下问题,那么相同的方法可能无法产生相同的最优结果。

Though, it seems to be working fine, for this count we need to pick only 4 coins. But if we slightly change the problem then the same approach may not be able to produce the same optimum result.

对于货币系统,我们有1、7、10值硬币,计算18的值硬币将是绝对最优的,但对于像15这样的计数,它可能使用的硬币比必要的还要多。例如,贪心法将使用10 + 1 + 1 + 1 + 1 + 1,总共6枚硬币。而同样的问题可以使用仅3枚硬币(7 + 7 + 1)来解决

For the currency system, where we have coins of 1, 7, 10 value, counting coins for value 18 will be absolutely optimum but for count like 15, it may use more coins than necessary. For example, the greedy approach will use 10 + 1 + 1 + 1 + 1 + 1, total 6 coins. Whereas the same problem could be solved by using only 3 coins (7 + 7 + 1)

因此,我们可以得出结论,贪心法选择了一种立即优化的解决方案,并且在整体优化是一个主要问题时可能会失败。

Hence, we may conclude that the greedy approach picks an immediate optimized solution and may fail where global optimization is a major concern.

Where Greedy Approach Fails

在许多问题中,贪心算法无法找到一个最优解,而且它可能产生一个最差解。诸如旅行商和背包之类的难题无法使用这种方法解决。

In many problems, Greedy algorithm fails to find an optimal solution, moreover it may produce a worst solution. Problems like Travelling Salesman and Knapsack cannot be solved using this approach.

Examples of Greedy Algorithm

大多数联网算法都采用贪婪方法。这里列出一些此类算法−

Most networking algorithms use the greedy approach. Here is a list of few of them −

我们在本教程的后续章节中将详细讨论这些示例。

We will discuss these examples elaborately in the further chapters of this tutorial.