多变量 Big-O 时间复杂度的简化

发布于 2025-01-15 05:52:30 字数 505 浏览 2 评论 0原文

我正在尝试计算一个函数的时间复杂度,该函数使用合并排序对两个数组进行排序,找到它们的交集并对这个交集的结果进行排序。

通过分析所涉及的步骤,我发现关键步骤的时间复杂度为

O(a log a) + O(b log b) + O(a + b) + O((a+b)log(a +b))

其中 a 是第一个数组的大小,b 是第二个数组的大小

我将其进一步简化为: O(a log a) + O(b log b) + O((a+b)log(a+b))

这就是我陷入困境的地方。但根据我所读到的内容,a + b 大于 ab 的想法允许我删除术语 a log ab log b。使用这个,我得到了整体的大 O 表示法 O((a+b)log(a+b))

我不确定这是否完全正确。

I am attempting to calculate the Time Complexity of a function that sorts two arrays using Merge sort, finds their intersection and sorts the result from this intersection.

By analyzing the steps involved, I found the key steps to have time complexities of

O(a log a) + O(b log b) + O(a + b) + O((a+b)log(a+b))

where a is the size of the first array and b is the size of the second array

I further simplified this down to:
O(a log a) + O(b log b) + O((a+b)log(a+b))

This is where I got stuck. But from what I have read, the idea of a + b being greater than both a and b allow me to remove the terms a log a and b log b. Using this, I got the overall Big O Notation as O((a+b)log(a+b)).

I am not sure if this is entirely correct though.

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

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

发布评论

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

评论(1

闻呓 2025-01-22 05:52:30

你的分析是正确的。如果您不确定,让我一步一步更明确地解释它。

总体时间复杂度为:O(alog(a)) + O(blog(b)) + O(alog(a+b) + blog(a+b))
对数是 x > 上的严格递增函数。 0,即log(x)> log(y) iff 当且仅当 x > y> 0 。
输入大小不能为负,因此我们可以利用这一事实进行分析。

使用这个属性,我们知道 log(a+b) > log(a)
,因此alog(a+b) > alog(a)
我们可以得出结论,O(a log(a))O(a log(a+b))子集,因为 >当输入大小趋于无穷大时,每个算法的上限都是alog(a),上限也是a log(a+b)
因此我们可以摆脱O(a log(a))
O(b log(b)) 应用相同的逻辑。

最后,我们有O(alog(a+b) + blog(a+b)),它对应于O((a+b)log(a+ b))

Your analysis is correct. Let me explain it more explicitly step by step if you are not sure about it.

Overall time complexity is: O(alog(a)) + O(blog(b)) + O(alog(a+b) + blog(a+b)).
Logarithm is a strictly increasing function on x > 0, that is, log(x) > log(y) iff x > y > 0.
Input size cannot be negative, therefore for our analysis we can use this fact.

Using this property, we know that log(a+b) > log(a), therefore alog(a+b) > alog(a).
We can conclude that O(a log(a)) is a subset of O(a log(a+b)) since every algorithm upper-bounded by alog(a) as input size goes to infinity is also upper-bounded by a log(a+b).
Therefore we can get rid of O(a log(a)).
Apply the same logic for O(b log(b)).

At the end, we have O(alog(a+b) + blog(a+b)), which corresponds to O((a+b)log(a+b)).

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