zjgggpgggg
play

zjgggpgggg Solving Recurrences g.gg Jan 13, 2020 The Master - PowerPoint PPT Presentation

CS3000: Algorithms & Data Jonathan Ullman Lecture 3: Divide and Conquer: Karatsuba ekeMMtBsneleagioenocBmarigologgthAEthfthggpatz zjgggpgggg Solving Recurrences g.gg Jan 13, 2020 The Master Theorem Recipe for


  1. CS3000: Algorithms & Data Jonathan Ullman Lecture 3: Divide and Conquer: Karatsuba • ekeMMtBsneleagioenocBmarigologgthAEthfthggpatz zjgggpgggg Solving Recurrences • g.gg Jan 13, 2020

  2. The “Master Theorem” • Recipe for recurrences of the form: + )" * ⁄ • ! " = $ ⋅ ! " & • Three cases: D $ 3T E & * > 1 : ! " = Θ " ./0 & $ karat suba 1 n • Megesort TH 2T E in & * = 1 : ! " = Θ " * log " $ • Binary search 1 n TC E l $ & * < 1 : ! " = Θ " * 2T E • 1 Hn t

  3. Cnd T E 1 Ask the Audience! n _a • Use the Master Theorem to Solve: 16 a Fa I n n'tog b 4 6 7 + )" 8 • ! " = 16 ⋅ ! D 2 21 6 a : + )" 8 • ! " = 21 ⋅ ! m2 Fd I 5 b d 2 6 n'Gbla • ! " = 2 ⋅ ! 8 + ) a 2 I age 2 b d O n 6 • ! " = 1 ⋅ ! 8 + )

  4. Divide and Conquer: Selection (Median)

  5. Selection • Given an array of numbers ;[1: "] , how quickly can I find the: OCD time • Smallest number? • Second smallest? time 0 n • ? -th smallest? nk time 01h2 t.me median FET nd smallest 11 3 42 28 17 8 2 15 @

  6. Selection • Fact: can select the ? -th smallest in A("?) time • Fact: can select the ? -th smallest in A " log " time • Sort the list, then return ;[?] 11 3 42 28 17 8 2 15 @ sort 2 3 8 11 15 17 28 42 • Today: select the ? -th smallest in A " time

  7. Warmup • You have 25 horses and want to find the 3 fastest • You have a racetrack where you can race 5 at a time • In: 1, 5, 6, 18, 22 Out: 6 ≻ 5 ≻ 18 ≻ 22 ≻ 1 • Problem: find the 3 fastest with only seven races c YE t winner e Tf a pg ry e 4 fate QD i tE B QD DK Di Di rid DW h v v slowest Fix Ky

  8. Median Algorithm: Take I 17 3 42 11 28 8 2 15 13 @ 11 3 5 13 2 8 17 28 42 Select(A[1:n],k): If(n = 1): return A[1] Choose a pivot p = A[1] Ocn time Partition around the pivot, let p = A[r] If(k = r): return A[r] ElseIf(k < r): return Select(A[1:r-1],k) ElseIf(k > r): return Select(A[r+1:n],k-r)

  9. Median Algorithm: Take I 1 2 3 4 5 6 7 8 9 @ 1 9 k 8 91 5 12 4 8 3 7 k 617184 7 1 2 ne 1 n 2 t t ht l n I 9 k I

  10. Median Algorithm: Take II • Problem: we need to find a good pivot element is the best pivot but also it's The median we're trying the problem to solve in the middle find element enough to It's an half of the list

  11. TLE OG T.me Median of Medians MOM(A[1:n]): ⁄ Let H ← J K For i = 1,…,m: 047 I 30cm Meds[i] = median{A[5i-4],A[5i-3],…,A[5i]} Let p ← Select(Meds[1:m], H L ⁄ ) e Ei Median of the medians B D B D B D B D B D OD i OD Do Do D B B B D D b D D D

  12. Median of Medians ⁄ • Claim: For every @ here are at least 3" 10 items that are smaller than OPO(@) and at least 3"/10 items that are larger. biggest med smallestmedian ran Fo E I smallest 44 Mr HI 1111 H 4h am he Makin IN z 411144141444444 KK 111 11444 41 1 4 1944 biggest are smaller than the MOM AH red dts 70 of red dts 3 Fo is

  13. Median Algorithm: Take II 17 3 42 11 28 8 2 15 13 @ 11 3 5 13 2 8 17 28 42 MOMSelect(A[1:n],k): If(n ≤ 25): return median{A} 1 F Let p = MOM(A) t n Partition around the pivot, let p = A[r] n If(k = r): return A[r] ElseIf(k < r): return MOMSelect(A[1:r-1],k) 11 ElseIf(k > r): return MOMSelect(A[r+1:n],k-r)

  14. Running Time Analysis

  15. R6 6 Recursion Tree ! " = ! ST + ! : + )" ! 1 = ) Werk Led Problems t I n o n Fo 15 11TH I 9,3 7h40 I 9 I FEET 2 1,49110 n

  16. R6 6 Proof by Induction ! " = ! ST + ! : + )" ! 1 = ) BI • Claim: !(") = A(") choose later C I'll for Nn E Cn some as long as Csi Cn I E I 1 Ba e T E Fo t 1 1 n Inductiest n Co E C 75 e n E C 1 E 9 Etc Fo t C Is 1 t on al c 10 C Fo 1 on as long as C lo C E n

  17. Ask the Audience • If we change MOM so that it uses n/3 blocks of size 3, would Select still run in O(n) time?

  18. Selection Wrapup • Find the ? -th largest element in A " time • Selection is strictly easier than sorting! • Divide-and-conquer approach • Find a pivot element that splits the list roughly in half • Key Fact: median-of-medians-of-five is a good pivot • Can sort in A " log " time using same technique • Algorithm is called Quicksort • Analyze running time via recurrence • Master Theorem does not apply • Fun Fact: a random pivot is also a good pivot!

  19. Divide and Conquer: Binary Search

  20. Binary Search Is 28 in this list? 2 3 8 11 15 17 28 42 ;

  21. Binary Search StartSearch(A,t): // A[1:n] sorted in ascending order Return Search(A,1,n,t) Search(A, ℓ ,r,t): If( ℓ > r): return FALSE WXℓ m ← ℓ + L If(A[m] = t): return m ElseIf(A[m] > t): return Search(A, ℓ ,m-1,t) Else: return Search(A,m+1,r,t)

  22. ! " = ! " 2 ⁄ + ) Running Time Analysis ! 1 = )

  23. Binary Search Wrapup • Search a sorted array in time A(log ") • Divide-and-conquer approach • Find the middle of the list, recursively search half the list • Key Fact: eliminate half the list each time • Prove correctness via induction • Analyze running time via recurrence ⁄ • ! " = ! " 2 + )

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend