an + c(1 + n/3) + c(2n/3 + 4) <= cn, or an + c + cn/3 + 2cn/3 + 4c <= cn, or an + c + 4c <= 0.Since all of the terms on the left-hand side of the inequality are positive, there is no value of c that will make the inequality true.
Note that showing that the recurrence does not have a linear solution isn't quite the same thing as showing that the algorithm doesn't have linear time complexity. For one thing, the recurrence is an overestimate, or pessimistic estimate, of the time required by the algorithm.
By analogy with mergesort, the recursion tree method of Section 4.2 of the text can be used to observe there are logarithmically many levels of the recursion tree, each with linear cost. This gives an overall worst-case time complexity of Θ(n log n).
More formally, the relevant recurrence is
Although the algorithm of this exercise may not have any obvious advantages over the other
Θ(n log n) sorting algorithms, it does exhibit very good locality of reference. That is, data that is needed at any point of the algorithm is likely to be stored very close to data that has been recently used. This makes the algorithm a useful one for sorting sequences that are too large to fit into main memory.
0 3000 1750 4750 4500 - 1 1 3 3
0 1000 3000 3500 - 2 3 3
0 4000 3000 - 3 3
0 2000 - 4
0 -
If the original matrices are called A1 through A5, then the optimal order of the computation is given by
E(dC-h) + F(dD-h) - C(h-dC) - D(h-dD) = (E-C)(dC-h) + (F-D)(dD-h)Since C and D are the lowest costs, the factors (E-C) and (F-D) are nonnegative. And since h is the lowest (i.e., the highest numbered) level, the factors dC-h and dD-h are nonpositive. So the overall change is nonpositive, and the resulting tree O' has cost at least as low as O.
And since O is optimal, O' has cost at least as high as O's cost. So O' is an optimal solution.
It's not hard to see that O'' is an optimal solution to the new instance of size n-1. If there were a better solution B, we could replace its leaf with cost C+D by a nonleaf with two children of costs C and D to get a solution to the original instance of size n. The arguments of the previous paragraph show that the resulting tree would have a cost that exceeds B's cost by exactly C+D. But this cost would be cheaper than O's cost, which exceeds the cost of O'' by exactly C+D. Since O is optimal, we get a contradiction, and so we may conclude that no such B can exist.
Now by the choice of n, the greedy solution to the instance of size n-1 is optimal, and thus has a cost equal to that of O''. Adding C+D to both sides of this equation gives that the cost of G is equal to the cost of O', which is optimal for the instance of size n. But we assumed that G wasn't optimal for this instance. This contradiction shows that there is no smallest value of n for which there exists a nonoptimal greedy solution, and thus that the greedy algorithm always gives the optimal solution.