算法设计与分析课后习题答案

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Let c = acf and n0 = n0f . We show that we can get constants c > 0 and n0 ≥ 1 such that af (n) ≤ cg (n) for all n ≥ n0 . By the definition of Big-Oh, this proves that if f (n) is O (g (n)), then af (n) is O (g (n)), for any positive constant a. 2
1. (10%) Rank the following functions by asymptotic growth rate in non-decreasing order: (3 )n , 264 − 1, n3 , 0.0001n2, 10000n, log n2 , 2log n , n log n, n2n , 21000 , n, n2 log n, 2 2n , log n, n100 , 4n , log n3 , nn , n3 log n Answer: 264 − 1, 21000 , log n, log n2 , log n3 , n, 2log nபைடு நூலகம், 10000n, n log n, 0.0001n2 , n2 log n, 3 n n3 , n3 log n, n100 , ( 2 ) , 2n , n2n , 4n , nn . Remark: In the above ranking, if a function f (n) preceeds another function g (n) then f (n) ∈ O (g (n)) holds. Consequently, there are several valid answers. Indeed, several of the above ranked functions f (n), g (n) satisfy f (n) ∈ Θ(g (n)), which implies that both f (n) ∈ O (g (n)) and g (n) ∈ O (f (n)) hold. Note: (1) log n2 = 2 log n, log n3 = 3 log n (2) Grouping these functions into the following classes can help to clarify their orders. (a) (b) ( c) (d ) (e) (f ) (g ) (h) (i) Constant: 264 − 1, 21000 Logarithmic: log n, log n2 , log n3 Linear: n, 2log n , 10000n n log n Polynomial: 0.0001n2, n3 , n100 n2 log n n3 log n Exponential: ( 3 )n , 2n , n2n , 4n 2 nn
CS2210b Data Structures and Algorithms Due: Thursday, January 20th
Assignment 1 (concept): Solutions
Note, throughout Exercises 1 to 4, n denotes the input size of a problem.
Inequality (1) is equivalent to the inequality below. cn3 − 0.001n3 + 1000n2 log n + 100n − 5 ≥ 0 (2)
Inequality (2) must be true if the following inequality holds (where 1000n2 log n and 100n are dropped since they are positive, and −5 is multiplied by n3 ). cn3 − 0.001n3 − 5n3 ≥ 0 (3)
Inequality (5) can not be true since c must be a constant but n0.001 is unbounded. In fact, as soon as n > e1000 log(c) we have c < n0.001 . This is a contradiction with the assumption that we can find such a constant c. Therefore, n1+0.001 is not O (n). 4. (10%) Use the definition of Big-Oh to prove that if f (n) is O (g (n)), then af (n) is O (g (n)), for any positive constant a. Solution: From the fact that f (n) is O (g (n)) we can find constants cf > 0 and n0f ≥ 1 such that f (n) ≤ cf g (n), ∀n ≥ n0f . By multiplying both sides of the inequality of (7) by a (a > 0), we get af (n) ≤ acf g (n), ∀n ≥ n0f . (7) (6)
Inequality (3) is equivalent to (c − 0.001 − 5)n3 ≥ 0, which is true ∀n ≥ n0 when c = 5.001 and n0 = 1. Now, we have found c = 5.001 and n0 = 1 such that 0.001n3 − 1000n2 log n − 100n +5 ≤ cn3 for all n ≥ n0 . Therefore, we have proved that 0.001n3 − 1000n2 log n − 100n + 5 is O (n3 ). Remark: The pair of c and n0 is not unique. For instance, when c = 1 and n0 = 2, Inequality (1) always holds for n ≥ n0 . 3. (15%) Use the definition of Big-Oh to prove that n1+0.001 is not O (n). Solution: We prove that n1+0.001 is not O (n) by contradiction. Suppose that n1+0.001 is O (n), which means that we can find c > 0 and n0 ≥ 1 such that n1+0.001 ≤ cn, ∀n ≥ n0 . (4) By dividing both sides of the inequality of (4) by n (n ≥ 1) we obtain the following: c ≥ n0.001 (5)
5. (25%) We want to know how many students are taking both CS2210 and CS2211 this term. Let A and B be the class lists of CS2210 and CS2211. Each of A and B consists of unique student IDs of the corresponding class. To keep it simple, we assume that the two classes have the same number of students, denoted by n. 5.1 Write an algorithm in pseudocode to count the number of students who are taking both CS2210 and CS2211 this term. 5.2 Compute the worst case running time T (n) of your algorithm with respect to the class size n. 5.3 Give the best Big-Oh complexity characterization of T (n). Solution 1: 5.1 Algorithm countCommon(A, B, n) Input: Two integer arrays A and B with both size of n Output: Number of common elements in A and B e ← 0 //number of common elements for i ← 0 to n − 1 do for j ← 1 to n − 1 do if B [j ] = A[i] then e=e+1 break return e 5.2 The worst case occurs when there are no common elements in A and B. In such case, every element in A needs to be compared with every element in B. This algorithm involves a nested “for” loop. We analyze the inner-most “for” loop first. In each iteration of the inner “for” loop, only a number of constant c operations are performed (mainly one comparison). The number of iterations of the inner “for” loop is n. Thus, the total number of operations performed in this loop is cn. As for the outer (or first) “for” loop, the number of iterations is again n. In each iteration of the outer “for” loop, it performs the work of the inner loop. Therefore, the total work done by the outer “for” loop is n × cn = cn2 . Consequently T (n) = cn2 + c′ where c′ is the number of operations for initializing e and returning e at the end. 5.3 T (n) = cn2 + c′ is O (n2 ). (Proof is straightforward.)
Observe that the functions in the class (e) are not equivalent for the f (n) ∈ Θ(g (n)) relation. The same observation is true for the class (h). 2. (15%) Use the definition of Big-Oh to prove that 0.001n3 − 1000n2 log n − 100n + 5 is O (n3 ). Solution: To show that 0.001n3 − 1000n2 log n − 100n + 5 is O (n3 ), we must find constants c > 0 and n0 ≥ 1 such that 0.001n3 − 1000n2 log n − 100n + 5 ≤ cn3 , ∀n ≥ n0 . 1 (1)
相关文档
最新文档