Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Big O notation wikipedia , lookup
Vincent's theorem wikipedia , lookup
Large numbers wikipedia , lookup
Approximations of π wikipedia , lookup
Collatz conjecture wikipedia , lookup
Proofs of Fermat's little theorem wikipedia , lookup
Factorization of polynomials over finite fields wikipedia , lookup
98 16. (a) Proof. Assume first that f = O(g) and f = Ω(g). Since f = Ω(g), there exist a positive constant C1 and a positive integer k1 such that f (n) ≥ C1 g(n) for every integer n ≥ k1 . Since f = O(g), there exist a positive constant C2 and a positive integer k2 such that f (n) ≤ C2 g(n) for every integer n ≥ k2 . Let k = max(k1 , k2 ). Then for every integer n ≥ k, C1 g(n) ≤ f (n) ≤ C2 g(n). Therefore, f = Θ(g). We now verify the converse. Assume that f = Θ(g). Then there are positive constants C1 and C2 and a positive integer k such that C1 g(n) ≤ f (n) ≤ C2 g(n) for every integer n ≥ k. Since C1 g(n) ≤ f (n) for every integer n ≥ k, it follows that f = Ω(g); while since f (n) ≤ C2 g(n) for every integer n ≥ k, it follows that f = O(g). (b) Proof. Assume first that f = Ω(g). Then there exist a positive constant C and a positive integer k such that f (n) ≥ Cg(n) for every integer n ≥ k. This, however, implies that g(n) ≤ C1 f (n) for every integer n ≥ k; so g = O(f ). For the converse, assume that g = O(f ). Then there exist a positive constant C ′ and a positive integer k ′ such that g(n) ≤ C ′ f (n) for every integer n ≥ k ′ . Hence f (n) ≥ C1′ g(n) for every integer n ≥ k ′ . Therefore, f = Ω(g). Exercises for Section 6.3. Analysis of Algorithms 1. Algorithm: Find a Number in a List s : a1 , a2 , . . . , an of n ≥ 3 Distinct Numbers That is Neither the Maximum nor the Minimum Number in the List. Input: An integer n ≥ 3 and a sequence s : a1 , a2 , . . . , an of n distinct numbers. Output: A number y in s that is neither the maximum nor the minimum number in s. 1. x := a1 2. z := a2 3. if x > z then t := x, x := z and z := t [If x > z, then the values of x and z are interchanged so that in every case, x < z.] 4. if x < a3 < z then y := a3 5. if a3 < x then y := x 6. if a3 > z then y := z 7. output y Since there is one comparison in each of Steps 3, 5 and 6 and two comparisons in Step 4, the time complexity of this algorithm (regardless of the value of n) is 5 = O(1). 2. Algorithm: Compute a1 a2 · · · an + a1 a2 · · · an−1 + · · · + a1 a2 + a1 for the Sequence s : a1 , a2 , . . . , an of n Numbers Input: A positive integer n and a sequence s : a1 , a2 , . . . , an of n numbers. Output: s = a1 a2 · · · an + a1 a2 · · · an−1 + · · · + a1 a2 + a1 . 1. s := an 2. for i := 1 to n − 1 do 3. s := an−i + an−i s 4. output s Since there is one addition and one multiplication in Step 3 and this step is performed n − 1 times, the time complexity of this algorithm is 2(n − 1) = 2n − 2 = Θ(n). 99 3. Algorithm: Compute (a1 + a2 + · · · + an )(a1 + a2 + · · · + an−1 ) · · · (a1 + a2 )a1 for the Sequence s : a1 , a2 , . . . , an of n Numbers Input: A positive integer n and a sequence s : a1 , a2 , . . . , an of n numbers. Output: x = (a1 + a2 + · · · + an )(a1 + a2 + · · · + an−1 ) · · · (a1 + a2 )a1 1. i := 1 2. x := a1 3. s := a1 4. while i < n do begin i := i + 1 s := s + ai x := sx end 5. output x Since there are n comparisons in Step 3, the time complexity of this algorithm is n = Θ(n). 4. Algorithm: Compute a1 a2 +a2 a3 +a3 a4 +· · ·+an−1 an +an a1 for the Sequence s : a1 , a2 , . . . , an of n Numbers Input: A positive integer n and a sequence s : a1 , a2 , . . . , an of n numbers. Output: s = a1 a2 + a2 a3 + a3 a4 + · · · + an−1 an + an a1 1. s := 0 2. an+1 := a1 3. for i := 1 to n do begin 4. x := ai ai+1 s := x + s end 4. output s There is one multiplication in Step 4 and one addition in Step 5. Since each step is performed n times, the time complexity of this algorithm is 2n = Θ(n). 5. Algorithm: Compute |a1 − a2 | + |a1 − a3 | + · · · + |a1 − an | + |a2 − a3 | + · · · + |an−1 − an | for the Sequence s : a1 , a2 , . . . , an of n ≥ 2 Numbers Input: An integer n ≥ 2 and a sequence s : a1 , a2 , . . . , an of n numbers. Output: s = |a1 − a2 | + |a1 − a3 | + · · · + |a1 − an | + |a2 − a3 | + · · · + |an−1 − an |. 1. s := 0 2. for i := 1 to n − 1 do begin 3. for j := i + 1 to n do begin 4. x := |ai − aj | 100 5. s := s + x end end 6. output s There is one subtraction in Step 4 and one addition in Step 5. These two steps are performed n − i times for each value of i (1 ≤ i ≤ n − 1). Therefore, the time complexity of this algorithm is 2(n − 1) + 2(n − 2) + · · · + 2 · 2 + 2 · 1 = 2[1 + 2 + · · · + (n − 1)] n(n − 1) = n2 − n = Θ(n2 ). = 2 2 6. Algorithm: Compute a1 a22 a33 · · · ann for a Sequence s : a1 , a2 , . . . , an of n ≥ 2 Numbers Input: An integer n ≥ 2 and a sequence s : a1 , a2 , . . . , an of n numbers. Output: y = a1 a22 a33 · · · ann . 1. x := an 2. y := an 3. for i := 1 to n − 1 do begin 4. x := an−i x y := xy end 5. output y There is one multiplication in Step 4 and one multiplication in Step 5. These steps are performed n−1 times and so the time complexity of this algorithm is 2(n−1) = 2n−2 = Θ(n). 7. (a) Algorithm: Compute an For a Real Number a and a Nonnegative Integer n. Input: A real number a and a nonnegative integer n. Output: p = an 1. p := 1 2. for i := 1 to n do 3. p := ap 4. output y (b) There is one multiplication in Step 3. Since Step 3 is performed n times, the time complexity of this algorithm is n = Θ(n). 8. (a) Algorithm: Compute f (a) = cn an + cn−1 an−1 + · · · + c1 a + c0 for Real Numbers cn , cn−1 , . . . , c1 , c0 , a, Where an , an−1 , . . ., a1 , a0 Are Computed by the Algorithm in Exercise 7. Input: A positive integer n, a real number a and sequences s : c0 , c1 , . . . , cn−1 , cn and s′ : a0 , a1 , . . . , an , where the numbers in s′ are computed using the algorithm in Exercise 7. Output: s = f (a) 1. s := 0 2. for i := 0 to n do 101 3. s := ci ai + s 4. output s (b) The algorithm in Exercise 7 has the time complexity i to compute ai for 1 ≤ i ≤ n. The time complexity to compute all of a0 , a1 , . . . , an is therefore 1 + 2 + · · · + n = n(n + 1)/2. Step 3 in the algorithm in (a) has one addition and one multiplication. Since Step 3 is performed n+1 times, the time complexity of the algorithm in (a) is n(n+1)/2+2(n+1) = (n + 1)(n + 4)/2 = Θ(n2 ). 9. (a) First, we write Horner’s algorithm. Input: A positive integer n and real numbers cn , cn−1 , . . . , c1 , c0 , a. Output: s = f (a) 1. s := cn 2. for i := 1 to n do 3. s := sa + cn−i 4. output s There is one multiplication, one addition and one subtraction in Step 3. Since this step is performed n times, the time complexity of this algorithm is 3n = Θ(n). (b) Horner’s algorithm is more efficient than the algorithm in Exercise 8. Exercises for Section 6.4. Searching and Sorting 1. Solution. In this case, k = 17, n = 9 and a1 = 3, a2 = 6, a3 = 7, a4 = 8, a5 = 15, a6 = 17, a7 = 19, a8 = 23, a9 = 24. By Steps 1 and 2, a = 1 and b = 9. Since 1 = a ≤ b = 9, Steps 4–8 are executed. By Step 4, p = 5. Among Steps 5–7, only a5 < k is true and so only the conclusion of Step 7 is performed, where a is assigned the value 5 + 1 = 6. The subsequence a6 , a7 , a8 , a9 is now considered. Since 6 = a ̸= b + 1 = 10, the conclusion of Step 8 is not performed. Since 6 = a ≤ b = 9, we go through the while loop at Step 3 again. In Step 4, we have p = 7. Among Steps 5–7, only a7 > k is true in Step 6 and the conclusion of Step 6 is performed. So b is assigned the value p − 1 = 6. Since 6 = a ̸= b + 1 = 7, we go through the while loop at Step 3 again. In Step 4, we have p = 6. Since a6 = k, the following is output: 17 is in position 6. Furthermore, a is assigned the number b + 2 = 8. Since none of the conditions in Steps 6–8 are satisfied, we return to the while loop at Step 3. However, 8 = a ≤ b = 6 is false and the algorithm ends. ! a=1 a1 a2 a3 a4 a5 a6 a7 a8 a9 k = 17 b=9 3 6 7 8 15 17 19 23 24 n=9 ... .. ...... . a=6 b=9 a6 17 a7 a8 19 23 a9 24 ... ... ...... . a=6 a6 b=6 17 “17 is in position 9” is output Figure 30: Applying the Binary Search Algorithm to Exercise 1