CSE 3101. Prof. Andy Mirzaian. Summations & Recurrence Relations. STUDY MATERIAL:. [CLRS] Appendix A, chapter 4 Lecture Note 3. Summations:. Recurrence Relations:. TOPICS. Summations & Recurrence Relations arise in the running time analysis of algorithms. SUMMATIONS:
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation
Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement.Téléchargez la présentation en cliquant sur ce lien.Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.
F I N
Plus de contenu associé
Transcription de la présentation
CSE 3101 Prof. Andy Mirzaian Summations & Recurrence Relations
STUDY MATERIAL: • [CLRS] Appendix A, chapter 4 • Lecture Note 3
f(n) is a step function sandwiched between n and . Why is “g” not true? Caution! To guarantee conclusion of the theorem, its premise must hold. The premise insists that not only f, but also g holds. f without g is possible. The following is an example: However, with Simple Analytical Functions (SAF), i.e., functions composed of only constants, arithmetic, exponential, logarithm, and functional composition, such a “bizarre” situation will not arise, and hence, the statement requiring “g” can be omitted from the premise of the theorem.
Simple Analytical Function (SAF) SAF : a functioncomposed of a finite number of applications of constants, arithmetic, exponential, logarithm, & functional composition. Example: FACTS: A SAF f(n) has the following properties: a) log f(n) andf(x)/x are also SAFs. b) has only a finite # of roots. (does not oscillate) c) is asymptotically sign invariant. (past its last real root) d) is asymptotically monotonic. (part (c) on its derivative) e)In ASSUME: f(n) g(n) f) when comparing SAFs asymptotically, “n0nn0” can be replaced by the phrase “for infinitely many n” (“for i.m. n” for short). E.g., “for all n that are positive integer powers of 2”. (“past last real root”) Our short articlesays more on this topic.The next table shows a summary of ASSUME on SAF.
n n T(n/4) T(n/2) 3n/4 n/4 n/2 log4 n log2 n T(n/16) T(n/8) T(n/8) T(n/4) T(n/4) (3/4)2n n/16 n/8 n/8 n/4 T(n/64) T(n/32) T(n/32) T(n/16) T(n/32) T(n/16) T(n/16) T(n/8) (3/4)3n n/64 n/32 n/32 n/16 n/32 n/16 n/16 n/8 T(n/256) T(n/16) The Recursion Tree Method CLAIM: T(n) = Q(n). Lower bound: T(n) W(n) obvious T(n) Upper bound
The Recursion Tree Method This is a special case of the following recurrence:T(n) = T(an) + T(bn) + Q(n)where 0 a < 1 and 0 b < 1 are real constant parameters. FACT:(1)a + b < 1 T(n) = Q(n) [linear](2) a + b = 1 T(n) = Q(n log n) (3) a + b > 1 T(n) = Q(nd) [super-linear poly]where d > 1 is the unique constant that satisfies the equation ad + bd = 1. [See the guess-&-verify method later& Exercise 9.]
Divide-&-Conquer MergeSort is the prototypical divide-&-conquer algorithm. Here is a high level description: AlgorithmMergeSort( S ) § John von Neumann [1945] Pre-Condition: input S is a sequence of numbers Post-Condition: output is a sorted permutation of the input sequence Base: if |S| 1then returnS Divide:Divide S into its left and right halves S=L, R, |L||R| ½|S| Conquer:L’ MergeSort( L ); R’ MergeSort( R ) Combine:S’ Merge( L’, R’ ) Output:returnS’ end
MERGE Loop Invariant: n merged so far A s f k s-1 i m-1 m j f+1 B right half left half Merge time = Q(n), where n = f – s +1. AlgorithmMergeSort( A[1..n] ) B[0..n+1] auxiliary array for merging MS(1,n) end procedureMS( s, f ) § sort A[s..f] Base:if s fthen return Divide:m (s+f)/2 Conquer:MS( s, m –1 ) ; MS( m, f ) Combine:Merge( s, m, f ) end procedureMerge( s, m, f ) B[s-1 .. m-2] A[s,m-1] B[m .. f] A[m,f] B[m-1]B[f+1] § barrier sentinel i s-1 ; j m for k s .. f do if B[i] B[j]then A[k] B[i]; i++ else A[k] B[j]; j++ end
AlgorithmMergeSort( A[1..n] ) B[0..n+1] auxiliary array for merging MS(1,n) end MergeSort Recurrence: T(n) = Q(1) n 1 T(n) =T( n/2 ) + T( n/2 ) + Q(n) n>1 procedureMS( s, f ) § sort A[s..f] Base:if s fthen return Divide:m (s+f)/2 Conquer:MS( s, m –1 ) ; MS( m, f ) Combine:Merge( s, m, f ) end procedureMerge( s, m, f ) B[s-1 .. m-2] A[s,m-1] B[m .. f] A[m,f] B[m-1]B[f+1] § barrier sentinel i s-1 ; j m for k s .. f do if B[i] B[j]then A[k] B[i]; i++ else A[k] B[j]; j++ end Simplified Recurrence: T(n) = 2 T(n/2) + Q(n) for n > 1 T(n) = Q(1) for n 1 Solution:T(n) = Q( n log n).
Divide-&-Conquer Recurrence a = # of recursive calls (a > 0) n/b = size of each sub-instance called recursively (b > 1) f(n) = cost of divide + combine steps (every thing except the recursive calls). In MergeSort we have: a = 2 recursive calls of size n/b = n/2 each. Cost of divide = Q(1) Cost of Combine, i.e., merge, is Q(n). So, f(n) = Q(n).
f(n) f(n) a af(n/b) f(n/b) f(n/b) f(n/b) a2f(n/b2) f(n/b2) f(n/b2) f(n/b2) f(n/b2) k = logbn f(n/bk-1) ak-1f(n/bk-1) f(n/bk-1) h(n) = Q(n logba) Q(1) Q(1) Q(1) Q(1) a k Recursion Tree: a logb n = n logb a(compare their logarithms) h(n) = ak ·Q(1) = Q(ak) =Q(alogbn) =Q(alogbn) = Q(nlogba) k = logbn = Q( logn).
S(n) = S f(n) T(n) = S(n) + h(n) = Q(max{S(n), h(n)}) h(n) = Q( n logba) af(n/b) a2f(n/b2) ak-1f(n/bk-1) h(n) = Q(n logba) a logb n = n logb a(compare their logarithms) h(n) = ak ·Q(1) = Q(ak) =Q(alogbn) =Q(alogbn) = Q(nlogba) k = logbn = Q( logn).
Special Solution: contribution of the internal nodes of the recursion tree. Homogeneous Solution: contribution of the leaves of the recursion tree. h(n) = Q(n logba) T(n) = S(n) + h(n) T(n) = h(n)( Q(n) + 1 ) f(n) is a SAF r(n) is a SAF
h(n) = Q( n logba ) Example 1: Assume a = 2, b = 4, f(n) = Q(nd log2 n). Express T(n) based on the constant parameter d. Solution:logb a = log4 2 = ½ h(n) = Q() d > ½ T(n) = Q(f(n)) = Q() d = ½ T(n) = Q(f(n) log n) = Q() d < ½ T(n) = Q(h(n)) = Q()
h(n) = Q( n logba ) Example 2: Assume b = 3,f(n) = Q(n4 / log n). Express T(n) based on the constant parameter a. Solution:h(n) = Q(nlog3a) , bd = 34 = 81 a < 81 T(n) = Q(f(n)) = Q(n4 / log n) a = 81 T(n) = Q(h(n) log log n) = Q(n4 log log n) a > 81 T(n) = Q(h(n)) = Q(nlog3a)
Guess-&-Verify BASIC INGRIDIANTS: • Guess a solution to the recurrence. • Verify it by mathematical induction. The induction variable must be a natural number. E.g., height of the recursion tree (max recursion depth), or size of the recursion tree (# times you apply the recurrence). n may also be a candidate if it “stays” integer. • If you spot a place where the verification does not go through, examine it to help revise your guess, and try again. • One key guiding principle, taken with a grain of salt, is to first make sure the leading term (with its constant coefficient) matches on the LHS and RHS of the recurrence. That will show you to guess higher or lower the next time! After figuring out the leading term, you can apply the same principle to find the lower order terms.
we need c 0 Guess-&-Verify: Example Recurrence: T(n) = 2T( n/2 ) + n2 , T(0) = 0 T(n) n2 W(n2) is obvious. Let’s see if O(n2) is an upper-bound: Guess: T(n) an2 + bn + c(for some constants a>0, b, c to be determined) Basis (n = 0): T(0) = 0 a02 + b0 + c Ind. Step (n 1): T(n) = 2T( n/2 ) +n2 2 [ a n/22 + b n/2 + c ] + n2 by induction hypothesis if n is even= 2 [ a(n/2)2 + b(n/2)+c ] + n2 not “” for odd n, if b<0 = (1+ a/2)n2 + bn + 2c an2 + bn + c our guessed upper-bound We need: for all even n 1:an2 + bn + c (1+ a/2)n2 + bn + 2c, i.e.,(a/2 – 1)n2 – c 0.
we need c 0 a = 2 b = -1 c = 0 works for all n 0. n2 T(n) 2n2 - n Guess-&-Verify: Example Recurrence: T(n) = 2T( n/2 ) + n2 , T(0) = 0 T(n) n2 W(n2) is obvious. Let’s see if O(n2) is an upper-bound: Guess: T(n) an2 + bn + c(for some constants a>0, b, c to be determined) Basis (n = 0): T(0) = 0 a02 + b0 + c Ind. Step (n 1): T(n) = 2T( n/2 ) +n2 2 [ a n/22 + b n/2 + c ] + n2 by induction hypothesis if n is odd = 2 [ a((n-1)/2)2 + b((n-1)/2)+c ] + n2 = (1+ a/2)n2 + (b-a)n + (2c-b+a/2) an2 + bn + c our guessed upper-bound We need: for all odd n 1:an2 + bn + c (1+ a/2)n2 + (b-a)n + (2c-b+a/2), i.e.,(a/2 – 1)n2 + an + (b – c – a/2) 0. We need: for all even n 1:an2 + bn + c (1+ a/2)n2 + bn + 2c, i.e.,(a/2 – 1)n2 – c 0.
Guess-&-Verify: Example Recurrence: T(n) = 2T( n/2 ) + n2 , T(0) = 0 Exercise 8: Using this same method, verify the following tighter LOWER BOUND: n1: 2n2 – n – 2n log n T(n) 2n2 – n. T(n) = 2n2 – n – O(n log n). a = 2 b = -1 c = 0 works for all n 0. n2 T(n) 2n2 - n For more examples of guess-&-verify see Lecture Note 2 & Sample Solutions ….
This is the QuickSort expected time recurrence. (We shall show that later). Note this recurrence is discrete version of the continuous integral equation Full History Recurrence Such recurrences often arise in average-case analysis. Values of T(n) for some small n: T(0) = f(0) T(1) = T(0) + f(1) = f(0) + f(1) T(2) = T(0) + T(1) + f(2) = 2f(0) + f(1) + f(2) T(3) = T(0) + T(1) + T(2) + f(3) = 4f(0) + 2f(1) + f(2)+ f(3) T(4) = T(0) + T(1) + T(2) + T(3) + f(4) = 8f(0) + 4f(1) + 2f(2) + f(3) + f(4)
Full History Recurrence A Solution Method: Now there is only one appearance of T(.) on the RHS. Continue with conventional methods.
Connexion au compte
Connectez-vous pour publier votre contenu sur SlideServe.