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.

Slides:



Advertisements
Similar presentations
Analysis of Algorithms II
Advertisements

Discrete Math Recurrence Relations 1.
Week 6 - Wednesday CS322.
Chapter 6 Advanced Counting 6.1 Recurrence Relations.
1 More Counting by Mapping Lecture 16: Nov 7. 2 This Lecture Division rule Catalan number.
Recursive Definitions and Structural Induction
Recursion Lecture 18: Nov 18.
CS 2210 (22C:19) Discrete Structures Advanced Counting
1 Copyright M.R.K. Krishna Rao Solving Recurrence Relations Steps for solving a linear homogeneous recurrence relation of degree 2 : Step #1.
Second-Order Differential
Kavita Math231 Recursion and Iteration. Kavita Math231 We use Recursion when we have to perform a complex task that can be broken into the several subtasks.
Recursion Sections 7.1 and 7.2 of Rosen Fall 2008 CSCE 235 Introduction to Discrete Structures Course web-page: cse.unl.edu/~cse235 Questions:
Discrete Structures Chapter 6 Recurrence Relations
1 Section 6.1 Recurrence Relations. 2 Recursive definition of a sequence Specify one or more initial terms Specify rule for obtaining subsequent terms.
6.Advanced Counting Techniques 1 Copyright M.R.K. Krishna Rao 2003 Ch 6. Recurrence Relations A recurrence relation for the sequence {a n } is an equation.
Chapter 8 With Question/Answer Animations 1. Chapter Summary Applications of Recurrence Relations Solving Linear Recurrence Relations Homogeneous Recurrence.
Recursion and Induction Themes –Recursion –Recurrence Definitions –Recursive Relations –Induction (prove properties of recursive programs and objects defined.
Analysis of Recursive Algorithms October 29, 2014
Copyright © Cengage Learning. All rights reserved.
Recursively Defined Sequences Lecture 35 Section 8.1 Wed, Mar 23, 2005.
Applied Discrete Mathematics Week 9: Relations
Advanced Counting Techniques
Rev.S08 MAC 1140 Module 12 Introduction to Sequences, Counting, The Binomial Theorem, and Mathematical Induction.
Induction and recursion
Jessie Zhao Course page: 1.
Chapter 8. Section 8. 1 Section Summary Introduction Modeling with Recurrence Relations Fibonacci Numbers The Tower of Hanoi Counting Problems Algorithms.
Copyright © Cengage Learning. All rights reserved. CHAPTER 5 SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION.
Advanced Counting Techniques CSC-2259 Discrete Structures Konstantin Busch - LSU1.
Chapter 8 With Question/Answer Animations 1. Chapter Summary Applications of Recurrence Relations Solving Linear Recurrence Relations Homogeneous Recurrence.
Solving Recurrence Lecture 19: Nov 25. Some Recursive Programming (Optional?)
CSE 2813 Discrete Structures Recurrence Relations Section 6.1.
R. Johnsonbaugh Discrete Mathematics 7 th edition, 2009 Chapter 7 Recurrence Relations Instructor Tianping Shuai.
15.1 CompSci 102© Michael Frank Today’s topics Recurrence relationsRecurrence relations –Stating recurrences –LiHoReCoCo Reading: Sections Reading:
Solving Second-Order Recursive Relations Lecture 36 ½ Section 8.3 Wed, Apr 19, 2006.
RECURRENCE Sequence Recursively defined sequence
Recursion. 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)?
Chapter 7 Advance Counting Techniques. Content Recurrence relations Generating function The principle of inclusion-exclusion.
Section 2.4. Section Summary Sequences. Examples: Geometric Progression, Arithmetic Progression Recurrence Relations Example: Fibonacci Sequence Summations.
Module #1 - Logic 1 Based on Rosen, Discrete Mathematics & Its Applications. Prepared by (c) , Michael P. Frank and Modified By Mingwu Chen Recurrence.
Chapter 8 With Question/Answer Animations. Chapter Summary Applications of Recurrence Relations Solving Linear Recurrence Relations Homogeneous Recurrence.
after UCI ICS/Math 6A, Summer AdvancedCounting -1 Recurrence Relations (RRs) A “Recurrence Relation”
Advanced Counting Techniques CSC-2259 Discrete Structures Konstantin Busch - LSU1.
Generating Functions and Counting Trees. Today’s Plan 1.Generating functions for basic sequences 2.Operations on generating functions 3.Counting 4.Solve.
Discrete Mathematics Recurrence Relations Chapter 5 R. Johnsonbaugh
Week 6 - Friday.  What did we talk about last time?  Solving recurrence relations.
Chapter 8 Recursion. 8.3 More Recurrence Second-Order Recurrence Definition – A second-order linear homogeneous recurrence relation with constant coefficients.
CS 103 Discrete Structures Lecture 13 Induction and Recursion (1)
Foundations of Discrete Mathematics Chapters 5 By Dr. Dalia M. Gil, Ph.D.
7.2 Solving Linear Recurrence Relations Some of these recurrence relations can be solved using iteration or some other ad hoc technique. However, one important.
CSE 2813 Discrete Structures Solving Recurrence Relations Section 6.2.
Agenda Lecture Content:  Recurrence Relations  Solving Recurrence Relations  Iteration  Linear homogenous recurrence relation of order k with constant.
Mathematical Induction Section 5.1. Climbing an Infinite Ladder Suppose we have an infinite ladder: 1.We can reach the first rung of the ladder. 2.If.
RECURRENCE Sequence Recursively defined sequence
Recursion and Induction Themes –Recursion –Recurrence Definitions –Recursive Relations –Induction (prove properties of recursive programs and objects defined.
Discrete Mathematics Lecture # 22 Recursion.  First of all instead of giving the definition of Recursion we give you an example, you already know the.
1 RECURRENCE 1. Sequence 2. Recursively defined sequence 3. Finding an explicit formula for recurrence relation.
Homogeneous Linear Recurrences To solve such recurrences we must first know how to solve an easier type of recurrence relation: DEF: A linear recurrence.
Recursively Defined Sequences Lecture 40 Section 8.1 Wed, Apr 11, 2007.
Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Advanced Algorithms Analysis and Design By Dr. Nazir Ahmad Zafar Dr Nazir A. Zafar Advanced Algorithms Analysis and Design.
Discrete Math For Computing II. Contact Information B. Prabhakaran Department of Computer Science University of Texas at Dallas Mail Station EC 31, PO.
CMSC Discrete Structures
Modeling with Recurrence Relations
Introduction to Recurrence Relations
Recursion.
CMSC Discrete Structures
CMSC Discrete Structures
Recurrence Relations Discrete Structures.
Chapter 7 Advanced Counting Techniques
Recursively Defined Sequences
Presentation transcript:

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 if I call hello(10)? 2.What if I call hello(-1)? 3.What if the order of printf() and hello() is reversed?

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.

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.

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)

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?

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…

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 ? … The set of all (n-1)-bit strings without 11. Totally r n-1 of them.

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 … The set of all (n-2)-bit strings without 11. Totally r n-2 of them.

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 ? … The set of all (n-2)-bit strings without 11. Totally r n-2 of them … 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

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 r n-3

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 ?

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

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 ?

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.

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,

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.

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

Number of Partitions How many ways to partition n elements into r non-empty groups? (page 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.

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.

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.

Number of Partitions How many ways to partition n elements into r non-empty groups? (page 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)

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.

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

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

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

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

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.

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

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

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

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.

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.

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,…)

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?

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.

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 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.

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:

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 :

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

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.

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!

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.

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.