80 likes | 97 Views
Learn to find running time of algorithms, analyze recurrence equations, and understand asymptotic notation. Practice probability with dice. Prepare for your midterm!
E N D
Problem 1: Running time of iterative algorithms 1. Find the running time of the following algorithms: (a) sample(A, n) sum = 0 for i = 1 to n do for j = 1 to n do for k = 1 to n/2 do sum = sum + A[i, j, k] (b) foo(n) for i = 1 to n do for j = 1 to i do print i*n + j
Problem 2: Recurrence Equations • Find the running time of the following algorithms: (a) fib(x) if x = 0 or x = 1 then return 1 else return fib(x-1) + fib(x -2) (b) review(n) exam(n) //assume this runs in Q(n) time review(n/3)
Analyzing algorithms • Know how to find the running time of recursive algorithms. • Know how to find the running time of iterative algorithms (e.g. Insertion sort from lecture 2). Find the number of times a comparison is executed. • Be able to express the running time of algorithms using O notation.
Problem 3: Asymptotic notation 3. f(n) = 20/n g(n) = 1/lg(n) Is f(n) o, O, Q, W or w of g(n)? (list all that apply)
Problem 4: Asymptotic notation 4. f(n) = lg(n) for n even g(n) = 0.5n2 + 37 3n2 for n odd a) Graph f(n) b) Is f(n) o, O, Q, W or w of g(n)? (list all that apply)
Problem 5. Probability 5. Suppose you throw two 4-sided dice (e.g. tetrahedral dice) with values of 1, 2, 3, or 4 on each side. a) What is the Sample Space? b) If event A is all the throws in which the sum of values is less than or equal to 3, list all the outcomes in event A.
Problem 5 continued • Suppose we define a discrete random variable, s, such that the value of s is the sum of the dice from a single throw. List the values of s for each outcome in the sample space. d) What is the expected value for the sum of the two dice?