# Section 1.3 Greedy Algorithm

128人阅读 评论(0)

The basic idea behind greedy algorithms is to build large solutions up from smaller ones. Unlike other approaches, however, greedy algorithms keep only the best solution they find as they go along.

Greedy algorithms are fast, generally linear to quadratic and require little extra memory. Unfortunately, they usually aren’t correct. But when they do work, they are often easy to implement and fast enough to execute.

## Sample Problems

1.Sorting a three-valued sequence [IOI 1996]

为一个3值序列排序

You are given a three-valued (1, 2, or 3) sequence of length up to 1000. Find a minimum set of exchanges to put the sequence in sorted order.

最长不超过1000的三值（1,2,3）序列，找到最小的交换方法，使得序列按顺序排列

Algorithm :The sequence has three parts: the part which will be 1 when in sorted order, 2 when in sorted order, and 3 when in sorted order. The greedy algorithm swaps as many as possible of the 1’s in the 2 part with 2’s in the 1 part, as many as possible 1’s in the 3 part with 3’s in the 1 part, and 2’s in the 3 part with 3’s in the 2 part. Once none of these types remains, the remaining elements out of place need to be rotated one way or the other in sets of 3. You can optimally sort these by swapping all the 1’s into place and then all the 2’s into place.

序列可分为三块：应该放1的（A区），应该放2的（B区），应该放3的（C区）；

2.Friendly Coins - A Counterexample [abridged]

友好的硬币（一个反例）[删减版]

Given the denominations of coins for a newly founded country, the Dairy Republic, and some monetary amount, find the smallest set of coins that sums to that amount. The Dairy Republic is guaranteed to have a 1 cent coin.

已知一个新国度（Dairy Republic）的各种硬币面值，和一个目标货币数。

Algorithm: Take the largest coin value that isn’t more than the goal and iterate on the total minus this value.

用贪心算法的话，想法很简单：不断使用尽可能大面值的硬币加入

Maybe not: Okay, the algorithm usually works. In fact, for the U.S. coin system {1, 5, 10, 25}, it always yields the optimal set. However, for other sets, like {1, 5, 8, 10} and a goal of 13, this greedy algorithm would take one 10, and then three 1’s, for a total of four coins, when the two coin solution {5, 8} also exists.

然而，本题使用贪心算法可能是错误的：

3.Topological Sort

拓扑排序

Given a collection of objects, along with some ordering constraints, such as “A must be before B,” find an order of the objects such that all the ordering constraints hold.

已知一个物体集合，和一些排序限制（比如"A需要排在B前面"）。

Algorithm: Create a directed graph over the objects, where there is an arc from A to B if “A must be before B.” Make a pass through the objects in arbitrary order. Each time you find an object with in-degree of 0, greedily place it on the end of the current ordering, delete all of its out-arcs, and recurse on its (former) children, performing the same check. If this algorithm gets through all the objects without putting every object in the ordering, there is no ordering which satisfies the constraints.

数据结构选择：有向图

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：2110次
• 积分：162
• 等级：
• 排名：千里之外
• 原创：14篇
• 转载：0篇
• 译文：1篇
• 评论：0条
阅读排行
评论排行