




















































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





















































Administrative
■ (^) 1 8.5x11 crib sheet allowed ○ (^) Both sides, mechanical reproduction okay ○ (^) You will turn it in with the exam
Review: Induction
■ (^) S(k) is true for fixed constant k ○ (^) Often k = 0 ■ (^) S(n) S(n+1) for all n >= k
Proof By Induction
■ (^) Show formula is true when n = k
■ (^) Assume formula is true for an arbitrary n
■ (^) Show that formula is then true for n+
Induction Example: Geometric Closed Form
■ (^) Basis: show that a^0 = (a0+1^ - 1)/(a - 1) a^0 = 1 = (a^1 - 1)/(a - 1) ■ (^) Inductive hypothesis: ○ (^) Assume a^0 + a^1 + … + an^ = (an+1^ - 1)/(a - 1) ■ (^) Step (show true for n+1): a^0 + a^1 + … + an+1^ = a^0 + a^1 + … + an^ + an+ = (an+1^ - 1)/(a - 1) + an+1^ = (an+1+1^ - 1)/(a - 1)
Review: Asymptotic Performance
○ (^) Running time ○ (^) Memory/storage requirements ■ (^) Use the RAM model: ○ (^) All memory equally expensive to access ○ (^) No concurrent operations ○ (^) All reasonable instructions take unit time (^) Except, of course, function calls ○ (^) Constant word size
Review: Asymptotic Notation
■ (^) f(n) is O(g(n)) if there exist positive constants c and n 0 such that f(n) c g(n) for all n n 0 ■ (^) Formally, O(g(n)) = { f(n): positive constants c and n 0 such that f(n) c g(n) n n 0
■ (^) A polynomial of degree k is O( nk )
Review: Asymptotic Notation
0 such that 0 cg(n) f(n) n n 0
1 ,^ c 2 , and^ n 0 such that c 1 g(n) f(n) c 2 g(n) n n 0 ■ (^) f(n) = (g(n)) if and only if f(n) = O(g(n)) AND f(n) = (g(n))
Review: Merge Sort MergeSort(A, left, right) { if (left < right) { mid = floor((left + right) / 2); MergeSort(A, left, mid); MergeSort(A, mid+1, right); Merge(A, left, mid, right); } } // Merge() takes two sorted subarrays of A and // merges them into a single sorted subarray of A. // Code for this is in the book. It requires O(n) // time, and does require allocating O(n) space
Review: Analysis of Merge Sort
● (^) So T(n) = (1) when n = 1, and 2T(n/2)
Review: Solving Recurrences
■ (^) A.k.a. the “making a good guess method” ■ (^) Guess the form of the answer, then use induction to find the constants and show that solution works ■ (^) Example: merge sort ○ (^) T(n) = 2T(n/2) + cn ○ (^) We guess that the answer is O(n lg n) ○ (^) Prove it by induction ■ (^) Can similarly show T(n) = Ω(n lg n), thus Θ(n lg n)
Review: Solving Recurrences ● (^) The “iteration method” ■ (^) Expand the recurrence ■ (^) Work some algebra to express as a summation ■ (^) Evaluate the summation ● (^) We showed several examples including complex ones: ^ 1 1 ( ) cn n b n aT c n T n
log log log log log
a a a a a b b b b b
Review: Heaps
16 4 10 14 7 9 3 2 8 1 A = 16 14 10 8 7 9 3 2 4 1