用贪婪算法解决背包问题

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:

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

  • Sorting: Topological sort, heap sort

    排序: 拓扑排序 , 堆排序

  • Priority Queue

    优先队列

  • Huffman coding compression

    霍夫曼编码压缩

  • Prim's and Kruskal's algorithm

    Prim和Kruskal算法

  • Dijkstra's algorithm to find the shortest path in a weighted graph

    Dijkstra的算法,用于找到加权图中的最短路径

  • Coin change problem

    找零问题

  • Fractional knapsack problem

    小背包问题

  • Job scheduling problem

    作业安排问题

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

用贪婪算法解决背包问题

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

  1. 使用ga算法解决背包问题_我如何使用算法解决现实生活中的手提背包的背包问题

    使用ga算法解决背包问题 I'm a nomad and live out of one carry-on bag. This means that the total weight of all m ...

  2. 求解决方法_解决方法

    求解决方法 Relationship management is one of the determining factors in the business health. One of the m ...

  3. python运行启动报错解决方法_解决python运行启动报错问题

    解决python运行启动报错问题 问题一: python启动报错api-ms-win-crt-process-l1-1-0.dll丢失 解决: 下载api-ms-win-crt-process-l1- ...

  4. python memoryerror解决办法_解决python报错MemoryError的问题

    解决python报错MemoryError的问题 如下: python 32bit 最大只能使用 2G 内存,坑爹之处,超过 2G 报错MemoryError. 而 64bit python则无此限制 ...

  5. 部分背包的贪婪算法 java_使用JAVA实现算法——贪心算法解决背包问题

    packageBasePart;importjava.io.BufferedReader;importjava.io.FileInputStream;importjava.io.IOException ...

  6. 背包问题 贪心算法 java_JS基于贪心算法解决背包问题

    前面我们分享了关于js使用贪心算法解决找零问题,本文我们接着为大家介绍JS基于贪心算法解决背包问题. 贪心算法:在对问题求解时,总是做出在当前看来是最好的选择.也就是说,不从整体最优上加以考虑,他所做 ...

  7. 基因算法解析、设计,以解决背包问题和旅行商问题为例

    一.算法说明 基因算法 基因算法有一套公共的完整的框架,伪代码如下. beginset time t = 0 # first generationinitGeneration() # initiali ...

  8. 贪心算法解决背包问题

    贪心算法解决背包问题 问题描述: 给定 n 个物品和一个容量为 C 的背包,请给出物品装入背包的方案,使得背包中物品的总价值 M 最大,并满足: 1.每个物品 I 的重量为 wi,价值为 vi. 2. ...

  9. 背包问题 贪心算法 java_JS基于贪心算法解决背包问题示例

    本文实例讲述了JS基于贪心算法解决背包问题.分享给大家供大家参考,具体如下: 贪心算法:在对问题求解时,总是做出在当前看来是最好的选择.也就是说,不从整体最优上加以考虑,他所做出的仅是在某种意义上的局 ...

最新文章

  1. 颠覆认知——Redis会遇到的15个「坑」,你踩过几个?
  2. 转:ASP自动解压RAR文件
  3. Python 两大环境管理神器:pyenv 和 virtualenv
  4. initPlayer初始化过程(一)
  5. 抽象工厂模式java_Java描述设计模式(04):抽象工厂模式
  6. 20年备受关注的6款数据工具!谁最好用?毫无争议的答案来了
  7. Adobe AIR简单的缓存技术
  8. cookielib和urllib2模块相结合模拟网站登录
  9. 获取HG526超级密码
  10. 实时数仓在有赞的实践
  11. Tidabie Tidal Music 潮汐音乐下载转换器
  12. 分层结构的生活例子_系统抽样和分层抽样在现实生活中有什么例子?
  13. mysql创建用户临时表_mysql创建与删除临时表实例
  14. LeetCode 香槟塔
  15. 51单片机:数码管显示
  16. 浏览器是先执行js还是先加载HTML,在HTML中使用JavaScript(浏览器对js的加载机制分析)...
  17. 创造与魔法241服务器系统什么时候修好,《创造与魔法》萌新小课堂——如何选择服务器...
  18. Python生成中文词云图(二):不可指定词云特定的形状和颜色。
  19. Media Queries之Respond.js
  20. 【UGUI】如何实现自动滚动文本效果

热门文章

  1. bash shell是如何识别特殊符号的
  2. mycat 不得不说的缘分
  3. Tomcat 服务器只能存有一个正在运行的项目
  4. 数据库 数据库SQL语句一
  5. 迭代获取ViewState
  6. MongoDB与其他数据库的对比
  7. 《几何与代数导引》例2.9
  8. 关于《详解》第12.1节按键驱动的进一步阐述
  9. android camera滑动,Android怎么实现小米相机底部滑动指示器
  10. 智能情绪分析技术_石化缘推荐:炼化企业智能机器人巡检技术应用前景分析!...