算法的渐近运行时间

发布于 2024-10-19 04:27:17 字数 943 浏览 4 评论 0原文

我决定尝试做一个关于分析算法最坏可能运行时间的问题并获得一些练习。 由于我是初学者,我只需要帮助以正确的方式表达我的答案。 我在一本使用以下算法的书中遇到了这个问题:

输入:一组 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 技术交流群。

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

发布评论

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

评论(3

你爱我像她 2024-10-26 04:27:17

计算 t 更改其值的次数。由于更改 t 是执行的最内层操作,因此找出发生的次数可以让您了解整个算法的复杂性。

i = 1 => j runs n - 1 times (t changes value n - 1 times)
i = 2 => j runs n - 2 times
...
i = n - 1 => j runs 1 time

因此,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 changing t is the innermost operation performed, finding how many times that happens will allow you to find the complexity of the entire algorithm.

i = 1 => j runs n - 1 times (t changes value n - 1 times)
i = 2 => j runs n - 2 times
...
i = n - 1 => j runs 1 time

So the number of times t changes is 1 + 2 + ... + n - 1. This sum is equal n(n - 1) / 2. This is dominated by 0.5 * n^2.

Now just find appropriate constants and you can prove that this is Ω(n^2), O(n^2), Θ(n^2).

半山落雨半山空 2024-10-26 04:27:17

对于大的 nT(n)=c*n^2 - c*n 接近于 c*n^2,这是以下的定义O(n^2)

T(n)=c*n^2 - c*n approaches c*n^2 for large n, which is the definition of O(n^2).

诗笺 2024-10-26 04:27:17

如果观察两个 for 循环,每个 for 循环都会给出 O(n),因为每个循环都以线性方式递增/递减。因此,两个循环组合起来的复杂度大致为 O(n^2)。 big-oh 的重点是找到主导项——系数并不重要。我强烈建议以正确的方式格式化您的伪代码,使其不含糊。无论如何,if 和 else 循环不会影响算法的复杂性。

让我们观察各种定义:

大哦

• f(n) 是 O(g(n)) 如果 f(n) 是
渐近“小于或等于”
g(n)

大欧米茄

• f(n) 是 Ω(g(n)) 如果 f(n) 是
渐近“大于或等于”
到 g(n)

大西塔

• f(n) 是 θ(g(n)) 如果 f(n) 是
渐近“等于”g(n)

因此您所需要做的就是找到满足答案的约束。

if you observe the two for loops, each for 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:

Big-Oh

• f(n) is O(g(n)) if f(n) is
asymptotically “less than or equal” to
g(n)

Big-Omega

• f(n) is Ω(g(n)) if f(n) is
asymptotically “greater than or equal”
to g(n)

Big-Theta

• f(n) is Θ(g(n)) if f(n) is
asymptotically “equal” to g(n)

so all you need are to find constraints which satisfy the answer.

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