用贪婪算法解决背包问题_解决主要算法问题的贪婪策略
用貪婪算法解決背包問題
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:
貪婪策略的基本屬性可以分為兩部分:
Greedy choice property
貪婪的選擇屬性
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
用貪婪算法解決背包問題
總結
以上是生活随笔為你收集整理的用贪婪算法解决背包问题_解决主要算法问题的贪婪策略的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringBoot 中的 3 种条件装
- 下一篇: 你知道group by的工作原理和优化思