ITEC 2620M Introduction to Data Structures Instructor: Prof. Z. Yang Course Website: http://people.math.yorku.ca/~zyang/itec2620m.htm Office: DB 3049
Complexity Analysis – Recurrence Relations
Key Points of this Lecture Analysis of recursive algorithms Recurrence relations Quicksort, Mergesort Best, Worst, and Average cases
Complexity Analysis Determine how the processing time of an algorithm grows with input size. What if the algorithm is recursive? use recurrence relations Recurrence relations The problem is solved through smaller sub-problems. The time to solve a problem is based on the time to solve smaller sub-problems. Express the time to solve a problem of size n as T(n).
From Recursion to Recurrence Relations Recursion has two cases recursive case base case Recurrence relation needs two cases recurring case for complexity analysis, T(1) is usually 1 eliminate constant factors
Quicksort What is the complexity of Quicksort? Is there a Best, Worst, and Average case? Can Quicksort run faster or slower based on the input? yes! What is the best case? pivot always splits the sub-groups exactly in half What is the worst case? pivot never makes sub-groups all elements are always to one side or the other What is the average case? pivot splits the sub-groups loosely in half
Quicksort - Recurrence Relation Best Case The time to do a problem of size n is the time to do two sub-problems of size n/2 + n compares (during partition). The time to do a problem of size 1 is 1 (a constant). Worst Case The time to do a problem of size n is the time to do a sub-problem of size (n-1 )+ n compares. Average Case O(nlogn)
Mergesort What is the complexity of Mergesort? Is there a Best, Worst, and Average case? Can Mergesort run faster or slower based on the input? No Does Mergesort look at the values before splitting? No. Does merge always process all values? Yes. Complexity of Mergesort algorithm does not depend on input. Recurrence relations The time to do a problem of size n is the time to do two sub-problems of size n/2 + n compares (during merge). The time to do a problem of size 1 is 1 (a constant).
Summary Mergesort and Quicksort both have O(nlogn) in the best and average cases. Mergesort is also O(nlogn) in the worst case. Quicksort is O(n**2) in the worst case. Worst case is unlikely, and constants are smaller for Quicksort. Quicksort is the preferred algorithm for many applications But not all!