Download presentation
Presentation is loading. Please wait.
1
Assignment 2 Tze calculations
2
If a and b are constant, real numbers then loga n and logb n differ by a constant factor. What is the value of this constant? Recall the standard identity: loga n = logb n/logb a which we can re-write as loga n = (1/logb a)* logb(n); thus loga n differs by a constant factor of 1/logb a from logb n. Symmetrically we could argue logb n = loga n/loga b = (1/loga b)* loga n; thus logb n differs by a constant factor of 1/loga b from loga n. Either answer is acceptable.
3
a) Give a valid time complexity for A in big-O notation.
Suppose the exact time required for an algorithm A is given by the equation above: a) Give a valid time complexity for A in big-O notation. b) Give a second (different) time complexity for A in big-O notation. c) Give the time complexity for A in big- notation. a) Answer: O(n2:5), or any higher power of n. Explanation: O(n0:5) = O(sqrt of n) grows faster than log n, so the second term must grow more slowly than O(n2:5). By a similar argument the second term can't grow faster than O(n1:5). O(n2:5 clearly grows faster than the third term which is constant. For the 5th term, n grows faster than log nk for any k, so the 5th term can't grow faster than O(n2). b) Answer: O(n2:5), or any higher power of n that is different from the answer in a). c) Answer: (n2:5). Explanation: Since the fourth term is inarguably (n2:5), the argument in the solution to a) proves that TA(n) 2 (O(n2:5)
4
Use the definition of big-O to prove the above equation
Hint: you need to find a specific c and n0 (as in the definition for big-O) such that f(n) < cn2 for all n > n0. Recall the denition of O(f(n)): A function f(n) is O(g(n)) if there exists constants c; n0 such that f(n) cg(n) for all n > n0. Naturally, for this question f(n) = 5n2 + 2n + 6. We want to prove that f(n) 2 O(n2), so g(n) = n2. Now the problem has become, nd constants c and n0 such that 5n2 + 2n + 6 cn2 for all n > n0. It is easy to see that 2n + 6 n2 for n 4. It then follows that 2n + 6 n2 5n2 + 2n n2 for all n 4 which exactly what we were looking for, for c = 6, n0 = 4. Thus, f(n) 2 O(n2).
5
b) Express the function you obtained in part a) in big-O notation.
a) Determine the exact number of statements that are executed when we run this code fragment as a function of n. Show all of your calculations. b) Express the function you obtained in part a) in big-O notation. This is a classic quadratic loop. The inner loop executes for values of j from 0 to n 1. Each time through the loop, 2 statements are executed (the loop condition and the println). This is a total of 2n statements. There is an extra statement for when the loop condition becomes false. So the total number of statements for the inner loop is 2n + 1. The outer loop executes for values of i between 0 and n 1. For each of those values of i, we execute the 2n + 1 statements for the inner loop, plus an additional statement for the outer loop condition, or 2n+2 statements per iteration of the outer loop. There are n iterations of the outer loop, so this totals to n(2n + 2) + 1 where the extra +1 is for the outer loop condition being false. Thus the number of statements executed is exactly 2n2 + 2n + 1 or O(n2) or (n2).
7
a) Determine the exact number of statements that are executed when we run this code fragment in the worst case. Your answer should be a function of n = a:length. Show all of your calculations. b) Determine one or more active operations. Find the number of times the active operation(s) are executed. Your answer should be a function of n = a:length. Show all of your calculations (refer back to calculations in part a) if need be, don't repeat arguments unnecessarily). c) Express the functions obtained in parts a) and b) in Big-O notation. Part a): As the question says, we let n = a:length and express our answer as a function Tsift(n). First let's consider how many statements are executed during one iteration of the while loop. Since this is a worst case analysis, we assume that both if statements are always true (which means the return statement that terminates the loop early never gets executed). Thus we execute 7 statements per loop iteration. Next we must determine how many times the loop executes. In the worst case, start = 0 and end = a:length1. Our loop counter root. Each time through the loop, root gets updated to root2+2 (child = root2+1, then the first if statement is always true, making child = child+1 = root2+2, then the second if statement is true causing root = child = root ). Let's consider the value of root each time through the loop: Iteration root 1 0 = 2 = 6 4 ( ) = 14 5 (( ) ) = 30 6 ((( ) ) ) = 62 : : : : : : i Pi1 j=1 2j
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.