用贪婪算法解决背包问题_解决主要算法问题的贪婪策略

本文介绍了贪婪算法的概念,通过举例说明其工作原理。贪婪策略是在每个步骤中做出局部最优决策,以期望达到全局最优。然而,这种策略并不总是有效。文章列举了贪婪算法在排序、优先队列、霍夫曼编码、图算法等领域中的应用,并指出其在解决复杂问题时可作为求解近似方案的方法。
摘要由CSDN通过智能技术生成

用贪婪算法解决背包问题

Introduction:

介绍:

Let's start the discussion with an example that will help to understand the greedy technique. If we think about playing chess, when we make a move we think about the consequences of the move in future states, but in case of playing cricket or tennis, we consider immediate states rather considering any future consequences. This means in some cases we make decisions which seems to be correct at that moment and in some cases we make a decision based on the following consequences or future cases.

让我们从一个有助于理解贪婪技巧的例子开始讨论。 如果我们考虑下棋,那么当我们做出举动时,我们会考虑这一举动在未来状态下的后果,但是在打板球或打网球的情况下,我们考虑的是即时状态,而不是考虑任何未来的后果 。 这意味着在某些情况下,我们做出当时似乎正确的决定,并且在某些情况下,我们基于以下后果或未来情况做出决定。

The idea of local and global can take place here. Local means the immediate case where global means considering the future situation.

本地和全球的想法可以在这里进行。 本地意味着眼前的情况,而全球意味着考虑未来的情况。

The greedy technique is all about making a local decision, based n that immediate case, on based on future consequences and that's why the strategy is known as greedy.

贪婪的技巧全在于根据紧急情况基于未来的后果做出本地决策,这就是为什么该策略被称为贪婪的原因

贪婪策略 (Greedy strategy)

Greedy strategy means to make a decision at each step without taking account its consequence at future steps. We find out the best local move at each step to reach the goal. The greedy strategy assumes that a bunch of local best decisions can lead to global optimization.

贪婪策略意味着在每个步骤中都做出决定,而不考虑其在后续步骤中的后果。 我们找到在实现目标的每一步中的最佳本地移动。 贪婪策略假设一堆本地最佳决策可以导致全局优化

什么贪心算法组成? (What greedy algorithm consists of?)

The basic properties of the greedy strategy can be divided into two part:

贪婪策略的基本属性可以分为两部分:

  1. Greedy choice property

    贪婪的选择属性

  2. Optimal substructure

    最佳子结构

Greedy choice property is about making local optimization (greedy). The choices made by greedy may depend on the past moves but never on the future steps. Iteratively, we make each greedy move to reduce the problem to a smaller problem and finally to achieve global optimization.

贪婪选择属性是关于进行局部优化(贪婪)的。 贪婪做出的选择可能取决于过去的举动,而不取决于未来的步骤。 反复地,我们采取各种贪婪的举动以将问题简化为较小的问题,并最终实现全局优化。

Optimal substructure means if we can divide the problem to further sub-problems and have optimal solutions for that, which will combine to a solution to solve the entire large problem.

最优子结构意味着如果我们可以将问题分解为其他子问题,并为此找到最优解决方案,则可以将其组合为解决整个大问题的解决方案。

贪婪总是奏效吗? (Does greedy work always?)

Needless to say, making a local best choice can't lead to global best choice always. Just think of a hill and think while climbing you are to find the maximum peak. You took a greedy strategy and found a local peak which is not at all global.

毋庸置疑,做出本地最佳选择并不能总导致全球最佳选择。 只是想想一座小山,想想在爬山时您会发现最大的山峰。 您采取了一个贪婪的策略,并发现了一个根本不是全球性的局部高峰。

Thus greedy strategy doesn't work for all to give the best solutions.

因此,贪婪策略并不能为所有人提供最佳解决方案。

贪婪算法一目了然 (Greedy algorithm at a glance )

Why to use greedy algorithm?

为什么要使用贪心算法?

  • It's straightforward, easy to examine and easy to code.

    它简单明了,易于检查且易于编码。

  • Since we are making local moves, no need to store any computation to re-examine.

    由于我们正在本地移动,因此无需存储任何计算即可重新检查。

But greedy has pitfalls

但是贪婪有陷阱

  • It doesn't have a solution to all problems

    它并不能解决所有问题

  • In many cases greedy fails to lead optimal solution

    在许多情况下,贪婪无法导致最优解

  • In some cases, it misleads to a wrong solution

    在某些情况下,它会误导错误的解决方案

Still due to the simple structure and strategic nature greedy has a lot of applications. In interview coding problems we also use greedy strategy to the well-known greedy problems. Following is a list of applications where greedy can be used:

仍然由于简单的结构和战略性质,贪婪具有许多应用。 在面试编码问题中,我们还对著名的贪婪问题使用贪婪策略。 以下是可以使用贪婪的应用程序列表:

There is also a special use of the greedy technique. When we need to find an approximate solution to a complex problem, greedy can be a superb choice.

贪婪技术也有特殊用途。 当我们需要找到一个复杂问题的近似解决方案时,贪婪可能是一个极好的选择。

翻译自: https://www.includehelp.com/icp/greedy-strategy-to-solve-major-algorithm-problems.aspx

用贪婪算法解决背包问题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值