04 Recurrences and Lists

04 Recurrences and Lists - Solving Recurrences, then Lists,...

This preview shows pages 1–17. Sign up to view the full content.

Solving Recurrences, then Lists, Stacks, Queues, and Trees 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miller 21 January 2010 Reading for today: Chapters 16-17 (skim)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 Announcements HW1 Written: Due Tuesday, 26 Jan 2010 at start of class Correction: Quiz 1: Asymptotics In recitation, 27 Jan 2010 HW1 Program: Abstract Syntax Trees Due Thursday, 28 Jan 2010 at 11:59 pm 1.
3 Last time…asymptotics and recurrences T(n) = O (f(n)) ~ means T(n) f(n) T(n) = Ω (f(n)) ~ means T(n) f(n) T(n) = Θ (f(n)) ~ means T(n) = f(n) (asymptotically and within a constant)

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
4 Last time…asymptotics and recurrences Recurrence: a function defined in terms of itself e.g., T(n) = T(n/2) + 1
5 Last time…asymptotics and recurrences MergeSort (A) { if (A.length is 1) return; A 1 ˡ left half of A; A 2 ˡ right half of A; MergeSort(A 1 ); MergeSort(A 2 ); Merge(A 1 , A 2 ); } T(n) = 2T(n/2) + O (n) otherwise T(n) = 1 if n = 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
6 Last time … solving recurrences Substitution method a.k.a “Guess and check” Guess the correct answer Verify solution via induction E.g., T(n) = 2T(n/2) + n 1, if n = 1
7 Last time … solving recurrences Iteration method Iterate the recurrence into a summation Might help form a guess for substitution method, or maybe can just compute the sum exactly e.g., T(n) = T(n-1) + n T(1) = 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
8 Last time … solving recurrences Recursion trees A trick to visualize how a recurrence is iterated Essentially, construct the call tree the computer would generate Compute work at each level in tree Sum the levels
9 Today’s outline Solving recurrences Master method Ye old faithful data structure: Lists and lists representations Mutability and destructive operations References: nulls and object oriented lists

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
10 Master method A “cookbook” method for solving recurrences of the following form: a 1 , b > 1 , and f(n) is an asymptotically positive function Key Idea : compare f(n) to n log b a T ( n ) = aT n b + f ( n )
11 Master method 1. If f(n) = O (n log b a – ε ) for some constant ε > 0: T(n) = Θ (n log b a ) 2. If f(n) = Θ (n log b a ) : T(n) = Θ (n log b a lg n) 3. If f(n) = Ω ( n log b a + ε ) for some constant ε > 0 and if af(n/b) cf(n) for some c < 1 and all sufficiently large n : T(n) = Θ (f(n))

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
12 Master method Example 1: T(n) = 9T(n/3) + n
13 Master method Example 2: T(n) = T(2n/3) + 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
14 Master method Example 3: T(n) = 3T(n/4) + n log n
15 Master method Example 4: T(n) = 2T(n/2) + n log n

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Master method: Why does it work?
This is the end of the preview. Sign up to access the rest of the document.
• Spring '09