算法的渐近运行时间
我决定尝试做一个关于分析算法最坏可能运行时间的问题并获得一些练习。 由于我是初学者,我只需要帮助以正确的方式表达我的答案。 我在一本使用以下算法的书中遇到了这个问题:
输入:一组 n 点 (x1, y1), 。 。 。 , (xn, yn) 其中 n ≥ 2。 输出:最近一对点的平方距离。 ClosePoints
1. if n = 2 then return (x1 − x2)^2 + (y1 − y2)^2
2. else
3. d ← 0
4. for i ← 1 to n − 1 do
5. for j ← i + 1 to n do
6. t ← (xi − xj)^2 + (yi − yj)^2
7. if t < d then
8. d ← t
9. return d
我的问题是如何提供一个很好的证明 T(n) = O(n^2),T(n) = Ω(n^2) 和 T (n) = θ(n^2)?,其中T(n) 表示最坏的可能运行时间。 我知道我们说 f 是 O(g), 当且仅当存在 n0 ∈ N 且 c > R 中的 0 使得对于所有 n ≥ n0 我们有 f(n) ≤ cg(n)。 如果有一个,我们也说 f 是 Ω(g) n0 ∈ N 且 c > R 中的 0 使得对于所有 n ≥ n0 我们有 f(n) ≥ cg(n)。
现在我知道该算法正在执行 c * n(n - 1) 次迭代,产生 T(n)=c*n^2 - c*n。 前 3 行执行 O(1) 次,第 4 行循环执行 n - 1 次迭代,即 O(n) 。第 5 行循环 n - i 次迭代,这也是 O(n) 。内循环内容的每一行 (第 6-7 行)需要 (n-1)(ni) 或只是 O(1)?为什么?唯一的变化是执行 8.(d ← t) 的次数,但它必须小于或等于O(n^2)。
那么,我应该如何写一个好的完整的证明 T(n) = O(n^2),T(n) = Ω(n^2) 和 T(n) = θ(n^2)? 提前致谢
I've decided to try and do a problem about analyzing the worst possible runtime of an algorithm and to gain some practice.
Since I'm a beginner I only need help in expressing my answer in a right way.
I came accros this problem in a book that uses the following algorithm:
Input: A set of n points (x1, y1), . . . , (xn, yn) with n ≥ 2.
Output: The squared distance of a closest pair of points.
ClosePoints
1. if n = 2 then return (x1 − x2)^2 + (y1 − y2)^2
2. else
3. d ← 0
4. for i ← 1 to n − 1 do
5. for j ← i + 1 to n do
6. t ← (xi − xj)^2 + (yi − yj)^2
7. if t < d then
8. d ← t
9. return d
My question is how can I offer a good proof that T(n) = O(n^2),T(n) = Ω(n^2) and T (n) = Θ(n^2)?,where T(n) represents the worst possible runtime.
I know that we say that f is O(g),
if and only if there is an n0 ∈ N and c > 0 in R such that for all
n ≥ n0 we have
f(n) ≤ cg(n).
And also we say that f is Ω(g) if there is an
n0 ∈ N and c > 0 in R such that for all n ≥ n0 we have
f(n) ≥ cg(n).
Now I know that the algoritm is doing c * n(n - 1) iterations, yielding T(n)=c*n^2 - c*n.
The first 3 lines are executed O(1) times line 4 loops for n - 1 iterations which is O(n) . Line 5 loops for n - i iterations which is also O(n) .Does each line of the inner loop's content
(lines 6-7) takes (n-1)(n-i) or just O(1)?and why?The only variation is how many times 8.(d ← t) is performed but it must be lower than or equal to O(n^2).
So,how should I write a good and complete proof that T(n) = O(n^2),T(n) = Ω(n^2) and T (n) = Θ(n^2)?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
计算
t
更改其值的次数。由于更改t
是执行的最内层操作,因此找出发生的次数可以让您了解整个算法的复杂性。因此,
t
变化的次数为1 + 2 + ... + n - 1
。该总和等于n(n - 1) / 2
。这主要是0.5 * n^2
。现在只要找到合适的常数,就可以证明这是
Ω(n^2), O(n^2), θ(n^2)
。Count the number of times
t
changes its value. Since changingt
is the innermost operation performed, finding how many times that happens will allow you to find the complexity of the entire algorithm.So the number of times
t
changes is1 + 2 + ... + n - 1
. This sum is equaln(n - 1) / 2
. This is dominated by0.5 * n^2
.Now just find appropriate constants and you can prove that this is
Ω(n^2), O(n^2), Θ(n^2)
.对于大的
n
,T(n)=c*n^2 - c*n
接近于c*n^2
,这是以下的定义O(n^2)
。T(n)=c*n^2 - c*n
approachesc*n^2
for largen
, which is the definition ofO(n^2)
.如果观察两个
for
循环,每个for
循环都会给出 O(n),因为每个循环都以线性方式递增/递减。因此,两个循环组合起来的复杂度大致为 O(n^2)。 big-oh 的重点是找到主导项——系数并不重要。我强烈建议以正确的方式格式化您的伪代码,使其不含糊。无论如何,if 和 else 循环不会影响算法的复杂性。让我们观察各种定义:
因此您所需要做的就是找到满足答案的约束。
if you observe the two
for
loops, eachfor
loop gives an O(n) because each loop is incrementing/decrementing in a linear fashion. hence, two loops combined roughly give a O(n^2) complexity. the whole point of big-oh is to find the dominating term- coeffecients do not matter. i would strongly recommend formatting your pseudocode in a proper manner in which it is not ambiguous. in any case, the if and else loops do no affect the complexity of the algorithm.lets observe the various definitions:
so all you need are to find constraints which satisfy the answer.