Lecture Slides on Recurrences - Object-Oriented Programming | CS 2110, Study notes of Computer Science

Material Type: Notes; Class: Object-Oriented Programming and Data Structures; Subject: Computer Science; University: Cornell University; Term: Fall 2008;

Typology: Study notes

Pre 2010

Uploaded on 08/30/2009

koofers-user-gry
koofers-user-gry ๐Ÿ‡บ๐Ÿ‡ธ

10 documents

1 / 23

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Solving
Recurrences
Lecture 22
CS2110 โ€“ Fall 2008
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17

Partial preview of the text

Download Lecture Slides on Recurrences - Object-Oriented Programming | CS 2110 and more Study notes Computer Science in PDF only on Docsity!

Solving

Recurrences

Lecture 22 CS2110 โ€“ Fall 2008

Prelim tonight!

Analysis of Merge-Sort

Recurrence:

T(n) = c + d + e + f + 2T(n/2) + gn + h T(1) = i

First, simplify by dropping lower-order terms

Simplified recurrence:

T(n) = 2T(n/2) + cn

T(1) = d

How do we find the solution?

Solving Recurrences

๏‚Ÿ Unfortunately, solving recurrences is like solving

differential equations

๏‚ง No general technique works for all recurrences

๏‚Ÿ Luckily, can get by with a few common patterns

๏‚Ÿ You will learn some more techniques in CS 2800

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 big bang was 15 billion years ago (5ยท10^17 secs)
    • Source: D. Harel, Algorithmics

How long would it take @ 1 instruction / ฮผsec?

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); }

T(0) = c

T(1) = c

T(n) = T(n โ€“ 1) + T(n โ€“ 2) + c

๏‚Ÿ Solution is exponential in n

๏‚Ÿ But not quite O(

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?

๏‚Ÿ Number of times loop is executed? n โ€“ 1

๏‚Ÿ Number of basic steps per loop? Constant

๏‚Ÿ Complexity of iterative algorithm = O(n)

๏‚Ÿ Much, much, much, much, much, better than O(ฯ•

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

  • S 2
  • S 4 + S 6 S 4 + S 5 S 6
  • S 7 S 2
  • S 3
  • S 5
  • S 7 ๏‚Ÿ Break 2 n+ x 2 n+ matrices up into 4 2 n x 2 n submatrices ๏‚Ÿ Multiply them the same way 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) E F G H A B C D Now Apply This Recursively โ€“ Divide and Conquer! =