简体   繁体   English

最坏情况下该算法的下限运行时间

[英]Lower bound running time of this algorith for worst case

I have an algorithm as follow: 我有一个算法如下:

在此输入图像描述

and even I have the anwser for finding lower bound of this algorithm: 甚至我有anwser来寻找这个算法的下界:

在此输入图像描述

But the problem is that I can not understand this: when we set i >= n/2 but n/4<=j<=n/2 then in this algorithm j can not get any value which is but as you see in the answer it says the middle loop iterates n/4? 但问题是我无法理解这一点:当我们设置i> = n / 2但是n / 4 <= j <= n / 2时,在这个算法中j不能得到任何值,但正如你在回答说中间循环迭代n / 4?

I am really confused why. 我真的很困惑为什么。

OK, I am not sure which part you are confused about, but let me rephrase lower bound explanation from your abstract, and, perhaps, the confusion part will clear up. 好吧,我不确定你对哪一部分感到困惑,但让我从你的摘要中重新解释下限解释,也许,混淆部分会清理。

First of all, the lower bound approach (kinda obvious stuff but omitted in your abstract and can be unobvious to beginner students). 首先,下限方法(有点明显的东西,但在你的摘要中省略,对初学者来说可能是不明显的)。 If you imagine set of all possible values of (i, j, k), we don't want to count them all, but we'll only count a smaller subset of them, defined by certain arbitrary restrictions. 如果您想象(i,j,k)的所有可能值的集合,我们不希望将它们全部计算,但我们只计算它们的较小子集 ,由某些任意限制定义。 It turns out it's easier to compute lower bound on a subset than on the entire set (because you can do simple math like multiplying lower bounds of ranges due to these restrictions), and transitively, this will also be a lower bound for the entire set. 事实证明,计算子集的下限比整个集合更容易(因为你可以做简单的数学运算,比如因为这些限制而乘以范围的下限),而且传统上,这也是整个集合的下限。 。

Now, these arbitrary restrictions are choosen the following: 1) Look at only i values >= n/2. 现在,这些任意限制选择以下内容:1)仅查看i值> = n / 2。 That means, instead of looking at [1..n], we look at [n/2..n]. 这意味着,不是看[1..n],而是看[n / 2..n]。 2) Taking into account the previous restriction, also restrict j: look at j values in range [n/4..n/2]. 2)考虑到先前的限制,也限制j:查看范围[n / 4..n / 2]中的j值。 The word "Consider" in your text applies to both (1) and (2)). 文中的“考虑”一词适用于(1)和(2))。 Note that the reason we can do that is that [n/4..n/2] is always a subset of the [1..i] range since we already decided that we only look at cases when i >= n/2. 请注意,我们可以这样做的原因是[n / 4..n / 2]始终是[1..i]范围的子集,因为我们已经决定只查看i> = n / 2时的情况。 Therefore limiting [1..i] to [n/4..n/2] is correct thing to do to get some lower bound. 因此,将[1..i]限制为[n / 4..n / 2]是正确的做法,以获得一些下限。

Now that we know i is [n/2..n] (at least n/2), and j is [n/4..n/2] (at least n/4), there are n/2*n/4 combinations of possible (i, j) pairs. 现在我们知道我是[n / 2..n](至少n / 2),并且j是[n / 4..n / 2](至少n / 4),有n / 2 * n / 4个可能的(i,j)对的组合。 For each of these pairs, the inner loop will do at least n/4-1 iterations (I am not sure why -1, perhaps to signify rounding down?), therefore we get n/2*n/4*(n/4-1) tuples of (i, j, k) is omega(n^3). 对于这些对中的每一对,内循环将至少进行n / 4-1次迭代(我不知道为什么-1,也许表示向下舍入?),因此我们得到n / 2 * n / 4 *(n / 4-1)(i,j,k)的元组是ω(n ^ 3)。

If a small subset of variants is omega(n^3), than the original set is omega(n^3) too. 如果一小部分变体是omega(n ^ 3),那么原始集合也是omega(n ^ 3)。

PS I didn't understand why you said "n/4<=j<=n/2 then in this algorithm j can not get any value". PS我不明白你为什么说“n / 4 <= j <= n / 2然后在这个算法j中得不到任何值”。 n/4 is smaller than n/2, so for big enough n values, j range will have some numbers. n / 4小于n / 2,因此对于足够大的n值,j范围将具有一些数字。

It looks to me like the number of iterations in the middle loop should always be less than or equal to number of iterations in inner loop because k would always go from 1 to j. 在我看来,中间循环中的迭代次数应始终小于或等于内循环中的迭代次数,因为k总是从1到j。

Below is an example of where I walked through the loop . 下面是我走过循环的一个例子。

array[1,2,3,4,5]

will produce the following iterations with the restrictions on the loops:

i=3,j=2,k=1 //i starts at 3 because of n/2 minimum
            //j can't go below or above 2 because of condition on middle loop
i=3,j=2,k=2
i=4,j=2,k=1
i=4,j=2,k=2
i=5,j=2,k=1
i=5,j=2,k=2

3 iterations of outer loop (at least n/2)
1 iteration of middle loop for each outer loop iteration (NOT at least n/4)
2 iterations of inner loop for each iteration of middle loop (at least n/4 - 1)

The text may have just swapped the descriptions for the bottom two loops. 文本可能刚刚交换了底部两个循环的描述。 If this is the case, the answer given would still be correct. 如果是这种情况,给出的答案仍然是正确的。

Regardless, the important thing to note is that it runs in O(n^3) time. 无论如何,重要的是要注意它在O(n ^ 3)时间内运行。

You can visualize number of iterations in the loop as 您可以将循环中的迭代次数可视化为

for( i in [1..n] )
  for( j in [1..i] )
     statement;

as (for n = 6): as(对于n = 6):

x 
x x 
x x x
x x x x
x x x x x
x x x x x x

this will give you complexity O(N*N) . 这将给你复杂度O(N*N) Number of iterations in innermost loop (in your sample) is also N dependent so it will give you one more N multiplier (third dimension). 最内层循环(在您的样本中)中的迭代次数也是N依赖的,因此它将为您提供一个N乘数(第三维)。 So x es will fill 1/32 (?) of cube volume. 因此x es将填充1/32(?)的立方体体积。 But as it is a cube formation then complexity will be O(N³) 但由于它是立方体形成,因此复杂性将为O(N³)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM