Masters Method Cookbook for Solving Recurrences - Lecture Insights

Download Presenatation
Masters Method Cookbook for Solving Recurrences - Lecture Insights
Slide Note
Embed
Share

Delve into the Masters Method Cookbook for efficiently solving recurrences of the form n = (a)T(n/b) + f(n) with various cases and implications. Explore the significance of nlogba and how it influences the time complexity analysis, backed by insightful examples and a breakdown of regularity conditions.

  • Recurrences
  • Masters Method
  • Time Complexity
  • Analysis
  • Algorithms

Uploaded on Mar 02, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 6

  2. Administrative Note taking Assistance needed with note taking for a student in our class Let me know either now or at the end of the class Updates Mid-term new date: March 10 TA office hours updated on website and syllabus: Office: Lab C in ECC, SEM Office hours:Friday: 10am-noon Course website (on CSE server) updated CS 477/677 - Lecture 6 2

  3. Masters method Cookbook for solving recurrences of the form: = n + ( ) ( ) T n aT f n b where, a 1, b > 1, and f(n) > 0 Idea: compare f(n) with nlogba f(n) is asymptotically smaller or larger than nlogba by a polynomial factor n f(n) is asymptotically equal with nlogba CS 477/677 - Lecture 6 3

  4. Masters method Cookbook for solving recurrences of the form: n = + ( ) ( ) T n aT f n b where, a 1, b > 1, and f(n) > 0 Case 1: if f(n) = O(nlogba- ) for some > 0, then: T(n) = ?(nlogba) Case 2: if f(n) = ?(nlogba), then: T(n) = ?(nlogba lgn) Case 3: if f(n) = ?(nlogba+ ) for some > 0, and if af(n/b) cf(n) for some c < 1 and all sufficiently large n, then: T(n) = ?(f(n)) regularity condition CS 477/677 - Lecture 6 4

  5. Why nlogba? n = + ( ) ( ) T n aT f n b Case 1: If f(n) is dominated by nlogba: T(n) = ?(nlogbn) n = ( ) T n aT b n 2 a T 2 b n 3 a T Case 3: If f(n)dominates nlogba: T(n) = ?(f(n)) 3 b n = i ( ) T n a T i i b Case 2: If f(n) = ?(nlogba): T(n) = ?(nlogba logn) Assumen = bk k = logbn At the end of iterations, i = k: ( ) ( ) i b = = = = log log log log n n n a ( ) ) 1 ( T n a T a T a n b b b b i b CS 477/677 - Lecture 6 5

  6. Examples T(n) = 2T(n/2) + n a = 2, b = 2, log22 = 1 Compare nlog22with f(n) = n f(n) = ?(n) Case 2 T(n) = ?(nlgn) CS 477/677 - Lecture 6 6

  7. Examples T(n) = 2T(n/2) + n2 a = 2, b = 2, log22 = 1 Compare n with f(n) = n2 f(n) = ?(n1+ ) Case 3 verify regularity cond.: a f(n/b) c f(n) 2 n2/4 c n2 c = is a solution (c<1) T(n) = ?(n2) CS 477/677 - Lecture 6 7

  8. Examples (cont.) T(n) = 2T(n/2) + n a = 2, b = 2, log22 = 1 Compare n with f(n) = n1/2 f(n) = O(n1- ) Case 1 T(n) = ?(n) CS 477/677 - Lecture 6 8

  9. Examples T(n) = 3T(n/4) + nlgn a = 3, b = 4, log43 = 0.793 Compare n0.793 with f(n) = nlgn f(n) = ?(nlog43+ ) Case 3: check regularity condition: 3(n/4)lg(n/4) (3/4)nlgn = c f(n), c=3/4 T(n) = ?(nlgn) CS 477/677 - Lecture 6 9

  10. Examples T(n) = 2T(n/2) + nlgn a = 2, b = 2, log22 = 1 Compare n with f(n) = nlgn seems like case 3 should apply f(n) must be polynomially larger by a factor of n In this case it is only larger by a factor of lgn CS 477/677 - Lecture 6 10

  11. The Sorting Problem Input: A sequence of nnumbers a1, a2, . . . , an Output: A permutation (reordering) a1 , a2 , . . . , an of the input sequence such that a1 a2 an CS 477/677 - Lecture 6 11

  12. Why Study Sorting Algorithms? There are a variety of situations that we can encounter Do we have randomly ordered keys? Are all keys distinct? How large is the set of keys to be ordered? Need guaranteed performance? Does the algorithm sort in place? Is the algorithm stable? Various algorithms are better suited to some of these situations CS 477/677 - Lecture 6 12

  13. Stability A STABLE sort preserves relative order of records with equal keys Sort file on first key: Sort file on second key: Records with key value 3 are not in order on first key!! CS 477/677 - Lecture 6 13

  14. Insertion Sort Idea: like sorting a hand of playing cards Start with an empty left hand and the cards facing down on the table Remove one card at a time from the table, and insert it into the correct position in the left hand compare it with each of the cards already in the hand, from right to left The cards held in the left hand are sorted these cards were originally the top cards of the pile on the table CS 477/677 - Lecture 6 14

  15. Example CS 477/677 - Lecture 6 15

  16. INSERTION-SORT Alg.: INSERTION-SORT(A) for j 2to n do key A[ j ] Insert A[ j ] into the sorted sequence A[1 . . j -1] i j - 1 while i > 0 and A[i] > key do A[i + 1] A[i] i i 1 A[i + 1] key Insertion sort sorts the elements in place 1 2 3 4 5 6 7 8 a1 a2 a3 a4 a5 a6 a7 a8 key CS 477/677 - Lecture 6 16

  17. Loop Invariant for Insertion Sort Alg.: INSERTION-SORT(A) for j 2to n do key A[ j ] Insert A[ j ] into the sorted sequence A[1 . . j -1] i j - 1 while i > 0 and A[i] > key do A[i + 1] A[i] i i 1 A[i + 1] key Invariant: at the start of each iteration of the for loop, the elements in A[1 . . j-1] are in sorted order CS 477/677 - Lecture 6 17

  18. Proving Loop Invariants Proving loop invariants works like induction Initialization (base case): It is true prior to the first iteration of the loop Maintenance (inductive step): If it is true before an iteration of the loop, it remains true before the next iteration Termination: When the loop terminates, the invariant gives us a useful property that helps show that the algorithm is correct CS 477/677 - Lecture 6 18

  19. Loop Invariant for Insertion Sort Initialization: Just before the first iteration, j = 2: the subarray A[1 . . j-1] = A[1], (the element originally in A[1]) is sorted CS 477/677 - Lecture 6 19

  20. Loop Invariant for Insertion Sort Maintenance: the while inner loop moves A[j -1], A[j -2], A[j -3], and so on, by one position to the right until the proper position for key(which has the value that started out in A[j]) is found At that point, the value of keyis placed into this position. CS 477/677 - Lecture 6 20

  21. Loop Invariant for Insertion Sort Termination: The outer for loop ends when j = n + 1 j-1 = n Replace nwith j-1 in the loop invariant: the subarray A[1 . . n] consists of the elements originally in A[1 . . n], but in sorted order j - 1 j The entire array is sorted! CS 477/677 - Lecture 6 21

  22. Analysis of Insertion Sort INSERTION-SORT(A) for j 2 to n do key A[ j ] Insert A[ j ] into the sorted seq. A[1 . . j -1] i j - 1 while i > 0 and A[i] > key do A[i + 1] A[i] i i 1 A[i + 1] key cost times c1 n c2 0 n-1 c4 c5 c6 c7 c8 n-1 n-1 = = = n jt ( 2 j n ) 1 jt 2 j n ( ) 1 jt 2 j n-1 ( ) ( ) n n n = j = j = j = + ) 1 + ) 1 + + + + ) 1 ( ) ( ( 1 1 ( T n c n c n c n c t c t c t c n 1 2 4 5 6 7 8 j j j 2 2 2 CS 477/677 - Lecture 6 22

  23. Best Case Analysis The array is already sorted while i > 0 and A[i] > key A[i] key upon the first time the while loop test is run (when i = j -1) tj= 1 T(n) = c1n + c2(n -1) + c4(n -1) + c5(n -1) + c8(n-1) = (c1 + c2 + c4 + c5 + c8)n - (c2 + c4 + c5 + c8) = an + b = (n) ( ) ( ) n n n = j = j = j = + ) 1 + ) 1 + + + + ) 1 ( ) ( ( 1 1 ( T n c n c n c n c t c t c t c n 1 2 4 5 6 7 8 j j j 2 2 2 CS 477/677 - Lecture 6 23

  24. Worst Case Analysis while i > 0 and A[i] > key The array is reversely sorted Always A[i] > key in while loop test Have to compare keywith all elements to the left of the j-th position compare with j-1 elements tj = j ) 1 + ) 1 n n ( ( n n n n = j = j = ) 1 = 1 ( j and j 2 2 2 2 ) 1 + ) 1 ) 1 ( ( ( n n n n n n = + ) 1 + ) 1 + + + + ) 1 ( ) ( ( 1 ( T n c n c n c n c c c c n 1 2 4 5 6 7 8 2 2 2 = + + 2 an bn c a quadratic function of n T(n) = (n2) order of growth in n2 CS 477/677 - Lecture 6 24

  25. Comparisons and Exchanges in Insertion Sort INSERTION-SORT(A) cost times c1 n c2 0 c4 c5 c6 c7 c8 for j 2 to n n-1 n-1 n-1 = = = do key A[ j ] Insert A[ j ] into the sorted sequence A[1 . . j -1] n2/2 comparisons i j - 1 n jt while i > 0 and A[i] > key 2 j n ( ) 1 jt do A[i + 1] A[i] 2 j n n2/2 exchanges i i 1 ( ) 1 jt 2 j n-1 A[i + 1] key CS 477/677 - Lecture 6 25

  26. Insertion Sort - Summary Idea: like sorting a hand of playing cards Start with an empty left hand and the cards facing down on the table. Remove one card at a time from the table, and insert it into the correct position in the left hand Advantages Good running time for almost sorted arrays (n) Disadvantages (n2) running time in worst and average case n2/2 comparisons and n2/2 exchanges CS 477/677 - Lecture 6 26

  27. Bubble Sort Idea: Repeatedly pass through the array Swaps adjacent elements that are out of order i 1 2 3 n 8 4 6 9 2 3 1 j Easier to implement, but slower than Insertion sort CS 477/677 - Lecture 6 27

  28. Example 8 4 6 9 2 3 1 1 8 4 6 9 2 3 i = 1 j i = 2 j 8 4 6 9 2 1 j 3 1 2 8 4 6 9 3 i = 1 i = 3 j 8 4 6 9 1 j 2 3 1 2 3 8 4 6 9 i = 1 i = 4 j 8 4 6 1 j 9 2 3 1 2 3 4 8 6 9 i = 1 i = 5 j 8 4 1 6 9 2 3 1 2 3 4 6 8 9 i = 1 j i = 6 j 8 1 j 4 6 9 2 3 1 2 3 4 6 8 9 i = 1 i = 7 j 1 8 j 4 6 9 2 3 CS 477/677 - Lecture 6 28 i = 1

  29. Bubble Sort Alg.: BUBBLESORT(A) fori 1tolength[A] do forj length[A]downtoi + 1 do ifA[j] < A[j -1] then exchange A[j] A[j-1] i 8 4 6 9 2 3 1 i = 1 j CS 477/677 - Lecture 6 29

  30. Readings Chapter 4 CS 477/677 - Lecture 6 30

Related


More Related Content