04 Recurrences and Lists

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

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

View Full Document Right Arrow Icon
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)
Background image of page 1

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

View Full DocumentRight Arrow Icon
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.
Background image of page 2
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)
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 Last time…asymptotics and recurrences Recurrence: a function defined in terms of itself e.g., T(n) = T(n/2) + 1
Background image of page 4
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
Background image of page 5

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

View Full DocumentRight Arrow Icon
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
Background image of page 6
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
Background image of page 7

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

View Full DocumentRight Arrow Icon
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
Background image of page 8
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
Background image of page 9

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

View Full DocumentRight Arrow Icon
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 )
Background image of page 10
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))
Background image of page 11

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

View Full DocumentRight Arrow Icon
12 Master method Example 1: T(n) = 9T(n/3) + n
Background image of page 12
13 Master method Example 2: T(n) = T(2n/3) + 1
Background image of page 13

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

View Full DocumentRight Arrow Icon
14 Master method Example 3: T(n) = 3T(n/4) + n log n
Background image of page 14
15 Master method Example 4: T(n) = 2T(n/2) + n log n
Background image of page 15

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

View Full DocumentRight Arrow Icon
Master method: Why does it work?
Background image of page 16
Image of page 17
This is the end of the preview. Sign up to access the rest of the document.