[PDF] CS 161 Lecture 3 - 1 Solving recurrences





Previous PDF Next PDF



CS 161 Lecture 3 - 1 Solving recurrences

Last class we introduced recurrence relations such as T(n) = 2T(?n/2?) + n. Typically Here a = 9



Discrete Mathematics II (Spring 2015) - 8.2 Solving Linear

Determine what is the degree of the recurrence relation. • Need to know the general solution equations. • Need to find characteristic equation. • Need to find 



Recurrences

n = 2k for some constant k and leaving out base case and constant in ?). Methods for solving recurrences. 1. Substitution method. 2. Iteration method.



Q1: Exercise 2.3-3 (10 points) Use mathematical induction to show

recurrence. T(n) =2 ifn=2. 2T(n/2) + n if n = 2k for k > 1 is T(n) = n logn. . Base Step: If n = 2



Master Theorem: Practice Problems and Solutions

The Master Theorem applies to recurrences of the following form: 2. T(n)=4T(n/2) + n2. 3. T(n) = T(n/2) + 2n. 4. T(n)=2nT(n/2) + nn.



GENERATING FUNCTIONS AND RECURRENCE RELATIONS

A recurrence recurrence relation is a set of equations Linear Recurrence. Fibonacci Sequence an = an?1 + an?2 n ? 2. ... n=2 n(n ? 1)xn +.



Solving Recurrences

For example the following recurrence (written in two different but standard ways) describes the identity function f (n) = n:.



1.1 Time complexity and Big-Oh notation: exercises

2. A quadratic algorithm with processing time T(n) = cn2 spends T(N) To have the well-defined recurrence assume that n = km with the integer.



Sample Induction Proofs

(?1)nn(n + 1). 2 . Base case: When n = 1 the left side of (1) is (?1)12 = ?1



3. Recurrence 3.1. Recursive Definitions. To construct a recursively

Example 3.2.1. The factorial function f(n) = n! is defined recursively as follows: 1. Initial Condition: f(0) = 1. 2. Recursion: f(n +1)=(n + 1)f(n).



Recurrence Relations - Hong Kong University of Science and

an= 2 n¡1; n ‚1: Given a recurrence relation for a sequence with initial conditions Solving the recurrence relation means to ?nd a formula to express the general termanof the sequence 2 Homogeneous Recurrence Relations Any recurrence relation of the form



Solving Recurrence Relations - Texas A&M University

n 2 and the base cases of the induction proof (which is not the same as the base case of the recurrence!) are n= 2 and n= 3 (We are allowed to do this because asymptotic notation only requires us to prove our statement for n n 0 and we can set n 0 = 2 ) We choose n= 2 and n= 3 for our base cases because when we expand the recurrence



Recurrence Relations - University of Ottawa

Many sequences can be a solution for the same recurrence relation a n = 2a n 1 a n 2; for n 2 The following sequences are solutions of this recurrence relation: I a n = 3n for all n 0 I a n = 5 for all n 0 The initial conditions for a sequence specify the terms before n 0 (before the recurrence relation takes e ect)



Solving Recurrence Relations - Princeton University

n 2 to be C0 and C1 Since the sequences fang and f 1rn 1 + 2r n 2g satisfy the degree 2 recurrence and agree on the rst two values they must be identical Example: Find the solution to the recurrence relation an = an 1 +an 2 with initial condi-tions a0 = 2 and a1 = 7 Solution: The characteristic equation is r2 r 2 = 0 i e (r 2)(r +1) = 0



Recurrence Relations - University of Illinois Urbana-Champaign

2 Recall that 1=nk= nk and we have rn 2(r25r + 6) = 0 Then we can factor the polynomial into rn 2(r 2)(r 3) = 0 We assumed that r is non-zero so we can divide by the rst factor rn 2 which leaves (r 2)(r 3) = 0 Thus the characteristic function (also: polynomial) remains on the left-hand side



Searches related to 2^n n^2 recurrence PDF

n = 2n for every nonnegative integer n is a solution of the recurrence relation a n = 2a n-1 - a n-2 for n = 234 Assume a 0=1 and a 1=2 Solution: Check initial conditions a 0= 20 = 1 a 1= 2 1 = 2 Check if {2n} satisfies a n = 2a n-1 - a n-2 2a n-1 - a n-2 = 2(2n-1) - 2n-2 = 2n-2(4-1) = 3(2n-2) 2n So {2n} is not a solution for a n = 2a

What is the solution of the recurrence relation?

.Thenasequence (a n ) is a solution of the recurrence relation a n= c 1a n?1+ c 2a n?2 if and only if a n= ? 1r n 1+ ? 2r n 2 for n ? 0 for some constants ? 1,? 2 Idea of the Proof The proof proceeds exactly as in the case of the Fibonacci numbers. Try to prove it yourself!

What is the solution of recurrence relation an = 10an-1 - 25an-2?

The solution of the recurrence relation an = 10an-1 - 25an-2, is: (a0 = 1, a1 = 15) Q8. Solution to recurrence relation T (n) = T (n - 1) + 4 is given by, where n > 0 and T (0) = 3.

What is a 2 nd-order linear recurrence?

This is a subset of the type of 2 nd -order recurrence formulas used by MCS . The Fibonacci number s are the best-known example of a 2 nd -order linear recurrence.

What is the initial condition for a recurrence relation?

So (a_n = 3a_ {n-1} + 2) is our recurrence relation and the initial condition is (a_0 = 1 ext {.}) We are going to try to solve these recurrence relations.

CS 161 Lecture 3 Jessica Su (some parts copied from CLRS)

1 Solving recurrences

Last class we introduced recurrence relations, such asT(n) = 2T(bn=2c) +n. Typically these re ect the runtime of recursive algorithms. For example, the recurrence above would correspond to an algorithm that made two recursive calls on subproblems of sizebn=2c, and then didnunits of additional work. Today we will be learning about how to solve these recurrences to get bounds on the runtime (likeT(n) =O(nlogn)).

1.1 Substitution method

A lot of things in this class reduce to induction. In the substitution method for solving recurrences we

1. Guess the form of the solution.

2. Use mathematical induction to nd the constants and show that the solution works.

1.1.1 Example

Recurrence:T(1) = 1 andT(n) = 2T(bn=2c) +nforn >1. We guess that the solution isT(n) =O(nlogn). So we must prove thatT(n)cnlognfor some constantc. (We will get ton0later, but for now let's try to prove the statement for alln1.) As our inductive hypothesis, we assumeT(n)cnlognfor all positive numbers less than n. Therefore,T(bn=2c)cbn=2clog(bn=2c)), and

T(n)2(cbn=2clog(bn=2c)) +n

cnlog(n=2) +n =cnlogncnlog2 +n =cnlogncn+n cnlogn(forc1) Now we need to show the base case. This is tricky, because ifT(n)cnlogn, thenT(1)0, which is not a thing. So we revise our induction so that we only prove the statement for n2, and the base cases of the induction proof (which is not the same as the base case of the recurrence!) aren= 2 andn= 3. (We are allowed to do this because asymptotic notation only requires us to prove our statement fornn0, and we can setn0= 2.) We choosen= 2 andn= 3 for our base cases because when we expand the recurrence formula, we will always go through eithern= 2 orn= 3 before we hit the case wheren= 1. 1 CS 161 Lecture 3 Jessica Su (some parts copied from CLRS) So proving the inductive step as above, plus proving the bound works forn= 2 andn= 3, suces for our proof that the bound works for alln >1. Plugging the numbers into the recurrence formula, we getT(2) = 2T(1) + 2 = 4 and T(3) = 2T(1) + 3 = 5. So now we just need to choose acthat satises those constraints on T(2) andT(3). We can choosec= 2, because 422log2 and 523log3. Therefore, we have shown thatT(n)2nlognfor alln2, soT(n) =O(nlogn).

1.1.2 Warnings

Warning:Using the substitution method, it is easy to prove a weaker bound than the one you're supposed to prove. For instance, if the runtime isO(n), you might still be able to substitutecn2into the recurrence and prove that the bound isO(n2). Which is technically true, but don't let it mislead you into thinking it's the best bound on the runtime. People often get burned by this on exams! Warning:You must prove the exact form of the induction hypothesis. For example, in the recurrenceT(n) = 2T(bn=2c) +n, we could falsely \prove"T(n) =O(n) by guessing T(n)cnand then arguingT(n)2(cbn=2c) +ncn+n=O(n). Here we needed to proveT(n)cn, notT(n)(c+ 1)n. Accumulated over many recursive calls, those \plus ones" add up.

1.2 Recursion tree

A recursion tree is a tree where each node represents the cost of a certain recursive sub- problem. Then you can sum up the numbers in each node to get the cost of the entire algorithm. Note: We would usually use a recursion tree to generate possible guesses for the runtime, and then use the substitution method to prove them. However, if you are very careful when drawing out a recursion tree and summing the costs, you can actually use a recursion tree as a direct proof of a solution to a recurrence. If we are only using recursion trees to generate guesses and not prove anything, we can tolerate a certain amount of \sloppiness" in our analysis. For example, we can ignore oors and ceilings when solving our recurrences, as they usually do not aect the nal guess.

1.2.1 Example

Recurrence:T(n) = 3T(bn=4c) + (n2)

We drop the

oors and write a recursion tree forT(n) = 3T(n=4) +cn2. 2 CS 161 Lecture 3 Jessica Su (some parts copied from CLRS) The top node has costcn2, because the rst call to the function doescn2units of work, aside from the work done inside the recursive subcalls. The nodes on the second layer all have costc(n=4)2, because the functions are now being called on problems of sizen=4, and the functions are doingc(n=4)2units of work, aside from the work done inside their recursive subcalls, etc. The bottom layer (base case) is special because each of them contributeT(1) to the cost. Analysis:First we nd the height of the recursion tree. Observe that a node at depth ire ects a subproblem of sizen=4i. The subproblem size hitsn= 1 whenn=4i= 1, or 3 CS 161 Lecture 3 Jessica Su (some parts copied from CLRS) i= log4n. So the tree has log4n+ 1 levels. Now we determine the cost of each level of the tree. The number of nodes at depthiis 3i. Each node at depthi= 0;1;:::;log4n1 has a cost ofc(n=4i)2, so the total cost of leveli is 3 ic(n=4i)2= (3=16)icn2. However, the bottom level is special. Each of the bottom nodes contribute costT(1), and there are 3log4n=nlog43of them.

So the total cost of the entire tree is

T(n) =cn2+316

cn2+316 2 cn

2++316

log4n1 cn

2+ (nlog43)

log 4n1X i=0 316
i cn

2+ (nlog43)

The left term is just the sum of a geometric series. SoT(n) evaluates to (3=16)log4n1(3=16)1cn2+ (nlog43) This looks complicated but we can bound it (from above) by the sum of the innite series 1 X i=0 316
i cn

2+ (nlog43) =11(3=16)cn2+ (nlog43)

Since functions in (nlog43) are also inO(n2), this whole expression isO(n2). Therefore, we can guess thatT(n) =O(n2).

1.2.2 Back to the substitution method

Now we can check our guess using the substitution method. Recall that the original recur- rence wasT(n) = 3T(bn=4c) + (n2). We want to show thatT(n)dn2for some constant d >0. By the induction hypothesis, we have thatT(bn=4c)dbn=4c2. So using the same constantc >0 as before, we have

T(n)3T(bn=4c) +cn2

3dbn=4c2+cn2

3d(n=4)2+cn2

316
dn2+cn2 dn2(whenc(13=16)d;i.e.d(16=13)c) 4 CS 161 Lecture 3 Jessica Su (some parts copied from CLRS) Note that we would also have to identify a suitable base case and prove the recurrence is true for the base case, and we don't have time to talk about this in lecture, but you should do that in your homework.

1.3 Master theorem

The master theorem is a formula for solving recurrences of the formT(n) =aT(n=b)+f(n), wherea1 andb >1 andf(n) is asymptotically positive. (Asymptotically positive means that the function is positive for all suciently largen.) This recurrence describes an algorithm that divides a problem of sizenintoasubproblems, each of sizen=b, and solves them recursively. (Note thatn=bmight not be an integer, but in section 4.6 of the book, they prove that replacingT(n=b) withT(bn=bc) orT(dn=be) does not aect the asymptotic behavior of the recurrence. So we will just ignore oors and ceilings here.)

The theorem is as follows:The master theorem compares the functionnlogbato the functionf(n). Intuitively, ifnlogba

is larger (by a polynomial factor), then the solution isT(n) = (nlogba). Iff(n) is larger (by a polynomial factor), then the solution isT(n) = (f(n)). If they are the same size, then we multiply by a logarithmic factor. Be warned that these cases are not exhaustive { for example, it is possible forf(n) to be asymptotically larger thannlogba, but not larger by a polynomial factor (no matter how small the exponent in the polynomial is). For example, this is true whenf(n) =nlogbalogn. In this situation, the master theorem would not apply, and you would have to use another method to solve the recurrence. 5 CS 161 Lecture 3 Jessica Su (some parts copied from CLRS)

1.3.1 Examples:

To use the master theorem, we simply plug the numbers into the formula. Example 1:T(n) = 9T(n=3)+n. Herea= 9,b= 3,f(n) =n, andnlogba=nlog39= (n2). Sincef(n) =O(nlog39) for= 1, case 1 of the master theorem applies, and the solution is

T(n) = (n2).

Example 2:T(n) =T(2n=3)+1. Herea= 1,b= 3=2,f(n) = 1, andnlogba=n0= 1. Since f(n) = (nlogba), case 2 of the master theorem applies, so the solution isT(n) = (logn). Example 3:T(n) = 3T(n=4) +nlogn. Herenlogba=nlog43=O(n0:793). For= 0:2, we havef(n) = (nlog43+). So case 3 applies if we can show thataf(n=b)cf(n) for some c <1 and all suciently largen. This would mean 3n4 logn4 cnlogn. Settingc= 3=4 would cause this condition to be satised. Example 4:T(n) = 2T(n=2)+nlogn. Here the master method does not apply.nlogba=n, andf(n) =nlogn. Case 3 does not apply because even thoughnlognis asymptotically larger thann, it is not polynomially larger. That is, the ratiof(n)=nlogba= lognis asymp- totically less thannfor all positive constants.

2 Maximum subarray problem

Now we will present another divide and conquer algorithm. Suppose you are given the price of a stock on each day, and you have to decide when to buy and when to sell to maximize your prot. Note that you cannot sell before you buy (so you can't just sell on the highest day and buy on the lowest day). Naive strategy:Try all pairs of (buy, sell) dates, where the buy date must be before the sell date. This takes (n2) time. bestProfit = -MAX_INT bestBuyDate = None bestSellDate = None for i = 1 to n: for j = i + 1 to n: if price[j] - price[i] > bestProfit: bestBuyDate = i bestSellDate = j bestProfit = price[j] - price[i] return (bestBuyDate, bestSellDate) 6 CS 161 Lecture 3 Jessica Su (some parts copied from CLRS)

2.1 Divide and conquer strategy

Instead of the daily price, consider the daily change in price, which (on each day) can be either a positive or negative number. Let arrayAstore these changes. Now we have to nd the subarray ofAthat maximizes the sum of the numbers in that subarray. Now divide the array into two. Any maximum subarray must either be entirely in the rst half, entirely in the second half, or it must span the border between the rst and the second half. If the maximum subarray is entirely in the rst half (or the second half), we can nd it using a recursive call on a subproblem half as large. If the maximum subarray spans the border, then the sum of that array is the sum of two parts: the part between the buy date and the border, and the part between the border and the sell date. To maximize the sum of the array, we must maximize the sum of each part. We can do this by simply (1) iterating over all possible buy dates to maximize the rst part (2) iterating over all possible sell dates to maximize the second part. Note that this takes linear time instead of quadratic time, because we no longer have to iterate over buy and sell dates simultaneously.7 CS 161 Lecture 3 Jessica Su (some parts copied from CLRS)

2.1.1 Runtime analysis

Note that we are omitting the correctness proof, because the main point is to give an example of the divide and conquer strategy. In the homework, you would normally need to provide a correctness proof, unless we say otherwise. First we analyze the runtime of FindMaxCrossingSubarray. Since each iteration of each of the two for loops takes (1) time, we just need to count up how many iterations there are altogether. The for loop of lines 3-7 makesmidlow+1 iterations, and the for loop of lines

10-14 makeshighmiditerations, so the total number of iterations ishighlow+ 1 =n.

Therefore, the helper function takes (n) time.

Now we proceed to analyze the runtime of the main function. For the base case,T(1) = (1), since line 2 takes constant time. For the recursive case, lines 1 and 3 take constant time. Lines 4 and 5 takeT(bn=2c) andT(dn=2e) time, since each of the subproblems has that many elements. The FindMax- CrossingSubarray procedure takes (n) time, and the rest of the code takes (1) time. So T(n) = (1) +T(bn=2c) +T(dn=2e) + (n) + (1) = 2T(n=2) + (n) (ignoring the oors and ceilings). By case 2 of the master theorem, this recurrence has the solutionT(n) = (nlogn). 8 CS 161 Lecture 3 Jessica Su (some parts copied from CLRS)

3 If we have extra time

3.1 Change of variables (substitution method)

T(n) = 2T(bpnc) + logn.

For now don't worry about rounding o values to be integers.

Letm= logn, i.e.n= 2m. ThenT(2m) = 2T(2m=2) +m.

Now letS(m) =T(2m). ThenS(m) = 2S(m=2) +m.

This recurrence has the solutionS(m) =O(mlogm).

SoT(n) =T(2m) =S(m) =O(mlogm) =O(lognloglogn).

quotesdbs_dbs11.pdfusesText_17
[PDF] la composante vivante dun milieu naturel

[PDF] les composantes de lenvironnement 6ème

[PDF] quelles sont les composantes de lenvironnement

[PDF] les composantes de l'environnement svt 6ème

[PDF] les composantes dun milieu naturel

[PDF] quelles sont les composantes dun milieu naturel

[PDF] les elements qui composent l'environnement

[PDF] les composantes de lenvironnement pdf

[PDF] 1.1.1 tenue des dossiers fournisseurs et sous-traitants

[PDF] 1.1.4 evaluation et suivi des stocks

[PDF] 1.2.3 traitement des devis des commandes

[PDF] 1.2.4 traitement des livraisons et de la facturation

[PDF] 1.3.1 suivi de la trésorerie et des relations avec les banques

[PDF] tenue des dossiers clients donneurs d ordre et usagers

[PDF] 2.2.3 suivi administratif des carrières