CISC 615 Introduction Design and Analysis of Algorithms Dr. Jerry A. Smith WRITTEN ASSIGNMENT 1 ANSWER KEY 1. Problem 1-1) In general, this problem requires f(n) = some time period be solve for a value n. This can be done for all case expect n lg n and n!, which will be evaluated numerically. For example, F(n) = lg n microsec = 1 sec => 2lg n = 21E6 => n = 21E6 lg n n0.5 n n lg n n2 n3 2n n! 1 second 21E6 1.00E12 1E6 62746 1.00E03 100 19 9 1 minute 26E7 3.60E15 6E7 2.8E6 7.75E03 391 25 10 1 hour 23.6E9 1.30E19 3.6E9 1.33E8 6.00E04 1532 31 12 1 day 28.64E10 7.46E21 8.64E10 2.75E9 2.94E05 4420 36 13 1 month 22.59E12 6.72E24 2.59E12 7.18E10 1.61E06 13736 41 15 1 year 23.15E13 9.95E26 3.15E13 7.97E11 5.62E06 31593 44 16 1 century 23.15E15 9.95E30 3.15E15 6.86E13 5.62E07 146645 51 17 In solving for problems like n! and n lg n, numerically solving in Excel is an acceptable approach. For example, let cell A1 equal n and cell A2 equal “=FACT(A1)” Next, we recognize 1 second equals 1x106 microseconds. So, increase cell A1 until cell A2 equals 1x106. Rather than manually changing cell A1, you could use Excel’s Goal Seek function. 2. Exercise 2.2-2) n ← length[A] for j ← 1 to n-1 do smallest ← j for i ← j + 1 to n do if A[i] < A[smallest] Cost c1 c2 c3 c4 Time 1 n n-1 n n n(n + 1) tj = ! j = "1 ! 2 j =2 j =2 c5 n " (t ! 1) = j j =2 n n !1 " ( j ! 1) = " j = j =2 then smallest ← i c6 j =1 n " (t j n(n ! 1) 2 ! 1) j =2 exchange A[j] ↔ A[smallest] c7 n-1 1 CISC 615 Introduction Design and Analysis of Algorithms Dr. Jerry A. Smith The algorithm maintains the loop invariant that at the start of each iteration of the outer for loop, the subarray A[1, …, j-1] consists of the j-1 smallest elements in the array A[1, …, n], and this subarray is the sorted order. After the first n-1 elements, the subarray A[1, …, n-1] contains the smallest n-1 elements, sorted, and therefore element A[n] must be the largest element. Best Case) Best-case will occur when the array is already sorted, thus c6 will never be executed. Hence the running time is: T(n) = c1(1) + c2(n) + c3(n-1) + c4(n2+n-2)/2 + c5(n2-n)/2 + c7(n-1) T(n) = (c4/2 + c5/2)n2 + (c2 + c3 + c4/2 – c5/2 + c7)n + (c1 – c3 – c4 – c7) T(n) = a n2 + b n + c T(n) = Θ(n2) Worse Case) Worst-case will occur when the array is unsorted in reverse order (i.e. <5, 4, 3, 2, 1>), thus c6 will occur n(n-1)/2 time. Hence the running time is: T(n) = c1(1) + c2(n) + c3(n-1) + c4(n2+n-2)/2 + c5(n2-n)/2 + c6(n2-n)/2 + c7(n-1) T(n) = (c4/2 + c5/2 + c6/2)n2 + (c2 + c3 + c4/2 – c5/2 - c6/2 + c7)n + (c1 – c3 – c4 – c7) T(n) = a n2 + b n + c T(n) = Θ(n2) The running time for the algorithm is Θ(n2) for all cases. 3. Exercise 2.2-4) Modify the algorithm so it tests whether the input satisfies some special-case condition and, if it does, output a pre-computed answer. The best-case running time is generally not a good measure of an algorithm. 4. Exercise 2.3-1) 2 CISC 615 Introduction Design and Analysis of Algorithms Dr. Jerry A. Smith 3 9 26 38 41 49 52 57 3 26 41 52 3 41 3 9 38 49 57 26 52 41 52 38 57 26 38 9 49 57 9 49 5. Exercise 2.3-3) Prove by induction that 2 if n = 2 # T ( n) = " k !2T (n / 2) + n if n = 2 , for k > 1 is T (n) = n lg n To prove by induction, we must 1) show it is true for some base case, 2) hypothesize for some value of k, and 3) show that it holds for k+1. Base Case: when k=1 => n = 2 T(n) = n lg n = 2 lg 2 = 2 x 1 = 2. Hypothesis Step: T (n) = (n) lg(n) T (2 k ) = (2 k ) lg(2 k ) Inductive Step k+1: T (2 k +1 ) = 2T (2 k +1 / 2) + 2 k +1 = 2T (2 k ) + 2 k +1 [ ] T (2 k +1 ) = 2 2 k lg 2 k + 2 k +1 = 2 k +1 lg 2 k + 2 k +1 T (2 k +1 ) = 2 k +1 (lg 2 k + 1) T (2 k +1 ) = 2 k +1 lg 2 k +1 Q.E.D. 6. Exercise 2.3-5) 3 CISC 615 Introduction Design and Analysis of Algorithms Dr. Jerry A. Smith The binary search procedures takes a sorted array A, a value v, and a range [low … high] of the array A, in which to search for the value v. ITERATIVE-BINARY-SEARCH(A, v, low, high) while low ≤ high do mid !(low + high) / 2" If v = A[mid] then return mid if v > A[mid] then low ← mid + 1 else high ← mid - 1 return NIL RECURSIVE-BINARY-SEARCH(A, v, low, high) if low > high then return NIL mid ← !(low + high) / 2" if v = A[mid] then return mid if v > A[mid] then return RECURSIVE-BINARYSEARCH(A, v, mid+1, high) else return RECURSIVE-BINARYSEARCH(A, v, low, mid-1) Based on the comparison of v to the middle element in the search range, the search continues with the range halved. 4 CISC 615 Introduction Design and Analysis of Algorithms Dr. Jerry A. Smith n=0 n=1 n=2 lg n T(n/2) c c c c T(n/2) c c c T(n/4) c c lg n ... T(1) c Total:c lg n The depth of the tree is computed by noting that T(1) occurs when T(n/22)=T(1), or n/2j = 1. This means n = 2j, or j = lg n. 7. Exercise 3.1-4) (a) Is 2n+1 = O(2n)? f(n) = O(g(n)) 0 ≤ f(n) ≤ c g(n) where c > 0 and n > n0 Hence, 0 ≤ 2n+1 ≤ c 2n where n > n0 0 ≤ 2n+1 / 2n ≤ c where n > n0 0 ≤ 2 ≤ c where n > 0 c ≥ 2 and n > 0 Therefore, 2n+1 = O(2n) Q.E.D. (b) Is 22n = O(2n)? f(n) = O(g(n)) 0 ≤ f(n) ≤ c g(n) where n > n0 5 CISC 615 Introduction Design and Analysis of Algorithms Dr. Jerry A. Smith Hence, 0 ≤ 22n ≤ c 2n where n > n0 0 ≤ 22n / 2n ≤ c where n > n0 0 ≤ 2n ≤ c where n > n0 0 ≤ 2no ≤ c Now, 0 ≤ 22n ≤ 2no 2n where n > n0 0 ≤ 22n ≤ 2n+no where n > n0 2n ≤ n+ n0 for n > n0 n ≤ n0 for n > n0 Is a contradiction so, 22n ≠O(2n) Q.E.D. 8. Show that n(n-1)/2 is O(n2) f(n) = O(g(n)) 0 ≤ f(n) ≤ c g(n) where c > 0 and n > n0 Hence, 0 ≤ n(n-1)/2 ≤ c n2 where c > 0 and n > n0 0 ≤ 1/2-1/2n ≤ c where c > 0 and n > n0 0 ≤ 1/2-1/4 ≤ c where n ≥ 2 c = 1/4 Hence, 0 ≤ n(n-1)/2 ≤ n2/4 where n ≥ 2 Therefore, n(n-1)/2 = O(n2) 9. Using "big-O" notation, give the worst case running times of the following procedures as a function of n. procedure mystery (n: integer); var i,j,k: integer; begin for i := 1 to n-1 do for j := i + 1 to n do 6 CISC 615 Introduction Design and Analysis of Algorithms Dr. Jerry A. Smith for k := 1 to j do ... a statement requiring O(1) time end Line 1 2 Pseudo code for i := 1 to n-1 do for j := i + 1 to n do Cost c1 c2 Times n c3 n +1 n +1 n ! j= j =2 3 for k := 1 to j do n2 + n " 2 2 "" j = k =3 j = k 4 ... a statement requiring O(1) time c4 n n "" j = k =2 j =k n(2n + 5)(n ! 1) 6 n(n + 1)(n ! 1) 3 T(n) = c1n + c2(n2 + n –2)/2 + c3(n)(2n+5)(n-1)/6 + c4(n)(n+1)(n-1)/3 T(n) = (c3+ c4)n3/3 + (c2 + c3)n2/2 + (n1 + c2/2 – 5c3/6 – c4/3)n – c2 T(n) an3 + bn2 + cn + d T(n) = O(n3) 7
© Copyright 2025 Paperzz