Analyzing Divide and Conquer Algorithms: Change of Variable Technique
280 likes | 414 Views
This lecture focuses on solving recurrence relations typical of divide and conquer algorithms, particularly through the “change of variable” technique. It includes a review of merge sort and its analysis using recurrence relations, with time permitting, a proof of the Master Theorem for solving non-homogeneous cases. This session aims to equip students with the understanding needed to analyze complex algorithmic behaviors and efficiently categorize algorithms based on their recurrence relations.
Analyzing Divide and Conquer Algorithms: Change of Variable Technique
E N D
Presentation Transcript
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License. CS 312: Algorithm Analysis Lecture #9: Solving Divide and Conquer Recurrence Relations with Change of Variable Slides by: Eric Ringger, with contributions from Mike Jones, Eric Mercer, Sean Warnick
Announcements • HW #6 Due Today • Questions about Non-homogeneous RR? • Project #2 • Questions? • Early Day: Wednesday • Due: Friday
Objectives • Understand how to analyze recurrence relations that are typical of divide and conquer algorithms • Learn to use the “change of variable” technique to solve such recurrences • Review Merge sort (quickly!) • Theoretical analysis using RRs • Time permitting, prove the Master theorem
Analysis Approach: Reduction Divide and Conquer Recurrence Relation Change of Variable Non-Homogeneous Recurrence Relation with Geometric Forcing Function LTI Homogeneous Recurrence Relation
Analysis Approach: Reduction Divide and Conquer Recurrence Relation Change of Variable Non-Homogeneous Recurrence Relation with Geometric Forcing Function “Unwindthe Stack” LTI Homogeneous Recurrence Relation
3 1 4 1 5 9 2 6 5 3 5 8 9 3 1 4 1 5 9 2 6 5 3 5 8 9 2 3 5 5 6 8 9 1 1 3 4 5 9 1 1 2 3 3 4 5 5 5 6 8 9 9 Review: Merge sort • Split the array in 1/2 • Sort each 1/2 • Merge the sorted halves • Adhoc(): Use insertion sort when sub-arrays are small
3 1 4 1 5 9 2 6 5 3 5 8 9 3 1 4 1 5 9 2 6 5 3 5 8 9 2 3 5 5 6 8 9 1 1 3 4 5 9 1 1 2 3 3 4 5 5 5 6 8 9 9 Analysis of merge() proceduremerge (U[1..p+1], V[1..q+1], A[1..n]) i,j 1 U[p+1],V[q+1] sentinel for k 1 top+qdo if U[i] < V[ j] then A[k] U[i]; i i + 1 else A[k] V[j]; j j + 1 How long to merge? U V A
Merge sort proceduremergesort (A[1..n]) if n is small enough theninsertsort (A) else array U[1..1+floor(n/2)], V[1..1+ceil(n/2)] U[1..floor(n/2)] A[1..floor(n/2)] V[1..ceil(n/2)] A[1+floor(n/2)..n] mergesort (U) mergesort (V) merge (U,V,A) What is the efficiency of Mergesort?
Using the Master Theorem a = number of sub-instances that must be solved n = original instance size (variable) n/b = size of subinstances d = polynomial order of g(n), where g(n) = cost of dividing and recombining
Another Useful Logarithm Identity Proof: Statement Reason • and are inverses • commutativity of • and are inverses
(Sketch of the)Proof of the Master Theorem How to proceed?
Assignment • Read: Section 2.3 in the textbook • HW #7: • Part III Exercises (Section 3.2) • Analyze 3-part mergesort using recurrence relations techniques • Problem 2.4 in the textbook (using the master theorem where possible)