Solution of assignment 1 R-1.10 Algorithm Loop1(n): s0 for i 1 to n do 1 (assign a value) (n+1) assignments & (n+1) comparisons & n increments of i ss+i n * (one addition + one assignment) (5n+3) total operations f(n)=5n+3, which is in O(n) running time. Proof: Let g(n)=n. We can find a c = 6, and n0 = 2, such that for every n >= n0, f(n) <= cg(n). So, the running time of Loop1 method is O(n). R-1.14 Algorithm Loop5(n): s0 for i 1 to n2 do for j 1 to i do ss+i 1 (assign a value) (n2+1) assignments & (n2+1) comparisons & n2 increments of i n2 3 7 3i 2 n4 n2 operations 2 2 i 1 n2 2i n 4 n 2 operations i 1 (5n4/2+15n2/2+3) total operations f(n) = 5n4/2+15n2/2+3. By omitting the lower-order term and the constant factor, we will have that this algorithm is in O(n4). Proof: Let g(n) = n4. We can find a c = 3 and n0 = 4, such that for every n >= n0, f(n) <= cg(n). So, the running time of Loop5 method is O(n4). R-1.15 Proof: Step1: f(n) is O(g(n)) there exist constants c1 and n1, such that for every n >= n1, f(n) <=c1g(n). Similarly, d(n) is O(h(n)) there exist constants c2 and n2, such that for every n >= n2,d(n) <=c2h(n). Step2: let c = max (c1, c2), and n0 = max (n1, n2). Then for every n >= n0, f(n) + d(n) <= c1g(n) + c2h(n) <= c (g(n) + h(n)), so, f(n) + d(n) is O(g(n)+h(n)). R-1.19 Proof: Let f(n) = (n+1)5and g(n) = O(n5). We need to find constants c and n0, such that for every n >= n0 , f(n) <= cg(n). The condition is equivalent to (n+1)5 <= cn5 1 n+1 <= c1/5 n (c1/5-1)n >= 1 Let c=2, then the condition is equivalent to n >= 6.725 Thus, we can pick n0 = 7. So, we have c = 2 and n0 = 7, such that for every n>=n0, f(n) <= cg(n). So, (n+1)5 is O(n5). R-1.23 Proof: Let f(n) = n3logn, g(n) = n3 . We need to find constants c and n0, such that for every n >= n0, f(n) >= cg(n). The condition is equivalent to n3logn >= cn3 logn >= c Let c = 1 and n0 = 2. Then for every n >= n0, we have logn >= c, that is, f(n) >= cg(n). Thus, n3logn is Ω(n3). R-1.25 Proof: Step1: f(n) is O(g(n)) there exist constants c1 and n1, such that for every n >= n1, f(n) <=c1g(n). Similarly, d(n) is O(h(n)) there exist constants c2 and n2, such that for every n >= n2,d(n) <=c2h(n). Step2: let c = c1* c2 and n0 = max (n1, n2). Then for every n>= n0, f(n) <=c1g(n) and d(n) <=c2h(n). Because all terms>=0, we have f(n)* d(n)<= c1* c2* g(n)*f(n) f(n)* d(n)<= c* g(n)*f(n). Thus for every n >= n0, f(n)* d(n)<= c* g(n)*f(n), so, f(n)* d(n)is O(g(n)*f(n)). R-5.4 a. T (n) 2T (n / 2) log n n logb a n log2 2 n , f(n)=logn. Let , f(n) is O(n0.5), that is, f(n)is O(n logb a ) Thus, we are in case 1. This means T ( n ) is (n) by the master method. b. T (n) 8T (n / 2) n2 n logb a n log2 8 n3 , f(n)=n2. Let 1 , n 2 is O(n3 ) , that is, f(n)is O(n logb a ) . Thus, we are in case 1. This means T ( n ) is (n3 ) by the master method. c. T (n) 16T (n / 2) (n log n)4 2 n logb a n log2 16 n 4 . F(n)= (nlogn)4. Let k 4 , (n log n) 4 is (n 4 log k n) , that is, f (n) (n logb a log k n) . Thus we are in case 2. This means T ( n ) is (n4 log5 n) d. T (n) 7T (n / 3) n n logb a nlog3 7 , f(n)=n. Let log3 7 1 , then n is O(nlog3 7 ) , that is, f(n)is O(n logb a ) . Thus, we are in case 1. This means T ( n ) is (nlog3 7 ) e. T (n) 9T (n / 3) n3 log n n logb a n log3 9 n 2 , f(n)=n3logn. let 1 , then, n3 log n is (n 2 ) , that is f(n) is (n logb a ). af (n / b) 9 f (n / 3) 9((n / 3)3 log(n / 3)) (n3 / 3) log(n / 3) let 1/ 3 , d=1, then, af (n / b) (n3 / 3) log(n / 3) (n3 / 3) log(n) f (n) for all n>=d. Thus, we are in case 3. This means T ( n ) is (n3 log n) Question 8 log n; n; nlogn; n2; n2 + log n; n− n3 + 7n5; 2n ; n!; nn; Actually n2 and n2 + log n are the same. 3