Recurrences The expression: is a recurrence. –Recurrence: an equation that describes a function in terms of its value on smaller functions BIL741: Advanced.

Slides:



Advertisements
Similar presentations
A simple example finding the maximum of a set S of n numbers.
Advertisements

5/5/20151 Analysis of Algorithms Lecture 6&7: Master theorem and substitution method.
Comp 122, Spring 2004 Divide and Conquer (Merge Sort)
Introduction to Algorithms Jiafen Liu Sept
September 12, Algorithms and Data Structures Lecture III Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Divide-and-Conquer Recursive in structure –Divide the problem into several smaller sub-problems that are similar to the original but smaller in size –Conquer.
11 Computer Algorithms Lecture 6 Recurrence Ch. 4 (till Master Theorem) Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
ADA: 4. Divide/Conquer1 Objective o look at several divide and conquer examples (merge sort, binary search), and 3 approaches for calculating their.
Algorithms Recurrences. Definition – a recurrence is an equation or inequality that describes a function in terms of its value on smaller inputs Example.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
CS3381 Des & Anal of Alg ( SemA) City Univ of HK / Dept of CS / Helena Wong 4. Recurrences - 1 Recurrences.
Analysis of Recursive Algorithms
CS 253: Algorithms Chapter 4 Divide-and-Conquer Recurrences Master Theorem Credit: Dr. George Bebis.
Recurrences Part 3. Recursive Algorithms Recurrences are useful for analyzing recursive algorithms Recurrence – an equation or inequality that describes.
Recurrences The expression: is a recurrence. –Recurrence: an equation that describes a function in terms of its value on smaller functions Analysis of.
Analysis of Algorithms CS 477/677 Recurrences Instructor: George Bebis (Appendix A, Chapter 4)
October 1, Algorithms and Data Structures Lecture III Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Divide-and-Conquer 7 2  9 4   2   4   7
Analysis of Algorithms
Analysis of Algorithms CS 477/677
Analyzing Recursive Algorithms A recursive algorithm can often be described by a recurrence equation that describes the overall runtime on a problem of.
David Luebke 1 10/3/2015 CS 332: Algorithms Solving Recurrences Continued The Master Theorem Introduction to heapsort.
MCA 202: Discrete Mathematics Instructor Neelima Gupta
1 Computer Algorithms Lecture 7 Master Theorem Some of these slides are courtesy of D. Plaisted, UNC and M. Nicolescu, UNR.
Project 2 due … Project 2 due … Project 2 Project 2.
Merge Sort Solving Recurrences The Master Theorem
CSC 413/513: Intro to Algorithms Merge Sort Solving Recurrences.
CS 2133:Data Structures Merge Sort Solving Recurrences The Master Theorem.
DR. NAVEED AHMAD DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF PESHAWAR LECTURE-5 Advance Algorithm Analysis.
10/25/20151 CS 3343: Analysis of Algorithms Lecture 6&7: Master theorem and substitution method.
Algorithms Merge Sort Solving Recurrences The Master Theorem.
Tonga Institute of Higher Education Design and Analysis of Algorithms IT 254 Lecture 2: Mathematical Foundations.
1 Chapter 4 Divide-and-Conquer. 2 About this lecture Recall the divide-and-conquer paradigm, which we used for merge sort: – Divide the problem into a.
Introduction to Algorithms Chapter 4: Recurrences.
Recurrences David Kauchak cs161 Summer Administrative Algorithms graded on efficiency! Be specific about the run times (e.g. log bases) Reminder:
Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.
CSC 413/513: Intro to Algorithms Solving Recurrences Continued The Master Theorem Introduction to heapsort.
1 Algorithms CSCI 235, Fall 2015 Lecture 6 Recurrences.
Foundations II: Data Structures and Algorithms
Design & Analysis of Algorithms COMP 482 / ELEC 420 John Greiner
Divide and Conquer. Recall Divide the problem into a number of sub-problems that are smaller instances of the same problem. Conquer the sub-problems by.
1Computer Sciences. 2 GROWTH OF FUNCTIONS 3.2 STANDARD NOTATIONS AND COMMON FUNCTIONS.
Introduction to Algorithms (2 nd edition) by Cormen, Leiserson, Rivest & Stein Chapter 4: Recurrences.
Master Method Some of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Recurrences (in color) It continues…. Recurrences When an algorithm calls itself recursively, its running time is described by a recurrence. When an algorithm.
Divide and Conquer Faculty Name: Ruhi Fatima Topics Covered Divide and Conquer Matrix multiplication Recurrence.
Recurrences It continues… Jeff Chastine. Recurrences When an algorithm calls itself recursively, its running time is described by a recurrence. A recurrence.
Introduction to Algorithms: Divide-n-Conquer Algorithms
Mathematical Foundations (Solving Recurrence)
Introduction to Algorithms: Recurrences
CS 3343: Analysis of Algorithms
T(n) = aT(n/b) + cn = a(aT(n/b/b) + cn/b) + cn 2
Advance Analysis of Lecture No 9 Institute of Southern Punjab Multan
Introduction to Algorithms 6.046J
Algorithms and Data Structures Lecture III
Ch 4: Recurrences Ming-Te Chi
CS 3343: Analysis of Algorithms
Divide and Conquer (Merge Sort)
Merge Sort Solving Recurrences The Master Theorem
Solving Recurrences Continued The Master Theorem
Analysis of Algorithms
Algorithms Recurrences.
Quicksort Quick sort Correctness of partition - loop invariant
Algorithms and Data Structures Lecture III
Presentation transcript:

Recurrences The expression: is a recurrence. –Recurrence: an equation that describes a function in terms of its value on smaller functions BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)1

Recurrence Examples BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)2

Recurrence Examples BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)3

Solving Recurrences Substitution method “making a good guess method” Iteration method –(Repeated Substitution) Master method BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)4

Substitution Method “making a good guess method” Guess the form of the answer, then use induction to find the constants and show that solution works Examples: –T(n) = 2T(n/2) +  (n)  T(n) =  (n lg n) –T(n) = 2T(  n/2  ) + n  T(n) =  (n lg n) –T(n) = 2T(  n/2  + 17) + n   (n lg n) BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)5

Substitution Method BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)6

Substitution Method T(n) = 2 T(  n/2  ) + n Guess: O(n lgn) Verify –Inductive Hypothesis: T(n) ≤ c n lgn for appropriate choice of c > 0 –Prove that T(n) ≤ c n lgn for appropriate choice of c > 0 Use induction: Assume T(  n/2  ) ≤ c  n/2  lg(  n/2  ) holds Show T(n) ≤ c n lgn BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)7

Substitution Method T(n) = 2 T(  n/2  ) + n T(n)≤ 2 c  n/2  lg(  n/2  ) + n apply IH ≤ c n lg(n/2) + n = c n lgn – c n lg2 + n = c n lgn – c n + n ≤ c n lgn when c ≥ 1 BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)8

Substitution Method T(n) = T(  n/2  ) + T(  n/2  ) + 1 Guess: O(n) Try to show T(n) ≤ cn for appropriate constant c (IH) T(n) = T(  n/2  ) + T(  n/2  ) + 1 T(n) ≤ c  n/2  + c  n/2  + 1 = cn + 1 but does not imply T(n) ≤ c n So, our IH does not work –go to O(n 2 ) or –change IH  T(n) ≤ cn – b where b ≥ 0 BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)9

Substitution Method IH  T(n) ≤ cn – b where b ≥ 0 (subtracting lower order term) T(n) = T(  n/2  ) + T(  n/2  ) + 1 T(n) ≤ ( c  n/2  - b ) + ( c  n/2  - b )+ 1 = cn - 2b + 1 ≤ cn - b when b ≥ 1 BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)10

Substitution Method – Avoiding Pitfalls T(n) = 2 T(  n/2  ) + n Guess: O(n) ?? (wrong guess) T(n) ≤ cn ( IH ) T(n)≤ 2 c  n/2  + n apply IH ≤ c n + n = O(n)  WRONG Since c is constant, we have not prove that the exact form of IH, i.e. T(n) ≤ cn BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)11

Substitution Method – Changing Variables T(n) = 2 T(  n  ) + lg n  a difficult recurrence Rename m as lgn yields T(2 m ) = 2 T(2 m/2 ) + m Rename S(m) = T(2 m ) S(m) = 2 T(m/2) + m Similar to our previous recurrence  O(m lgm) Change back S(m) to T(n) T(n) = T(2 m ) = S(m) = O(m lgm)  O(lgn lg lg n) BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)12

Iteration Method Another option is what the book calls the “iteration method” –Expand the recurrence –Work some algebra to express as a summation –Evaluate the summation BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)13

Iteration Method BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)14 T(n) = c + T(n-1) c + c + T(n-2) 2c + T(n-2) 2c + c + T(n-3) 3c + s(n-3) … kc + T(n-k) = ck + T(n-k) So far for n >= k we have T(n) = ck + T(n-k) What if k = n? T(n) = cn + T(0) = cn  O(n)

Iteration Method BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)15 T(n) = n + T(n-1) n + (n-1) + T(n-2) n + (n-1) + (n-2) + T(n-3) … What if k = n?  O(n 2 )

Iteration Method BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)16 T(n) = 2T(n/2) + c 2(2T(n/2/2) + c) + c 2 2 T(n/2 2 ) + 2c + c 2 2 (2T(n/2 2 /2) + c) + 3c 2 3 T(n/2 3 ) + 4c + 3c 2 3 T(n/2 3 ) + 7c 2 3 (2T(n/2 3 /2) + c) + 7c 2 4 T(n/2 4 ) + 15c … 2 k T(n/2 k ) + (2 k - 1)c

Iteration Method So far for n > 2k we have –T(n) = 2 k T(n/2 k ) + (2 k - 1)c What if k = lg n? –T(n) = 2 lg n T(n/2 lg n ) + (2 lg n - 1)c = n T(n/n) + (n - 1)c = n T(1) + (n-1)c = nc + (n-1)c = (2n - 1)c  O(n) BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)17

Recursion-Tree Method A recursion tree models the costs (time) of a recursive execution of an algorithm. The recursion tree method is good for generating guesses for the substitution method. The recursion-tree method promotes intuition. BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)18

Recursion-Tree Method BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)19

Solving Recurrences BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)20 Try to solve following recurrence equation to understand Master Theorem

Solving Recurrence T(n) = aT(n/b) + cn a(aT(n/b/b) + cn/b) + cn a 2 T(n/b 2 ) + cna/b + cn a 2 T(n/b 2 ) + cn(a/b + 1) a 2 (aT(n/b 2 /b) + cn/b 2 ) + cn(a/b + 1) a 3 T(n/b 3 ) + cn(a 2 /b 2 ) + cn(a/b + 1) a 3 T(n/b 3 ) + cn(a 2 /b 2 + a/b + 1) … a k T(n/b k ) + cn(a k-1 /b k-1 + a k-2 /b k-2 + … + a 2 /b 2 + a/b + 1) BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)21

Solving Recurrence So we have –T(n) = a k T(n/b k ) + cn(a k-1 /b k a 2 /b 2 + a/b + 1) For k = log b n –n = b k –T(n)= a k T(1) + cn(a k-1 /b k a 2 /b 2 + a/b + 1) = a k c + cn(a k-1 /b k a 2 /b 2 + a/b + 1) = ca k + cn(a k-1 /b k a 2 /b 2 + a/b + 1) = cna k /b k + cn(a k-1 /b k a 2 /b 2 + a/b + 1) = cn(a k /b k a 2 /b 2 + a/b + 1) BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)22

Solving Recurrence So with k = log b n –T(n) = cn(a k /b k a 2 /b 2 + a/b + 1) What if a = b? –T(n)= cn(k + 1) = cn(log b n + 1) =  (n log n) BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)23

Solving Recurrence So with k = log b n –T(n) = cn(a k /b k a 2 /b 2 + a/b + 1) What if a < b? –Recall that  (x k + x k-1 + … + x + 1) = (x k+1 -1)/(x-1) –So: –T(n) = cn ·  (1) =  (n) BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)24

Solving Recurrence So with k = log b n –T(n) = cn(a k /b k a 2 /b 2 + a/b + 1) What if a > b? –T(n) = cn ·  (a k / b k ) = cn ·  (a log n / b log n ) = cn ·  (a log n / n) recall logarithm fact: a log n = n log a = cn ·  (n log a / n) =  (cn · n log a / n) =  (n log a ) BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)25

Solving Recurrence So… BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)26

The Master Theorem Given: a divide and conquer algorithm –An algorithm that divides the problem of size n into a subproblems, each of size n/b –Let the cost of each stage (i.e., the work to divide the problem + combine solved subproblems) be described by the function f(n) Then, the Master Theorem gives us a cookbook for the algorithm’s running time: BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)27

The Master Theorem if T(n) = aT(n/b) + f(n) then BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)28 CASE 1 : asymptotically smaller CASE 2 : asymptotically equal CASE 3 : asymptotically greater

The Master Theorem – Does Not Apply 1.f(n) is smaller than function, but NOT asymptotically smaller 2.f(n) is greater than function, but NOT asymptotically greater 3.CASE 3 condition does not hold BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)29

Master Method – Three Cases BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)30

Master Method – Three Cases BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)31

Using The Master Method T(n) = 9T(n/3) + n –a=9, b=3, f(n) = n –n log b a = n log 3 9 =  (n 2 ) –Since f(n) = O(n log  ), where  =1, CASE 1 applies: –Thus the solution is T(n) =  (n 2 ) BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)32

Using The Master Method T(n) = T(2n/3) + 1 –a=1, b=3/2, f(n) = 1 –n log b a = n log 3/2 1 = n 0 = 1 –Since f(n) =  (n log b a ) =  (1), CASE 2 applies: –Thus the solution is T(n) =  (lgn) BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)33

Using The Master Method T(n) = 3T(n/4) + nlgn –a=3, b=4, f(n) = nlgn –n log b a = n log 4 3 = n –Since f(n) =  (n log 4 3+  ) where  is approximately 0.2 CASE 3 applies: For sufficiently large n af(n/b)=3f(n/4)lg(n/4) ≤ (3/4)nlgn = cf(n) for c=3/4 By case 3 –Thus the solution is T(n) =  (nlgn) BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)34

Using The Master Method Master theorem does NOT apply to the recurrence T(n) = 2T(n/2) + nlgn –a=2, b=2, f(n) = nlgn –n log b a = n –Since f(n) = nlgn is asymptotically larger than n log b a = n –CASE 3 applies: –But f(n) = nlgn is NOT polynomially larger than n log b a = n –The ratio f(n) / n log b a = (nlgn) / n is asymptotically less than n  For any positive constant  –So the recurrence falls the gap between case 2 and case 3 BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)35

Using The Master Method BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)36

Using The Master Method BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)37

General Method (Akra-Bazzi) BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)38

Idea of Master Theorem BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)39

Idea of Master Theorem BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)40

Idea of Master Theorem BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)41

Idea of Master Theorem BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)42

Proof of Master Theorem: Case 1 and Case 2 BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)43

Proof of Case 1 BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)44

Case 1 (cont’) BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)45

Case 1 (cont’) BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)46

Proof of Case 2 (limited to k=0) BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)47

The Divide-and-Conquer Design Paradigm 1. Divide the problem (instance) into subproblems. 2. Conquer the subproblems by solving them recursively. 3. Combine subproblem solutions. T(n) is a recurrence equation BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)48

Example: Merge Sort 1. Divide: Trivial. 2. Conquer: Recursively sort 2 subarrays. 3. Combine: Linear- time merge. T(n) = 2 T(n/2) + O(n) # subproblems subproblem size work dividing and combining BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)49

Master Theorem – Merge Sort BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)50

Binary Search Find an element in a sorted array: 1. Divide: Check middle element. 2. Conquer: Recursively search 1 subarray. 3. Combine: Trivial. Example: Find BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)51

Master Theorem - Binary Search CASE 2 (k=0)  T(n) =  (lgn) BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)52

Powering a Number BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)53

Matrix Multiplication BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)54

Matrix Multiplication – Standard Algorithm BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)55

Matrix Multiplication – Divide-and-Conquer Algorithm BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)56

Matrix Multiplication - Analysis of D&C Algorithm BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)57

Matrix Multiplication - Strassen’s Idea BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)58

Matrix Multiplication - Strassen’s Idea BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)59

Matrix Multiplication - Strassen’s Idea BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)60

Matrix Multiplication - Analysis of Strassen BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)61