















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Class: Object-Oriented Programming and Data Structures; Subject: Computer Science; University: Cornell University; Term: Fall 2008;
Typology: Study notes
1 / 23
This page cannot be seen from the preview
Don't miss anything!
















Lecture 22 CS2110 โ Fall 2008
Analysis of Merge-Sort
T(n) = c + d + e + f + 2T(n/2) + gn + h T(1) = i
Solving Recurrences
๏ง No general technique works for all recurrences
Solving Recurrences ๏ Recurrences are important when using divide & conquer to design an algorithm ๏ Solution techniques: ๏ง Can sometimes change variables to get a simpler recurrence ๏ง Make a guess, then prove the guess correct by induction ๏ง Build a recursion tree and use it to determine solution ๏ง Can use the Master Method ๏ท A โcookbookโ scheme that handles many common recurrences To solve T(n) = aT(n/b) + f(n) compare f(n) with nlogba ๏ Solution is T(n) = O(f(n)) if f(n) grows more rapidly ๏ Solution is T(n) = O(nlogba) if nlogba^ grows more rapidly ๏ Solution is T(n) = O(f(n) log n) if both grow at same rate ๏ Not an exact statement of the theorem โ f(n) must be โwell- behavedโ
Recurrence Examples ๏ T(n) = T(n โ 1) + 1 โ T(n) = O(n) Linear Search ๏ T(n) = T(n โ 1) + n โ T(n) = O(n 2 ) QuickSort worst-case ๏ T(n) = T(n/2) + 1 โ T(n) = O(log n) Binary Search ๏ T(n) = T(n/2) + n โ T(n) = O(n) ๏ T(n) = 2 T(n/2) + n โ T(n) = O(n log n) MergeSort ๏ T(n) = 2 T(n โ 1) โ T(n) = O( n )
1/10,000 sec 1/2500 sec 1/400 sec 1/100 sec 9/100 sec 1/10 sec 3.2 sec 5.2 min 2.8 hr 28.1 days 1/1000 sec 1 sec 35.7 yr 400 trillion centuries a 75-digit number of centuries 2.8 hr 3.3 trillion years a 70-digit number of centuries a 185-digit number of centuries a 728-digit number of centuries 10 20 50 100 300 n n 2 n n 5 n 2
The Fibonacci Function ๏ Mathematical definition: fib(0) = 0 fib(1) = 1 fib(n) = fib(n โ 1) + fib(n โ 2), n โฅ 2 ๏ Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, โฆ int fib(int n) { if (n == 0 || n == 1) return n; else return fib(n-1) + fib(n-2); } Fibonacci (Leonardo Pisano) 1170โ1240? Statue in Pisa, Italy Giovanni Paganucci 1863
The Fibonacci Recurrence int fib(int n) { if (n == 0 || n == 1) return n; else return fib(n-1) + fib(n-2); }
n
ฯ = (a+b)/b = b/a ฯ 2 = ฯ + 1 ฯ = = 1.618... 1 + โ 5 2 The Golden Ratio a b ratio of sum of sides (a+b) to longer side (b) = ratio of longer side (b) to shorter side (a)
Can We Do Better?
n
if (n <= 1) return n; int parent = 0; int current = 1; for (int i = 2; i โค n; i++) { int next = current + parent; parent = current; current = next; } return (current);
f n f n+ f n f n+ f n+ f n+ ๏ Let fn denote the n th (^) Fibonacci number ๏ง f 0 = 0 ๏ง f 1 = 1 ๏ง fn+2 = fn+1 + fn, n^ โฅ^0 ๏ Note that^ , thus ๏ Can compute the nth power of a matrix by repeated squaring in O(log n) time ๏ Gives complexity O(log n) ๏ Just a little cleverness got us from exponential to logarithmic! ...But We Can Do Even Better! 0 1 1 1 = 0 1 1 1 f 0 f 1 = n
s 1 + s 2 - s 4 + s 6 s 4 + s 5 s 6 + s 7 s 2 - s 3 + s 5 - s 7 a b c d e f g h Matrix Multiplication in Less Than O(n
) (Strassen's Algorithm) ๏ Idea: naive 2 x 2 matrix multiplication takes 8 scalar multiplications, but we can do it in 7: where s 1 = (b - d)(g + h) s 5 = a(f - h) s 2 = (a + d)(e + h) s 6 = d(g - e) s 3 = (a - c)(e + f) s 7 = e(c + d) s 4 = h(a + b) =
S 1