Motivation Introduction to Algorithms Introduction to Algorithms � For insertion sort (and other problems) as n � For insertion sort (and other problems) as n doubles in size, the quadratic quadruples! Recursion & Merge Sort Recursion & Merge Sort � Can we decrease n ? � Can we decrease n ? � What if we Divide the sort into smaller CSE 680 pieces? pieces? Prof. Roger Crawfis � We can then solve those ( Conquer them). � We need to be able to combine the pieces W d t b bl t bi th i in a manner simpler than quadratic. Divide and Conquer q Merge Sort g MergeSort(A, left, right) { � Divide (into two equal parts) � Divide (into two equal parts) if (left < right) { � Conquer (solve for each part separately) mid = floor((left + right) / 2); MergeSort(A, left, mid); MergeSort(A, left, mid); � Combine separate solutions C bi t l ti MergeSort(A, mid+1, right); � Merge sort Merge(A, left, mid, right); } � Divide into two equal parts } � Sort each part using merge-sort p g g (recursion!!!) // Merge() takes two sorted subarrays of A and // merges them into a single sorted subarray of A � Merge two sorted subsequences // // (how long should this take?) (how long should this take?)
Merge Sort: Example g p Analysis of Merge Sort y g � Show MergeSort() running on the array � Show MergeSort() running on the array Statement Statement Effort Effort T(n) MergeSort(A, left, right) { Θ (1) if (left < right) { A = {10, 5, 7, 6, 1, 4, 8, 3, 2, 9}; { , , , , , , , , , } Θ (1) Θ mid = floor((left + right) / 2); / MergeSort(A, left, mid); T(n/2) MergeSort(A, mid+1, right); T(n/2) Θ (n) Merge(A, left, mid, right); g ( , , , g ); ( ) } } � So T(n) = Θ (1) when n = 1 and � So T(n) Θ (1) when n 1, and 2T(n/2) + Θ (n) when n > 1 � So what (more succinctly) is T(n)? � So what (more succinctly) is T(n)? Recurrences Recursion Tree � The expression: � The expression: = 1 ⎧ c n 1 2 3 4 5 6 7 8 ⎪ ⎪ ⎪ ⎪ = ⎨ T ( n ) ⎛ ⎞ 1 3 5 8 2 4 6 7 log n n ⎪ + > ⎜ ⎟ 2 T cn n 1 ⎪ ⎪ ⎝ ⎝ 2 ⎠ ⎠ ⎩ ⎩ 2 1 5 3 8 4 7 2 6 � is a recurrence . 5 1 8 3 7 4 6 2 Recurrence: an equation that describes a function in • n comparisons per level terms of its value on smaller functions • log n levels • total runtime = n log n t t l ti l
Recurrence Examples p Recurrence Examples p = 0 = 0 ⎧ ⎧ ⎧ ⎧ 0 0 n n 0 0 0 n n 0 = = ⎨ ⎨ T ( n ) T ( n ) + − > + − > ⎩ ⎩ c T ( n 1 ) n 0 n T ( n 1 ) n 0 Recurrence Examples p Recurrence Examples p = 1 ⎧ ⎧ ⎧ ⎧ c c n n 1 ⎪ ⎪ = ⎪ c n 1 = ⎪ ⎨ T n ( ) = = ⎛ ⎛ ⎞ ⎞ ⎨ ⎨ T T ( ( n n ) ) n n ⎪ ⎪ + > ⎜ ⎟ 2 T c n 1 ⎪ ⎛ ⎞ ⎪ n ⎝ ⎠ ⎩ 2 + > ⎜ ⎟ ⎪ aT cn n 1 ⎝ ⎝ b ⎠ ⎠ ⎩ ⎩ b
Solving Recurrences g � Chapter 4 will look at several methods to � Chapter 4 will look at several methods to solve these recursions: � Substitution method � Substitution method � Recursion-tree method � Master method � Master method
Recommend
More recommend