1 / 28

Recurrence Relations

Recurrence Relations. Part 1 - Definitions & Concepts. Sequences. These are some examples of sequences:. A.) { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 .. }. B.) { 6, 3, 3, 2, 4, 1 }. C.) { 3, 6, 12, 24, 48, 96, 192, 384, 768 .. }.

dterhune
Télécharger la présentation

Recurrence Relations

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Recurrence Relations Part 1 - Definitions & Concepts

  2. Sequences These are some examples of sequences: A.) { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 .. } B.) { 6, 3, 3, 2, 4, 1 } C.) { 3, 6, 12, 24, 48, 96, 192, 384, 768 .. } A sequence is an ordered list of objects, usually numbers, ordered meaning that {1,2,3} is not the same as {3,1,2}. An element or term of a sequence is an object in that sequence. A sequence can be infinite like A and C, meaning it goes on forever, or finite like B. Like the 3 in B, it’s fine for the same object to pop up more than once.

  3. Recursive Sequences A recursive sequence has a rule associated with it that relates an element to the ones that come before it. For example: A.) { 3, 6, 12, 24, 48, 96, 192, 384, 768 .. } Each element in this sequence is twice the element before it; for example, 192 = 2*96. This is true for all of the elements except the 3 at the beginning, which obviously doesn’t have an element before it to multiply by two. B.) { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 .. } This is another example, called the Fibonacci sequence. Each element is the sum of the two before it; 5 = 3+2, 21= 13+8. In this case there are two values that this does not work for, the 0 and 1 at the beginning. For any given recursive sequence, some of the values at the beginning don’t have enough before them to look at. These values have to be given and are called initial values. The further back the rule looks, the more initial values there will be. Sequence A looks one element back, and has one initial value (3); Sequence B looks two elements back and has two initial values (0 and 1)

  4. Recurrence Relations A.) { 3, 6, 12, 24, 48, 96, 192, 384, 768 .. } B.) { 1, 2, 4, 8, 16, 32, 64, 128, 256 .. } Both of these sequences work on the rule that each element is twice the one before it. If we call an the nth element, the “twice the one before it” rule can be written: an = 2 * an-1 and an initial value could be, for ex: a0 = 3 Both A and B follow that rule. The only way they are different is that the initial value is 3 for A, and 1 for B. We would like to be able to point out that they are so similar, so if two sequences follow the same recursion rule, we say that they satisfy the same recurrence relation. In fact we’ll just start calling ‘rules’ recurrence relations. There are many sequences that satisfy a given recurrence relation, like A and B satisfy an = 2* an-1. Each sequence is different from the others only based on its given initial values. Given a recurrence relation and the required initial values, there is only one sequence that works – the solution is unique. So if two sequences have the same initial values and recurrence relation, they are the same sequence.

  5. Quick Note on Programming Programming a recursive sequence is pretty easy. All you have to know is the recurrence relation and the initial values. Say the relation is an = 2*an-1 + 3*an-2. Since it reaches back to n-2, we’ll need two initial values; call them a0=0 and a1=4. Then a program might look like this: public int someFunction ( int n ) { if (n==0) return 0; if (n==1) return 4; return 2*someFunction(n-1)+3*someFunction(n-2); } This version is easy to code, but not necessarily efficient to calculate. Not only will there be duplication of effort (calculating someFunction(4) will compute someFunction(2) twice), but for large values of n you’ll end up with a large stack of recursive calls.

  6. Explicit Solutions & What is to Come • It turns out that the nth value of the relation described by the rule an = 2*an-1 + 3*an-2 and the initial conditions a0=0 and a1=4 can be calculated with the equation an = 3n – (-1)n. Calculating this value with this equation is much more efficient than using the recursive rule. • The rest of this powerpoint is devoted to: • Part 2 : Showing examples of problems involving this type of recurrence relation. • Part 3 : Describing a special subset of recurrence relations for which we can come up with an explicit equation. • Part 4 : Sample problems using this technique.

  7. Happy Fun Times Review! • What is a sequence? • What is a recursive sequence? • What is an initial value? • What is a recurrence relation?

  8. Recurrence Relations Part 2 - Making RRs from real problems

  9. Doing Stuff With R.R.s So, you get what recurrence relations are; but what’s the point? Many problems can be reduced to patterns that can be described by recurrence relations. Here’s an example: How many bit-strings (ex 1001, 010110100, 10111 etc) of some length n that have no consecutive 0s are there? How would you write a program to solve this? First, recognize that every valid bit string ends in either a 1 or a 0. Valid strings of length 3: 111 , 101, 011, 010, 110 • Given all the strings of length 3, to make the strings of length 4, you can: • Take every string and add a 1 to it (always safe to add a 1) • Take every string that ends with a 1 and add a 0 to it (only safe to add a 0 if it currently ends with a 1) • [Continues on next page]

  10. Doing Stuff With R.R.s • So how many strings does this rule represent? • For n bits, • Take every string and add a 1 to it (always safe to add a 1)A copy of everything we have with a 1 added to it • All the valid strings using n-1 bits • Take every string that ends with a 1 and add a 0 to it (only safe to add a 0 if it currently ends with a 1)Everything that we had two steps ago, with a 1 added and then a 0 added • All the strings that were valid at n-2 bits • In other words, the number of strings of length n = number of strings of length n – 1 + number of strings of length n -2 or: • an = an-1 + an-2

  11. Doing Stuff With R.R.s Confused? If so, it may help to actually write out the valid strings of n bits: If you have an idea of what you are looking for, the pattern for the number of strings is not too hard to spot – each term is the sum of the previous two. (8 = 5 + 3, 5 = 3 + 2, …) Although it isn't necessarily obvious how many strings of length 0 we should say there are, you can work backwards and decide that the answer must be 1 to make the pattern work. (If you think about it, there is 1 way to pick zero bits…) This is another approach we can use to decide that: an = an-1 + an-2 with a1 = 2 and a0 = 1

  12. Doing Stuff With R.R.s How many bit-strings (ex 1001, 010110100, 10111 etc) of some length n that have no consecutive 0s are there? Using our recurrence relation, an = an-1 + an-2you could code a soloution to the question doing something like this: public int numOfBS ( int n ) { if (n==0) return 2; if (n==1) return 3; return numOfBS(n-1)+numOfBS(n-2); } This is an easier way to think about the problem than giant lists of possible strings – just try listing all the possible strings of length 10 by hand…

  13. Happy Fun Times Review! • If you remember, the Fibonacci sequence has recurrence relation an = an-1+an-2 and initial values f(0)=0 and f(1)=1. What might a program for the nth Fibonacci number look like? • Say that an= an-1 + 3*an-3. How many initial values would you need? (The answer isn’t 2!) Pick some random initial values and write a program. • How would you solve the following using a recurrence relation? (You might consider this the simplest type of recurrence relation) A fish whose name is Geoffrey puts $10,000 into a bank. Every year he gets 5% interest. What is the recurrence relation for the nth year? What is/are the initial value(s)? Click For Hint Each year the money in the bank is multiplied by 1.05

  14. Recurrence Relations Part 3 - LHRRWCCs (Linear Homogenous Recurrence Relations with Constant Coefficients)

  15. Linear Homogeneous RRs.. ..with constant coefficients. A mouthful of a name that describes relations of the form: an = b1*an-1 + b2 *an-2 + … + bk *an-k an = 3 * an-1 an = 4*an-15,325 an = 2 * an-1 + an-2 Examples: (The b1.. bk are the constant coefficients: In the first example, b1=3) This is a pretty broad category, but there are some things it doesn’t fit. For example, an=1+an-1isn’t one, because that 1 isn’t related to any an’s. Also, factorial, an=n*an-1doesn’t fit either because n is not a constant. The only terms allowed are an’s multiplied by some constant (which can be 0, as they are in the third example for an-1 through an-15,324) Some properties of LHRRWCCs: The ‘k’ in an-k is called the ‘degree’. It’s a measure of how far the RR looks back; it is also the number of initial values that are required. Also, LHRRWCCs can be solved with explicit equations…

  16. Equations - Intro A fish whose name is Geoffrey puts $10,000 into a bank. Every year he gets 5% interest. What is the recurrence relation for the nth year? What is/are the initial value(s)? This is an excellent place to start making equations because it is a rather simple one. If you didn’t get it before, the recurrence relation and initial value: an = 1.05 * an-1 , a0 = 10,000 You might already know the answer to this. Every year, you multiply by 1.05; so, if you want the nth year, you multiply by (1.05)n. f(0) = 10,000 f(2) = 1.05 * 1.05 *10,000 = (1.05)2 * 10,000 … f(1) = 1.05 * 10,000 f(3) = 1.05 * 1.05 * 1.05 * 10,000 = (1.05)3 * 10,000 f(n)= (1.05)n * 10,000 People tend to write that equation like this: an = a0 * rnwhere r=1.05 In fact any recurrence relation an = r * an-1 has the equation an = a0 * rn

  17. LHRRWCC Equations How would you go about making an equation for, say, the Fibonacci sequence? The RR is an= an-1+an-2. If you recall, for degree one (interest problem) the equation is an=a0*rn. If a0=1, that’s just an=rn. We can actually find a similar answer for the Fibonacci sequence. First, its easiest (although not necessary) to rewrite an= an-1+an-2 as an+2= an+1+an; they’re the same thing. So then since an=rn, which also means an+1= r *an: r2 *an= r *an + an r2 *an – r *an – an = 0 which we can rewrite as which means either an = 0, or r2-r-1 = 0 an * ( r2 – r – 1 ) = 0 or Now, an = 0 obviously works, since 0 = 0+0. Since it works for any LHRRWCC as long as all the initial values are 0 also, it’s called a ‘trivial solution’ and we just tend to ignore it. r2 – r – 1 = 0 is called the characteristic polynomial of this particular LHRRWCC, and can be solved using the famous Quadratic Formula, or by factoring, or whatever, but you can solve it. The important thing, though, is that it doesn’t have just one answer; in this case there are two. To see this, if we use the quadratic formula or factor or whatever, we find out r2 – r – 1 = (r – [1+√5]/2) * (r – [1-√5]/2) (factored form) In fact any polynomial P of degree k (degree being largest exponent; 2 in this case) will have k roots, or solutions to P = 0. And, the degree of the characteristic polynomial is the same as the degree of the LHRRWCC, and is the same as the number of initial values necessary for that LHRRWCC. On the next slide, how the roots of the characteristic polynomial are useful is discussed.

  18. Dealing with Characteristic Polynomials Just a little practice with them. If you work out the characteristic polynomial for an= an-1- 2*an-2+ 4*an-3you get r3- r2+ 2r- 4=0. For an = 2.5*an-1 + 13*an-4you get r4-2.5r3-13=0. Do you see a pattern? Basically you just make the coefficients on the right negative. Then, as far as factoring them goes, if you factor r2+6r+8=0 into (r+4)(r+2)=0 , the roots are actually -4 and -2. For (r-4)(r-2)=0, they’re 4 and 2. You make the numbers negative. If you don’t factor but use the quadratic formula instead, the roots for ar2+br+c=0 are [-b±√(b2-4ac)]/2a, and you don’t have to make them negative. It’s also possible to have complex roots, like for r2+r+1=0 whose roots are -1/2 + i*(√3)/2 and -1/2 - i*(√3)/2 . The recurrence relation is an=-an-1-an-2andfor initial values 0 and 1, this corresponds to the sequence { 0, 1, -1, 0, 1, -1, 0, 1, … } which although it looks different than most since it repeats, is still made up of real numbers. So if you get complex roots, don’t panic. For polynomials of degree > 2, it becomes a lot harder to factor and you may want instead to try out some root-finding calculator on teh interwebs, of which there are many.

  19. Once You Have Roots So, what is useful about the roots [1+√5]/2 and [1-√5]/2? Well, for one thing they are solutions. For example, [1+√5]/2 ≈ 1.618: a0= 1.6180 =1 a2=1.6182=2.618 a4=1.6184=6.854 (these numbers are a1 = 1.6181 = 1.618 a3=1.6183=4.236 a5=1.6185=11.09 rounded) You can see that this satisfies the RR an= an-1+an-2: 2.618=1+1.618, 6.854 = 4.236+2.618, etc. So both of the roots work in the equation an=rn. However, that isn’t all that works. Say you have two sequences that work; the one above and the Fibonacci sequence, for example. If we call this one A and the Fib F, An=An-1+An-2and Fn=Fn-1+Fn-2. What if you added these two together? Call that one, I don’t know, H. So Hn= An+ Fn = (An-1+An-2) + (Fn-1+Fn-2) = (An-1 + Fn-1) + (An-2 + Fn-2) = Hn-1 + Hn-2. So, if you add them, you get another one that works. This is because they are LHRRWCCs; specifically because they are Linear. Given some whatevers, a linear combination is what you get by multiplying each one by a coefficient and then adding them together. For example, if you’ve done vectors, you know that you can turn any 2d vector into its x and y components, or x*i + y*j. That’s a linear combination. In other words, any point on an x-y graph is a linear combination of some distance along the x axis, i, and some distance along the y axis, j. i and j are called the ‘bases’, bases just basically being whatever you’re linear-combination-ing. In the same way, the roots of the characteristic polynomial, in this case [1+√5]/2 and [1-√5]/2, are the bases for every possible sequence that satisfies the recurrence relation. Formally, if you call the first one r1and the second one r2, and if you call their coefficients b1and b2, then the equation for some sequence a looks like: an = b1*r1 + b2*r2

  20. Well, That's Not Quite True So, say we have a LHRRWCC of a sequence a, and we have turned it into its characteristic polynomial P of degree k. Also, we’ve done a lot of math that we’re quite proud of and we’ve got the roots of P, specifically r1, r2 .. rk. Then an=b1r1n+b2r2n+..+bkrkn, assuming we’ve figured out the b’s which we’ll get to later. This is all well and good, but only if all of the r’s are different! Here’s an example of a characteristic polynomial that this doesn’t work for: r2-2r+1=0 which factors to (r-1)(r-1)=0. Horrors! We have two roots, but, they’re the same! It turns out there’s an easy fix. First, an= b1rn still works. All we have to do is add in a second thing that also happens to work, namely an=b2*n*rn. If we had three roots that were the same, we’d have to add in an=b3n2rn also. If our characteristic polynomial was (r-1)(r-1)(r-1)(r-5)(r-5)(r+11)=0 The general equation would look like an = b1(1)n + b2n(1)n + b3n2(1)n + b4(5)n + b5n(5)n + b6(-11)n A couple side notes. First, note that if you factor a polynomial, the roots are negative of what’s in the factor. That is, if (r+6) is a factor, -6 is the root, and if its (r-6), 6 is the root. Second, the number of times a root pops up is called its multiplicity. That is, in the above example, the root 1 had multiplicity 3, 5 had multiplicity 2, and -11 had multiplicity 1. And also, it does continue like it looks like it should; with multiplicity 4, you’ll have a bn3rn, 5 is bn4rn, etc.

  21. Almost done! Dealing with b's The last step is to figure out the coefficients. Now, since every sequence that satisfies the same recurrence relation has the same characteristic polynomial, and therefore the same roots. So the only thing different in the equation are the coefficients, while the only thing that’s different in the actual sequences we already know are the initial values. And also, there are just as many coefficients as there are roots, and there are just as many roots as initial values. So it makes sense that we work out the coefficients by using the initial values. Let’s find the coefficients for the Fibonacci sequence. We already know the roots, so: an = b1 [ (1+√5)/2]n + b2 [(1- √5)/2]n If we plug in 0 and 1, which are the ns for the two initial values, a0 = b1 + b2= 0, a1 = b1 [ (1+√5)/2] + b2 [(1- √5)/2] = 1 This is a system of equations. You can mess about with substitution and elimination, or you can use matrices and have a calculator do it for you. Eh, I dunno, your choice. With matrices it looks something (decimals aren’t exact) like: A More General Form To solve this, if you put the 2x2 into your calculator as [A] and the [0,1] as [B] then [A]-1[B] will give you the answer.

  22. Happy Fun Times Review! • What’s the equation for an=5*an-1if a0=13? • What is the characteristic equation for an= an-1 + 12an-2? What are its roots? • What’s a general equation given the characteristic polynomial (r-1)(r-1)(r+5)=0? • Given the RR an=3an-1-2an-2, and the initial values a0=15, a1=25, find a happy magic equation.

  23. Recurrence Relations Part 4 - Problems

  24. Koch Snowflake Start with an equilateral triangle. Every step, remove the middle third of every line and add in two of the sides of an equilateral triangle. (Call the first triangle the 0th iteration) 0 1 Q1: What is the perimeter of the n-th iteration if the starting side length is L (so the starting perimeter is 3L) ? 2 lots Q2: If the A/3 is the area ADDED the 0th time, that is the area of the three little triangles, what is the amount of area ADDED the n-th time? Note that in this case, the n=0 corresponds to the difference between the area of iteration n=1 and iteration n=0. Area added 0th time Area added 1st time

  25. Koch Snowflake - Solutions Q1: Notice that every iteration, each line becomes 4/3 as long as it was before: 1 2 3 4 1 2 3 So, the nth perimeter pn = 4/3 * pn-1. You should recognize that the equation is then (4/3)n *p0, and if the side length is L, p0=3L pn = (4/3)n * 3L Q2: Because every iteration, each line is broken from one line into 4 lines, every iteration will have 4 times as many triangles added to it as the last one. And, each triangle will have a side length of 1/3 what it had last time, which means 1/9 the area. That means that between each iteration, the area added is 4/9 what was added the time before. So, an = 4/9*an-1. You’re given that a0=A/3, (the /3 so that A is the area of the original triangle, but that doesn’t matter so much) so the equation is an = (4/9)n * A/3 Incidentally, if you add up the area added each time plus the starting area in order to get the total area, you end up with what’s called a geometric series. Those are included in the “Helpful Extras”.

  26. Coloring You only have to find the recurrence relations and initial values for all of them; just find an equation for one of them (not Q1!) Q1. Say you have two letters, A and B, to use. If you can’t have the same letter twice in a row, how many strings of length n can you make? Q2. What if you have three letters? (A,B,C) With three letters: A, B, C, AB, AC, BA, BC, CA, CB, ABA, ABC, ACA … Q3. What if you have two letters and can have the same letter up to twice in a row? Q4. With three letters? Q5. With up to three in a row, and four letters? A, B, C, AA, AB, AC, … AAB, AAC, ABA, ABB, ABC … Bonus: Have you found a pattern yet? Try to find a general sort of pattern for L letters and R allowed in a row. (It’s called ‘coloring’ because if the letters are called colors, it’s how many ways to color a line with only so many of the same color in a row. Coloring is a whole subject in mathematics, because math is so serious a subject that it is easy to get away with being childish.)

  27. Coloring - Solutions Q1. So, there’s only one possible letter to add at any time. That is to say, an = an-1. Initially, you have 2 options: A or B. Q2. Now there are two possible letters to add. an = 2*an-1. Q3. Those of length n can be divided into those with a mismatch at the end (AB, BA) and those with double at the end (AA,BB). The mismatches come from adding the other letter to the end of an n-1, (A = > AB) the doubles come from adding two of the other letter to the end of an n-2 (A => ABB). an = an-1 + an-2. Initial is A/B, AA/AB/BA/BB: a1 = 2, a2 = 4. Q4. Same as above, but there’re two options each time (A => AB,AC). an = 2*an-1+2*an-2. Also, there are different initial values: a1 = 3, a2 = 9. Q5.There are those that end in one of the same, BA, those that end in two of the same, and those that end in three of the same. A => AB, AC, AD ; A => ABB, ACC, ADD ; A => ABBB, ACCC, ADDD. Each time there’re three options for colors (the ones that aren’t the previous color), and each can be made from any working string of the correct length (Singles from n-1, doubles from n-2, etc). an = 3*an-1 + 3*an-2 + 3*an-3. Initial values are a1=4, a2=16, a3=64. Q6. In general, the RR is an = (C-1)*an-1 + .. + (C-1)*an-R, with initial values a1=C, a2=C^2 .. aR=C^R.

  28. Nonhomogeneous RRs Many useful recurrence relations are non-homogeneous. Something like: an = an-1 + 13n2 – 5 In other words, it’s an ordinary LHRRWCC, except that added to the end of it are a few terms that might depend on n but not on previous values of an. For information on solving these, see: http://www.hlt.utdallas.edu/~vh/Courses/Fall09/DiscreteMath/Lectures/Non-homogeneous%20recurrence%20relations.ppt#360,9,Example

More Related