简体   繁体   English

离散数学Big-O表示法复杂度

[英]Discrete Mathematics Big-O notation Algorithm Complexity

I can probably figure out part b if you can help me do part a. 如果您可以帮助我完成a部分,那么我可能可以找出b部分。 I've been looking at this and similar problems all day, and I'm just having problems grasping what to do with nested loops. 我整天都在研究这个问题以及类似的问题,而在掌握如何处理嵌套循环方面却遇到了问题。 For the first loop there are n iterations, for the second there are n-1, and for the third there are n-1.. Am I thinking about this correctly? 对于第一个循环,有n次迭代,对于第二个循环,有n-1个,对于第三个循环,有n-1个。我是否正确地考虑了这一点?

Consider the following algorithm, 考虑以下算法,
which takes as input a sequence of n integers a1, a2, ..., an 它以n个整数a1,a2,...,an的序列作为输入
and produces as output a matrix M = {mij} 并产生矩阵M = {mij}作为输出
where mij is the minimum term 其中mij是最小项
in the sequence of integers ai, a + 1, ..., aj for j >= i and mij = 0 otherwise. 在整数ai的序列中,对于j> = i,否则mij = 0的a +1,...,aj。

initialize M so that mij = ai if j >= i and mij = 0 初始化M,以便如果j> = i且mij = 0,则mij = ai

for i:=1 to n do
    for j:=i+1 to n do
        for k:=i+1 to j do
            m[i][j] := min(m[i][j], a[k])
        end
    end
end
return M = {m[i][j]}

(a) Show that this algorithm uses Big-O(n^3) comparisons to compute the matrix M. (a)证明该算法使用Big-O(n ^ 3)比较来计算矩阵M。
(b) Show that this algorithm uses Big-Omega(n^3) comparisons to compute the matrix M. (b)证明该算法使用Big-Omega(n ^ 3)比较来计算矩阵M。

Using this face and part (a), conclude that the algorithm uses Big-theta(n^3) comparisons. 使用该脸部和(a)部分,可以得出结论,该算法使用Big-theta(n ^ 3)比较。

In part A, you need to find an upper bound for the number of min ops. 在A部分中,您需要找到min操作数的上限。

In order to do so, it is clear that the above algorithm has less min ops then the following: 为此,很明显上述算法的min操作数少于以下算法:

for i=1 to n
  for j=1 to n //bigger range then your algorithm
    for k=1 to n //bigger range then your algorithm
        (something with min)

The above has exactly n^3 min ops - thus in your algorithm, there are less then n^3 min ops. 上面的恰好具有n ^ 3 min的运算量-因此,在您的算法中, n^3分钟的运算量会更少

From this we can conclude: #minOps <= 1 * n^3 (for each n > 10, where 10 is arbitrary). 由此我们可以得出结论: #minOps <= 1 * n^3 (对于每个n> 10,其中10是任意的)。
By definition of Big-O , this means the algorithm is O(n^3) 根据Big-O的定义 ,这意味着算法为O(n^3)

You said you can figure B alone, so I'll let you try it :) 您说您可以单独计算B,所以我让您尝试:)


hint: the middle loop has more iterations then for j=i+1 to n/2 提示:中间循环的迭代次数更多,for j=i+1 to n/2

For each iteration of outer loop inner two nested loop would give n^2 complexity if i == n . 对于外部循环的每次迭代,如果i == n则内部两个嵌套循环将给出n^2复杂度。 Outer loop will run for i = 1 to n . 外循环将针对i = 1 to n So total complexity would be a series like: 1^2 + 2^2 + 3^2 + 4^2 + ... ... ... + n^2 . 因此,总复杂度应为: 1^2 + 2^2 + 3^2 + 4^2 + ... ... ... + n^2 This summation value is n(n+1)(2n+1)/6 . 该总和值为n(n+1)(2n+1)/6 Ignoring lower order terms of this summation term ultimately the order would be O(n^3) 忽略该求和项的低阶项,最终该阶为O(n^3)

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

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