I can probably figure out part b if you can help me do part a. 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?
Consider the following algorithm,
which takes as input a sequence of n integers a1, a2, …, an
and produces as output a matrix M = {mij}
where mij is the minimum term
in the sequence of integers ai, a + 1, …, aj for j >= i and mij = 0 otherwise.
initialize M so that mij = ai if j >= i and mij = 0
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.
(b) Show that this algorithm uses Big-Omega(n^3) comparisons to compute the matrix M.
Using this face and part (a), conclude that the algorithm uses Big-theta(n^3) comparisons.
In part A, you need to find an upper bound for the number of
minops.In order to do so, it is clear that the above algorithm has less
minops then the following:The above has exactly n^3
minops – thus in your algorithm, there are less thenn^3min ops.From this we can conclude:
#minOps <= 1 * n^3(for each n > 10, where 10 is arbitrary).By definition of Big-O, this means the algorithm is
O(n^3)You said you can figure B alone, so I’ll let you try it 🙂
hint: the middle loop has more iterations then
for j=i+1 to n/2