340 likes | 505 Views
Introduction to Algorithms. Lecture 2. Agenda. Review Asymptotic Notation Q Notation O (Big Oh) Notation W Notation w Notation o Notation Recurrences Substitution Method Recursion Tree Master Method . In the Last lecture we found that. Define the Analysis of algorithms
E N D
Introduction to Algorithms • Lecture 2
Agenda • Review • Asymptotic Notation • QNotation • O (Big Oh) Notation • WNotation • w Notation • o Notation • Recurrences • Substitution Method • Recursion Tree • Master Method
In the Last lecture we found that • Define the Analysis of algorithms • Discuss What is more important than performance? • Insertion sort algorithms was presented and best and worst case times were calculated. • We discussed Merge sort algorithm and worst case time was calculated. • We concluded that • Θ(nlgn)grows more slowly than Θ(n2). • Merge sort asymptotically beats insertion sort in the worst case. • In practice, merge sort beats insertion sort for n> 30 or so.
Why Asymptotic notation? • A way to describe behavior of functions in the limit. • Describe growth of functions. • Focus on what’s important by abstracting away low-order terms and constant factors. • How we indicate running times of algorithms. • A way to compare “sizes” of functions:
Set definition of O-notation • EXAMPLE: • 2n2∈O(n3) ( c = 1 and n0 = 2)
W-notation (lower bounds) • O-notation is an upper-bound notation. It makes no sense to say f(n) is at least O(n2). • Ω-notation (lower bounds) • EXAMPLE: (c= 1, n0= 16)
W-notation Remember
Θ-notation (tight bounds) Example: c1 = 1/4, c2 = 1/2, and n0 = 8.
Theorem f(n) = Q(g(n)) if and only if f(n) = O(g(n)) and f(n) = W(g(n))
Solving recurrences • The analysis of merge sort from Lecture 1required us to solve a recurrence. • Recurrences are like solving integrals, differential equations, etc. • In the Next Lecture ISA: • Some more detail for solving recurrences • Examples for recurrences • Applications of recurrences to divide-and-conquer algorithms.
Substitution method • Substitution method steps: • Guess the form of the solution. • Verify by induction. • Solve for constants. The problem here how to guess the form...
Recursion-tree method • A recursion tree models the costs (time) of a recursive execution of an algorithm. • The recursion-tree method can be unreliable, just like any method that uses ellipses (…). • The recursion tree method is good for generating guesses for the substitution method.
Example of recursion tree Solve T(n) = T(n/4) + T(n/2)+ n2
Example of recursion tree Solve T(n) = T(n/4) + T(n/2)+ n2
Example of recursion tree Solve T(n) = T(n/4) + T(n/2)+ n2
Example of recursion tree Solve T(n) = T(n/4) + T(n/2)+ n2
Example of recursion tree Solve T(n) = T(n/4) + T(n/2)+ n2
Example of recursion tree Solve T(n) = T(n/4) + T(n/2)+ n2
The master method • The master method applies to recurrences of the form T(n) = aT(n/b) + f(n) , where a≥1, b> 1, and f is asymptotically positive.