Section 1.3 Greedy Algorithm

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区);
所以,第一步把所有(A区的2,B区的1)、(A区的3,C区的1)、(B区的3,C区的2)交换;
接下来,把所有1放回A区,再把所有2放回B区;
排序结束。

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)的各种硬币面值,和一个目标货币数。
要求找到最少的硬币数,使得总和等于目标货币数。
该国家确保有1分硬币(保证题目必有解)

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.

然而,本题使用贪心算法可能是错误的:
反例:硬币面值{1,5,8,10},目标面值13;

贪心算法结果:10+1+1+1   4枚硬币
正确结果:8+5   2枚硬币

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.

数据结构选择:有向图
把物体化为节点,把排序限制化为有向箭头(从排在前面的物体指向排在后面的物体)
贪心算法:每一次发现一个物体没有指出去的箭头,“贪婪地”把它直接放在最后,然后对它的前一个结点进行递归操作。
如果遍历所有物体后没有找到满足条件的序列,则无解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值