vcfab算法示例_用示例解释贪婪算法

vcfab算法示例

什么是贪心算法? (What is a greedy algorithm?)

You may have heard about a lot of algorithmic design techniques while sifting through some of the articles here. Some of them are:

在浏览本文中的某些文章时,您可能听说过许多算法设计技术。 他们之中有一些是:

  • Brute Force

    蛮力
  • Divide and Conquer

    分而治之
  • Greedy Programming

    贪婪编程
  • Dynamic Programming to name a few. In this article, you will learn about what a greedy algorithm is and how you can use this technique to solve a lot of programming problems that otherwise do not seem trivial.

    动态编程仅举几例。 在本文中,您将了解什么是贪婪算法,以及如何使用此技术来解决很多编程问题,否则这些问题似乎并不容易。

Imagine you are going for hiking and your goal is to reach the highest peak possible. You already have the map before you start, but there are thousands of possible paths shown on the map. You are too lazy and simply don’t have the time to evaluate each of them. Screw the map! You started hiking with a simple strategy – be greedy and short-sighted. Just take paths that slope upwards the most. This seems like a good strategy for hiking. But is it always the best ?

想象一下您要去远足,您的目标是尽可能达到最高峰。 开始之前您已经有了地图,但是地图上显示了数千种可能的路径。 您太懒了,根本没有时间评估它们。 拧地图! 您从一个简单的策略开始远足-贪婪且目光短浅。 只需选择最向上倾斜的路径即可。 这似乎是远足的好策略。 但是它总是最好的吗?

After the trip ended and your whole body is sore and tired, you look at the hiking map for the first time. Oh my god! There’s a muddy river that I should’ve crossed, instead of keep walking upwards. This means that a greedy algorithm picks the best immediate choice and never reconsiders its choices. In terms of optimizing a solution, this simply means that the greedy solution will try and find local optimum solutions - which can be many - and might miss out on a global optimum solution.

旅行结束后,您的全身酸痛又疲惫,您将第一次看看远足地图。 哦,我的上帝! 我应该越过一条泥泞的河,而不是继续向上走。 这意味着贪婪的算法会选择最佳的即时选择,而不会重新考虑其选择。 就优化解决方案而言,这仅意味着贪婪的解决方案将尝试找到局部最优解(可能很多),并且可能会错过全局最优解。

正式定义 (Formal Definition)

Assume that you have an objective function that needs to be optimized (either maximized or minimized) at a given point. A Greedy algorithm makes greedy choices at each step to ensure that the objective function is optimized. The Greedy algorithm has only one shot to compute the optimal solution so that it never goes back and reverses the decision.

假设您有一个目标函数,需要在给定点进行优化(最大化或最小化)。 贪婪算法会在每个步骤中做出贪婪的选择,以确保优化目标函数。 贪婪算法只计算一次最优解,因此它永远不会退缩并扭转决策。

贪婪算法有一些优点和缺点: (Greedy algorithms have some advantages and disadvantages:)

  • It is quite easy to come up with a greedy algorithm (or even multiple greedy algorithms) for a problem. Analyzing the run time for greedy algorithms will generally be much easier than for other techniques (like Divide and conquer). For the Divide and conquer technique, it is not clear whether the technique is fast or slow. This is because at each level of recursion the size of gets smaller and the number of sub-problems increases.

    提出一个贪婪算法(甚至多个贪婪算法)是很容易的。 与其他技术(例如“分而治之”)相比,分析贪婪算法的运行时间通常要容易得多。 对于分治法,尚不清楚该技术是快速还是缓慢。 这是因为在每个递归级别上,的大小都会变小,子问题的数量也会增加。
  • The difficult part is that for greedy algorithms you have to work much harder to understand correctness issues. Even with the correct algorithm, it is hard to prove why it is correct. Proving that a greedy algorithm is correct is more of an art than a science. It involves a lot of creativity. Usually, coming up with an algorithm might seem to be trivial, but proving that it is actually correct, is a whole different problem.

    困难的部分是,对于贪婪算法,您必须更加努力地理解正确性问题。 即使使用正确的算法,也很难证明其正确性。 证明贪婪算法是正确的,更多的是艺术而不是科学。 它涉及很多创造力。 通常,想出一种算法看似微不足道,但是证明它实际上是正确的,则是一个完全不同的问题。

间隔调度问题 (Interval Scheduling Problem)

Let's dive into an interesting problem that you can encounter in almost any industry or any walk of life. Some instances of the problem are as follows:

让我们深入探讨一个有趣的问题,您几乎可以在任何行业或各行各业中遇到该问题。 问题的某些情况如下:

  • You are given a set of N schedules of lectures for a single day at a university. The schedule for a specific lecture is of the form (s time, f time) where s time represents the start time for that lecture and similarly the f time represents the finishing time. Given a list of N lecture schedules, we need to select maximum set of lectures to be held out during the day such that  none of the lectures overlap with one another i.e. if lecture Li and Lj are included in our selection then the start time of j >= finish time of i or vice versa .

    在大学里,一天中会为您提供N个课程安排。 特定讲座的日程安排采用(s 时间,f时间)的形式其中s 时间代表该课程的开始时间,同样,f时间代表结束时间。 给定N个讲座时间表的列表,我们需要选择当天最多要举行的一组讲座,以使所有讲座都不相互重叠,即如果讲座Li和Lj被包括在我们的选择中,则j的开始时间> = i的完成时间,反之亦然

  • Your friend is working as a camp counselor, and he is in charge of organizing activities for a set of campers. One of his plans is the following mini-triathlon exercise: each contestant must swim 20 laps of a pool, then bike 10 miles, then run 3 miles.

    您的朋友正在担任营地顾问,他负责为一组营员组织活动。 他的计划之一是以下的微型铁人三项运动:每个参赛者必须在游泳池游泳20圈,然后骑自行车10英里,然后跑步3英里。
  • The plan is to send the contestants out in a staggered fashion, via the following rule: the contestants must use the pool one at a time. In other words, first one contestant swims the 20 laps, gets out, and starts biking.

    该计划是按照以下规则以交错方式将参赛者送出:参赛者必须一次使用一个池。 换句话说,第一个参赛者会游泳20圈,下车,然后开始骑自行车。
  • As soon as this first person is out of the pool, a second contestant begins swimming the 20 laps; as soon as he or she is out and starts biking, a third contestant begins swimming, and so on.

    当第一个人离开游泳池时,第二名选手开始游泳20圈; 他或她外出并开始骑自行车时,第三位选手开始游泳,依此类推。
  • Each contestant has a projected swimming time, a projected biking time, and a projected running time. Your friend wants to decide on a schedule for the triathlon: an order in which to sequence the starts of the contestants.

    每个参赛者都有一个预计的游泳时间,一个预计的骑自行车时间以及一个预计的跑步时间。 您的朋友想决定铁人三项赛的时间表:按顺序排列参赛者的出发顺序。
  • Let's say that the completion time of a schedule is the earliest time at which all contestants will be finished with all three legs of the triathlon, assuming the time projections are accurate. What is the best order for sending people out, if one wants the whole competition to be over as soon as possible? More precisely, give an efficient algorithm that produces a schedule whose completion time is as small as possible

    假设时间表是准确的,那么时间表的完成时间是所有铁人三项比赛所有三项比赛都将完成的最早时间。 如果要使整个比赛尽快结束,最好的送人命令是什么? 更精确地讲,给出一种有效的算法,该算法可生成时间表,该时间表的完成时间应尽可能短

演讲安排问题 (The Lecture Scheduling Problem)

Let's look at the various approaches for solving this problem.

让我们看一下解决这个问题的各种方法。

Earliest Start Time First  i.e. select the interval that has the earliest start time. Take a look at the following example that breaks this solution. This solution failed because there could be an interval that starts very early but that is very long. This means the next strategy that we could try would be where we look at smaller intervals first.

最早的开始时间首先,即选择最早的开始时间间隔。 请看下面的示例,它打破了此解决方案。 该解决方案失败了,因为可能有一个很早开始但很长的间隔。 这意味着我们可以尝试的下一个策略是首先查看较小的时间间隔。

Smallest Interval First  i.e. you end up selecting the lectures in order of their overall interval which is nothing but their  finish time - start time . Again, this solution is not correct. Look at the following case.

最短的间隔首先,即您最终按照整体间隔选择讲座,这只不过是他们的finish time - start time 。 同样,此解决方案不正确。 请看以下情况。

You can clearly see that the shortest interval lecture is the one in the middle, but that is not the optimal solution here. Let's look at yet another solution for this problem deriving insights from this solution.

您可以清楚地看到,最短的间隔课程是中间的课程,但这不是最佳的解决方案。 让我们看看该问题的另一种解决方案,该解决方案可从该解决方案中获得见解。

Least Conflicting Interval First  i.e. you should look at intervals that cause the least number of conflicts. Yet again we have an example where this approach fails to find an optimal solution.

最小冲突间隔首先,即您应该查看导致冲突次数最少的间隔。 再有一个例子,说明这种方法无法找到最佳解决方案。

The diagram shows us that the least confliciting interval is the one in the middle with just 2 conflicts. After that we can only pick the two intervals at the very ends with conflicts 3 each. But the optimal solution is to pick the 4 intervals on the topmost level.

该图向我们展示了最小的冲突间隔是中间的冲突间隔,只有两个冲突。 之后,我们只能在末端选择两个间隔,每个间隔为冲突3。 但是最佳解决方案是在最顶层选择4个间隔。

Earliest Finishing time first. This is the approach that always gives us the most optimal solution to this problem. We derived a lot of insights from previous approaches and finally came upon this approach. We sort the intervals according to increasing order of their finishing times and then we start selecting intervals from the very beginning. Look at the following pseudo code for more clarity.

最早的整理时间在前 。 这是始终为我们提供此问题最佳解决方案的方法。 我们从以前的方法中获得了很多见解,最后想到了这种方法。 我们根据时间间隔的增加顺序对时间间隔进行排序,然后从头开始选择时间间隔。 请看下面的伪代码以更清楚。

function interval_scheduling_problem(requests)
    schedule \gets \{\}
    while requests is not yet empty
        choose a request i_r \in requests that has the lowest finishing time
        schedule \gets schedule \cup \{i_r\}
        delete all requests in requests that are not compatible with i_r
    end
    return schedule
end

我们什么时候使用贪婪算法 (When do we use Greedy Algorithms)

Greedy Algorithms can help you find solutions to a lot of seemingly tough problems. The only problem with them is that you might come up with the correct solution but you might not be able to verify if its the correct one. All the greedy problems share a common property that a local optima can eventually lead to a global minima without reconsidering the set of choices already considered.

贪婪算法可以帮助您找到许多看似棘手的问题的解决方案。 它们的唯一问题是您可能会提出正确的解决方案,但可能无法验证其是否正确。 所有贪婪的问题都有一个共同的属性,即局部最优最终可以导致全局最小,而无需重新考虑已经考虑的选择集。

Greedy Algorithms help us solve a lot of different kinds of problems, like:

贪婪算法可帮助我们解决许多不同类型的问题,例如:

最短路径问题: (Shortest Path Problem:)

图中的最小生成树问题 (Minimum Spanning Tree Problem in a Graph)

霍夫曼编码问题 (Huffman Encoding Problem)

K中心问题 (K Centers Problem)

翻译自: https://www.freecodecamp.org/news/what-is-a-greedy-algorithm/

vcfab算法示例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值