Homework 1 Solution ļ· Problem 2-1, Page 39 a. Insertion sort runs in θ(n2) worst case time on an input of size n, so on each sublist of k length it runs in θ(k2) time. We have n/k sublists, so n/k θ(k2) = θ(nk) time overall. b. At each level we have log(n/k) merges and each merge takes θ(n) time, so merging n/k sublists takes θ(nlog(n/k)) worst-case time. c. Total time θ(nk + nlg(n/k)) = θ(nlg). Both nk and nlg(n/k) must be no more than O(nlgn). Therefor the largest value of k is (lgn). d. We should pick a value of k where insertion sort is faster than merge sort. It would be best if k is the smallest size. ļ· Exercise 4.1-5, Page 75 Maximum-Subarray(A,j) left = 0 right = 0 temp_max = A[0] total_max = 0 for i = 1 to j do temp_max = MAX(A[i], temp_max + A[i]) if temp_max > total_max total_max= temp_max right = i if temp_max == A[i] left = i return (left, right, tatol_max) ļ· Exercise 4.4-9, Page 93 š Assume 1- α > α. The tree is full for ššš1/(1−α) levels, each contributing cn, we guess š š Ω(nššš1/(1−α)) = Ω(nlogn). It has ššš1/α levels, each contributing ≤ cn, so we guess š O(šššš1/α ) = O(nlogn). We show T(n)=θ(nlogn) by substitution. (let 1-α=β) T(n) ≤ dnlogn T(n)=T(αn) + T(βn) + cn ≤dαnlg(αn)+cβnlg(βn)+cn ≤dαnlgn+dβnlgn+dαnlgα+dβnlgβ+cn ≤dnlgn+(d(αlgα+βlgβ)+c)n ≤dnlgn it is true for the value of d where (d(αlgα+βlgβ)+c≤0), therefor: d ≤ d ≥ −c αlgα + (1 − α)lg(1 − α) c −αlgα − (1 − α)lg(1 − α) In order to show the lower bound we should do the same manipulation to show that T(n) ≥ dnlogn. Therefor T(n) = θ(nlogn). ļ· Problem 4-1, Page 107 a. T(n) = 2T(n/2) + n4 a=2, b=2, f(n) = n4 nlog22= n Case 3 of master’s method F(n) = n4 = Ω(nlog22 + 2) a/bk = 2/24 = 1/8 < 1 Θ(n4) b. T(n) = T(7n/10) + n a=1, b=10/7, f(n) = n n^(log10/71)= 1 Case 3 of the master’s method F(n) = n = Ω(n^(log10/71+1) a/bk = 1/(10/7)1 = 7/10 < 1 Θ(n) c. T(n) = 16T(n/4) + n2 a=16, b=4, f(n) = n2 n^(log416) = n2= f(n) = n2 Case 2 of the master method where k = 0 T(n) = Θ(n^(log416) log 0+1 n) = Θ(n2 log 1 n) = Θ(n2 log n) d. F. T(n) = 2T(n/4) + sqrt(n) a=2, b=4, f(n) = sqrt(n) n^(logb a) = n^(log4 2) = n1/2 = sqrt(n) Case 2 of the master method where k =0 T(n) = Θ(n^(log42) log 0+1 n) = Θ(n1/2 log 1 n) = Θ(sqrt(n) log n) ļ· Problem 5 Missing_item(A) n=length(A) item = (n*(n+1))/2 for i=1 to n do item = item – A[i] return item