{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

algorithm.design.kleinberg.tardos.solutions.ch2 (6)

Algorithm Design

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: (a) We prove this for f(71) : 713. The outer loop of the given algorithm runs for exactly 71 iterations, and the inner loop of the algorithm runs for at most 71 iterations every time it is executed. Therefore, the line of code that adds up array entries AM through A]j] (for various 1’s and j’s) is executed at most 712 times. Adding up array entries AM through AU] takes 0(j — 1 + 1) operations, which is always at most 0(71). Storing the result in B]1,j] requires only constant time. Therefore, the running time of the entire algorithm is at most 712 - C(71), and so the algorithm runs in time 0(713). (b) Consider the times during the execution of the algorithm when 1 3 71/4 and j Z 371/4. In these cases, j — 1 + 1 Z 371/4 — 71/4 + 1 > 71/2. Therefore, adding up the array entries AM through A]j] would require at least 71/2 operations, since there are more than 71/2 terms to add up. How many times during the execution of the given algorithm do we encounter such cases? There are (71/4)2 pairs (1,j) with 1 3 71/4 andj Z 371/4. The given algorithm enumerates over all of them, and as shown above, it must perform at least 71/2 operations for each such pair. Therefore, the algorithm must perform at least 71/2 - (71/4)2 : 713/32 operations. This is 9(713), as desired. (C) Consider the following algorithm. For 1:1,2,...71 Set B[1,1+1] to A[1]+A[1+1] For k:2,3,...,71—1 For 1:1,2,...,71—/<: Set j:1+k Set B]1,j] to be B]1,j— 1]+A]j] This algorithm works since the values B]1,j — 1] were already computed in the previous iteration of the outer for loop, when 11 was j — 1 — 1, since j — 1 —1 < j — 1. It first computes B]1,1+ 1] for all 1 by summing AM with A]1+ 1]. This requires 0(71) operations. For each 11, it then computes all B]1,j] for j —1 : k by setting B]1,j] : B]1,j— 1] +A]j]. For each 11, this algorithm performs 0(71) operations since there are at most 71 B]1,j]’s such that j —1 : 11. There are less than 71 values of k to iterate over, so this algorithm has running time 0(712). 1ex474.221.961 ...
View Full Document

{[ snackBarMessage ]}