贪心算法的最佳复杂度是多少?

发布于 2024-12-09 02:58:54 字数 259 浏览 4 评论 0原文

看起来最好的复杂度是线性 O(n)。

实际情况并不重要,我所说的是一般的贪婪算法。

有时候贪心也有好处?

在我感兴趣的具体情况下,是计算变化。

假设您需要找 35 美分的零钱。你有 1、5、10、25 枚硬币。贪心算法,编码简单,可以快速轻松地解决这个问题。首先抓取 35 中最高值的 25 美分,然后再抓取 10 美分以完成总数。这将是最好的情况。当然,也有不好的情况和这种贪心算法会出现问题的情况。我说的是确定此类问题的最佳情况复杂性。

It seems like the best complexity would be linear O(n).

Doesn't matter the case really, I'm speaking of greedy algorithms in general.

Sometimes it pays off to be greedy?

In the specific case that I am interested would be computing change.

Say you need to give 35 cents in change. You have coins of 1, 5, 10, 25. The greedy algorithm, coded simply, would solve this problem quickly and easily. First grabbing 25 cents the highest value going in 35 and then next 10 cents to complete the total. This would be best case. Of course there are bad cases and cases where this greedy algorithm would have issues. I'm talking best case complexity for determining this type of problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小猫一只 2024-12-16 02:58:54

任何具有必须单独获取的 n 项输出的算法最多具有 O(n) 时间复杂度;贪心算法也不例外。例如,背包问题的更自然的贪婪版本将 NP 完全问题转换为 O(n^2) 的问题 - 您尝试所有项目,选择留下最少可用空间的项目其余的;然后尝试所有剩余的,再次选择最好的;等等。每一步都是O(n)。但复杂性可以是任何东西——这取决于贪婪的难度。 (例如,像层次凝聚聚类这样的贪婪聚类算法具有 O(n^2) 的单独步骤来评估(至少天真地),并且需要 O(n)这些步骤。)

Any algorithm that has an output of n items that must be taken individually has at best O(n) time complexity; greedy algorithms are no exception. A more natural greedy version of e.g. a knapsack problem converts something that is NP-complete into something that is O(n^2)--you try all items, pick the one that leaves the least free space remaining; then try all the remaining ones, pick the best again; and so on. Each step is O(n). But the complexity can be anything--it depends on how hard it is to be greedy. (For example, a greedy clustering algorithm like hierarchical agglomerative clustering has individual steps that are O(n^2) to evaluate (at least naively) and requires O(n) of these steps.)

时间海 2024-12-16 02:58:54

当您谈论贪婪算法时,通常您谈论的是算法的正确性而不是时​​间复杂度,尤其是对于诸如更改之类的问题。

使用贪心启发法是因为它们很简单。这意味着简单问题可以轻松实现,困难问题可以合理近似。在后一种情况下,您会发现时间复杂度优于保证正确的算法。在前一种情况下,您不能指望比最佳时间复杂度更好。

When you're talking about greedy algorithms, typically you're talking about the correctness of the algorithm rather than the time complexity, especially for problems such as change making.

Greedy heuristics are used because they're simple. This means easy implementations for easy problems, and reasonable approximations for hard problems. In the latter case you'll find time complexities that are better than guaranteed correct algorithms. In the former case, you can't hope for better than optimal time complexity.

空城缀染半城烟沙 2024-12-16 02:58:54

贪婪方法

  1. 背包问题...使用合并排序对给定元素进行排序..(nlogn)
  2. 使用线性搜索找到需要 O(n) 的最大截止时间
  3. 选择一个元素...O(n²)

nlogn + n + n² = n² 在最坏的情况下......

现在我们可以应用二分搜索而不是线性搜索......?

GREEDY APPROACH

  1. knapsack problem...sort the given element using merge sort ..(nlogn)
  2. find max deadline that will take O(n)
  3. using linear search select one by one element....O(n²)

nlogn + n + n² = n² in worst case....

now can we apply binary search instead of linear search.....?

风柔一江水 2024-12-16 02:58:54

贪婪与否本质上与计算复杂性无关,除了贪婪算法在解决相同问题时往往比其他算法更简单,因此它们往往具有较低的复杂性。

Greedy or not has essentially nothing to do with computational complexity, other than the fact that greedy algorithms tend to be simpler than other algorithms to solve the same problem, and hence they tend to have lower complexity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文