Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursion Lecture 17: Nov 11. Quiz int hello(int n) { if (n==0) return 0; else printf(“Hello World %d\n”,n); hello(n-1); } 1.What would the program do.

Similar presentations


Presentation on theme: "Recursion Lecture 17: Nov 11. Quiz int hello(int n) { if (n==0) return 0; else printf(“Hello World %d\n”,n); hello(n-1); } 1.What would the program do."— Presentation transcript:

1 Recursion Lecture 17: Nov 11

2 Quiz int hello(int n) { if (n==0) return 0; else printf(“Hello World %d\n”,n); hello(n-1); } 1.What would the program do if I call hello(10)? 2.What if I call hello(-1)? 3.What if the order of printf() and hello() is reversed?

3 Computing Sum of Arithmetic Progression int AP(int n) { if (n==0) return 0; else return (n+AP(n-1)); } Many programs can be written in a recursive way. The way of thinking is quite different. The idea is very similar to induction. Always try to reduce it to smaller problems.

4 Computing Exponential Function int EX(int n) { if (n==0) return 1; else return (EX(n-1)+EX(n-1)); } How many function calls if I run EX(n)? This function is to compute 2 n. 2 n times. If we replace the last line by return 2EX(n-1), then the program will compute the same thing, but there will be only n function calls.

5 Recursively Defined Sequences We can also define a sequence by specifying its recurrence relation. Arithmetic sequence: (a, a+d, a+2d, a+3d, …, ) recursive definition: a 0 =a, a i+1 =a i +d Geometric sequence: (a, ra, r 2 a, r 3 a, …, ) recursive definition: a 0 =a, a i+1 =ra i Harmonic sequence: (1, 1/2, 1/3, 1/4, …, ) recursive definition: a 0 =1, a i+1 =ia i /(i+1)

6 The Rabbit Population A mature boy/girl rabbit pair reproduces every month. Rabbits mature after one month. w n ::= # newborn pairs after n months r n ::= # reproducing pairs after n months Start with a newborn pair: w 0 =1, r 0 = 0 Rabbit Populations How many rabbits after n months?

7 w n ::= # newborn pairs after n months r n ::= # reproducing pairs after n months r 1 = 1 r n = r n-1 + w n-1 w n = r n-1 so r n = r n-1 + r n-2 It was Fibonacci who was studying rabbit population growth. Rabbit Populations We will compute the closed form for r n later…

8 Number of Bit Strings without a Specific Pattern How many n-bit string without the bit pattern 11? Let r n be the number of such strings. Case 1: The first bit is 0. Then any (n-1)-bit string without the bit pattern 11 can be appended to the end to form a n-bit string without 11. So in this case there are exactly r n-1 such n-bit strings. How do we compute it using r 1,r 2,…,r n-1 ? 0+ 00000000000000000 00000000000000001 … 1010101010101010101 The set of all (n-1)-bit strings without 11. Totally r n-1 of them.

9 Number of Bit Strings without a Specific Pattern How many n-bit string without the bit pattern 11? Let r n be the number of such strings. How do we compute it using r 1,r 2,…,r n-1 ? Case 2: The first bit is 1. Then the second bit must be 0, because we can’t have 11. Then any (n-2)-bit string without the bit pattern 11 can be appended to the end to form a n-bit string without 11. So in this case there are exactly r n-2 such n-bit strings. 10+ 0000000000000000 0000000000000001 … 101010101010101010 The set of all (n-2)-bit strings without 11. Totally r n-2 of them.

10 Number of Bit Strings without a Specific Pattern How many n-bit string without the bit pattern 11? Let r n be the number of such strings. How do we compute it using r 1,r 2,…,r n-1 ? 10+ 0000000000000000 0000000000000001 … 101010101010101010 The set of all (n-2)-bit strings without 11. Totally r n-2 of them. 0+ 00000000000000000 00000000000000001 … 1010101010101010101 The set of all (n-1)-bit strings without 11. Totally r n-1 of them. Therefore, r n = r n-1 + r n-2

11 In-Class Exercise How many n-bit string without the bit pattern 111? Let r n be the number of such strings. 10+ r n-2 0+ r n-1 r n = r n-1 + r n-2 + r n-3 110+ r n-3

12 Domino Given a 2xn puzzle, how many ways to fill it with dominos (2x1 tiles)? E.g. There are 3 ways to fill a 2x3 puzzle with dominos. Let r n be the number of ways to fill a 2xn puzzle with dominos. How do we compute it using r 1,r 2,…,r n-1 ?

13 Domino Given a 2xn puzzle, how many ways to fill it with dominos (2x1 tiles)? Let r n be the number of ways to fill a 2xn puzzle with dominos. r n-1 to fill the remaining 2x(n-1) puzzle r n-2 to fill the remaining 2x(n-2) puzzle Case 1: put the domino vertically Case 2: put the domino horizontally Therefore, r n = r n-1 + r n-2

14 Parenthesis How many valid ways to add n pairs of parentheses? ((())) (()()) (())() ()(()) ()()() E.g. There are 5 valid ways to add 3 pairs of parentheses. Let r n be the number of ways to add n pairs of parentheses. How do we compute it using r 1,r 2,…,r n-1 ?

15 Parenthesis How many valid ways to add n pairs of parentheses? Let r n be the number of ways to add n pairs of parentheses. Case 1: ()-------------------- r n-1 ways to add the remaining n-1 pairs. Case 2: (--)------------------ r n-2 ways to add the remaining n-2 pairs. 1 way to add 1 pair Case 3: (----)---------------- r n-3 ways to add the remaining n-3 pairs. 2 ways to add 2 pairs So there are 2xr n-3 in this case. So there are r n-2 in this case. So there are r n-1 in this case.

16 Parenthesis How many valid ways to add n pairs of parentheses? Let r n be the number of ways to add n pairs of parenthese. Case k: (----------)---------- r n-k-1 ways to add the remaining n-k-1 pairs. r k ways to add k pairs By the product rule, there are r k r n-k-1 ways in case k. The cases are depended on the position of the matching closing parenthesis of the first opening parenthesis, and so these cases are disjoint. Therefore, by the sum rule,

17 Parenthesis How many valid ways to add n pairs of parentheses? It turns out that r n has a very nice formula: Unfortunately we won’t derive it in this course… This is called the Catalan number. There are many combinatorial applications of this formula.

18 Number of Partitions How many ways to partition n elements into r non-empty groups? S(4,4)=1{x1} {x2} {x3} {x4} {x1 x2} {x3 x4} {x1 x3} {x2 x4} {x1 x4} {x2 x3} {x1} {x2 x3 x4} {x2} {x1 x3 x4} {x3} {x1 x2 x4} {x4} {x1 x2 x3} {x1 x2} {x3} {x4} {x2 x3} {x1} {x4} {x1 x3} {x2} {x4} {x2 x4} {x1} {x3} {x1 x4} {x2} {x3} {x3 x4} {x1} {x2} S(4,2)=7 S(4,3)=6

19 Number of Partitions How many ways to partition n elements into r non-empty groups? (page 470-472 of the textbook) Case 1: The element n is in its own group. {xn} …………… Let S(n,r) be the number of ways to partition n elements into r groups. Then any partition of the remaining n-1 elements into r-1 groups can be appended to form a parititon of n elements into r groups. So there are S(n-1,r-1) ways in this case.

20 Number of Partitions How many ways to partition n elements into r non-empty groups? Case 2: The element n is NOT in its own group. Let S(n,r) be the number of ways to partition n elements into r groups. In this case, for any partition of n elements into r groups, map this into a partition of n-1 elements into r groups. {x1,x5},{x2,x6,x7},{x3,x11},……,{x4,x12,xn} {x1,x5},{x2,x6,x7},{x3,x11},……,{x4,x12} This is a partition counted in S(n-1,r). This mapping is a r-to-1 mapping. So there are rS(n-1,r) ways to partition in this case.

21 Number of Partitions How many ways to partition n elements into r non-empty groups? Case 2: The element n is NOT in its own group. Let S(n,r) be the number of ways to partition n elements into r groups. To think of it in another way, given any partition of the remaining n-1 elements into r groups, we can extend it in r different ways, and any partition in case 2 can be obtained in this way. {x1,x5},{x2,x6,x7},{x3,x11},……,{x4,x12} {xn} So there are rS(n-1,r) ways to partition in this case.

22 Number of Partitions How many ways to partition n elements into r non-empty groups? (page 470-472 of the textbook) Case 1: The element n is in its own group. Let S(n,r) be the number of ways to partition n elements into r groups. So there are S(n-1,r-1) ways in this case. Case 2: The element n is NOT in its own group. So there are rS(n-1,r) ways to partition in this case. These two cases are disjoint, thus by the sum rule, we have S(n,r) = S(n-1,r-1) + rS(n-1,r)

23 Tower of Hanoi The goal is to move all the disks to post 3. The rule is that a bigger disk cannot be placed on top of a smaller disk.

24 Tower of Hanoi Can you write a program to solve this problem? Think recursively!

25 Move 1,2 (n)::= Move 1,3 (n-1); biggest disk 1  2; Move 3,2 (n-1) Tower of Hanoi http://www.mazeworks.com/hanoi/ To move the biggest disk, we must first move the disks on top to another post.

26 Tower of Hanoi Tower_of_Hanoi(int origin, int destination, int buffer, int number) { if (n==0) return; Tower_of_Hanoi(origin, buffer, destination, number-1); printf(“Move Disk #%d from %d to %d\n”, number, origin, destination); Tower_of_Hanoi(buffer, destination, origin, number-1); } This is the power of recursive thinking. The program is very short, yet there is no easy way to write it without recursion

27 Tower of Hanoi T(A,C,B,3) Tower_of_Hanoi(origin, destination, buffer, number) Move 3 from A to C. T(A,B,C,2) T(B,C,A,2) move 2 from A to B move 2 from B to C T(A,C,B,1) T(C,B,A,1) T(B,A,C,1) T(A,C,B,1) move 1 from A to C move 1 from C to B move 1 from B to A move 1 from A to C 1 2 3 4 5 6 7

28 Tower of Hanoi Suppose your friend gave you a program for moving 9 disks. Call this program T9. Now you want to write a program for moving 10 disks, say T10. Then you use T9 to move the first 9 disks from A to B, move the largest disk from A to C, and then use T9 again to move the first 9 disks from B to C. Once you have a program for T10, you could also write T11 similarly. So, in fact, without recursion, you can write a program for Tower of Hanoi for any n, but one program for each n. There is no mystery here. Then, you realize that the programs are very similar. Like the idea of array of numbers, you can also write it like an array of functions, and that’s exactly the idea of the recursive program.

29 Solving Recurrence a 0 =1, a k = a k-1 + 2 a 1 = a 0 + 2 a 2 = a 1 + 2 = (a 0 + 2) + 2 = a 0 + 4 a 3 = a 2 + 2 = (a 0 + 4) + 2 = a 0 + 6 a 4 = a 3 + 2 = (a 0 + 6) + 2 = a 0 + 8 See the pattern is a k = a 0 + 2k = 2k+1 You can verify by induction.

30 Solving Hanoi Sequence a 1 =1, a k = 2a k-1 + 1 a 2 = 2a 1 + 1 = 3 a 3 = 2a 2 + 1 = 2(2a 1 + 1) + 1 = 4a 1 + 3 = 7 a 4 = 2a 3 + 1 = 2(4a 1 + 3) + 1 = 8a 1 + 7 = 15 a 5 = 2a 4 + 1 = 2(8a 1 + 7) + 1 = 16a 1 + 15 = 31 a 6 = 2a 5 + 1 = 2(16a 1 + 15) + 1 = 32a 1 + 31 = 63 Guess the pattern is a k = 2 k -1 You can verify by induction.

31 Solving Fibonacci Sequence a 0 =0, a 1 =1, a k = a k-1 + a k-2 a 2 = a 1 + a 0 = 1 a 3 = a 2 + a 1 = 2a 1 + a 0 = 2 a 4 = a 3 + a 2 = 2a 2 + a 1 = 3a 1 + 2a 0 = 3 a 5 = a 4 + a 3 = 2a 3 + a 2 = 3a 2 + 2a 1 = 5a 1 + 3a 0 = 5 a 6 = a 5 + a 4 = 2a 4 + a 3 = 3a 3 + 2a 2 = 5a 2 + 3a 1 = 8a 1 + 5a 0 = 8 a 7 = a 6 + a 5 = 2a 5 + a 4 = 3a 4 + 2a 3 = 5a 3 + 3a 2 = 8a 2 + 5a 1 = 13a 1 + 8a 0 = 13 See the pattern a n = a n-k a k+1 + a n-k-1 a k but this does not give a formula for computing a n

32 Second Order Recurrence Relation In the book it is called “second-order linear homogeneous recurrence relation with constant coefficients”. a k = Aa k-1 + Ba k-2 A and B are real numbers and B≠0 For example, Fibonacci sequence is when A=B=1.

33 Geometric-Sequence Solution a k = Aa k-1 + Ba k-2 Find solutions of the form (1, t, t 2, t 3, t 4, …, t n, …) t k = At k-1 + Bt k-2 That is, suppose a k =t k t 2 = At + B t 2 - At – B = 0 So t is a root of the quadratic equation t 2 - At – B = 0.

34 Example a k = a k-1 + 2a k-2 Find solutions of the form (1, t, t 2, t 3, t 4, …, t n, …) So t must be a root of the quadratic equation t 2 - t – 2 = 0. This implies that t=2 or t=-1. So solutions of the form (1, t, t 2, t 3, t 4, …, t n, …) are: (i) (1,2,4,8,16,32,64,…) (ii) (1,-1,1,-1,1,-1,…)

35 Example a k = a k-1 + 2a k-2 So solutions of the form (1, t, t 2, t 3, t 4, …, t n, …) are: (i) (1,2,4,8,16,32,64,…) (ii) (1,-1,1,-1,1,-1,1,…) Are there other solutions? Try (2,1,5,7,17,31,65,…) (0,3,3,9,15,33,63,…) (4,5,13,23,49,95,193,…) How to obtain these solutions?

36 Linear Combination of Two Solutions If (r 0,r 1,r 2,r 3,…) and (s 0,s 1,s 2,s 3,…) are solutions to a k = Aa k-1 + Ba k-2, then the sequence (a 0,a 1,a 2,a 3,…) defined by the formula a k = Cr k + Ds k also satisfies the same recurrence relation for any C and D. (page 490 of the textbook) This is easy to check anyway. This says that any linear combination of two solutions for the recurrence relation is also a solution for the recurrence.

37 Distinct-Roots Theorem Suppose a sequence (a 0,a 1,a 2,a 3,…) satisfies a recurrence relation a k = Aa k-1 + Ba k-2 If t 2 - At – B = 0 has two distinct roots r and s, then a n = Cr n + Ds n for some C and D. (page 491-493 of the textbook) If we are given a 0 and a 1, then C and D are uniquely determined. The theorem says that all the solutions of the recurrence relation are a linear combination of the two series (1,r,r 2,r 3,r 4,…,r n,…) and (1,s,s 2,s 3,s 4,…,s n,…) defined by the distinct roots of t 2 - At – B = 0.

38 Solving Fibonacci Sequence a 0 =0, a 1 =1, a k = a k-1 + a k-2 First solve the quadratic equation t 2 - t – 1 = 0. So the distinct roots are:

39 Solving Fibonacci Sequence a 0 =0, a 1 =1, a k = a k-1 + a k-2 By the distinct-roots theorem, the solutions satisfies the formula: To figure out C and D, we substitute the value of a 0 and a 1 :

40 Multinomial Theorem Solving these two equations, we get: Therefore:

41 Single-Root Case a k = Aa k-1 + Ba k-2 Find solutions of the form (1, t, t 2, t 3, t 4, …, t n, …) Suppose this quadratic equation has only one root r, then we know that (1, r, r 2, r 3, r 4, …, r n, …) satisfies the recurrence relation. Are there other solutions? a k = Aa k-1 + Ba k-2 So t is a root of the quadratic equation t 2 - At – B = 0.

42 Another Solution of the Single-Root Case (0, r, 2r 2, 3r 3, 4r 4, …, nr n, …) also satisfies the recurrence relation. a k = Aa k-1 + Ba k-2 Let r be the single root of the quadratic equation t 2 - At – B = 0. Since r is the single root, A=2r and B=-r 2. a k = 2ra k-1 - r 2 a k-2 Therefore we just need to verify that The right hand side is: which is equal to the left hand side!

43 Single-Root Theorem Suppose a sequence (a 0,a 1,a 2,a 3,…) satisfies a recurrence relation a k = Aa k-1 + Ba k-2 If t 2 - At – B = 0 has only one root r, then a n = Cr n + Dnr n for some C and D. If we are given a 0 and a 1, then C and D are uniquely determined. The theorem says that all the solutions of the recurrence relation are a linear combination of the two series (1,r,r 2,r 3,r 4,…,r n,…) and (0,r,2r 2,3r 3,4r 4,…,nr n,…) defined by the only root of t 2 - At – B = 0.

44 Exercise a 0 =1, a 1 =3, a k = 4a k-1 - 4a k-2 Solve the quadratic equation t 2 – 4t + 4. The only solution is t=2. By the single-root theorem, all solutions are of the form a n = C2 n + Dn2 n. Plug in a 0 and a 1, we solve C=1 and D=1/2. a n = 2 n + n2 n-1.


Download ppt "Recursion Lecture 17: Nov 11. Quiz int hello(int n) { if (n==0) return 0; else printf(“Hello World %d\n”,n); hello(n-1); } 1.What would the program do."

Similar presentations


Ads by Google