Presentation is loading. Please wait.

Presentation is loading. Please wait.

1A Recursively Defined Functions

Similar presentations


Presentation on theme: "1A Recursively Defined Functions"— Presentation transcript:

1 1A Recursively Defined Functions

2 f(3) = 3 ∙ f(2) f(2) = 2 ∙ f(1) f(1) = 1 ∙ f(0) f(0) = 1
EX. Define recursively the factorial function f. SOLUTION: Recall that the factorial function f is defined by f(n) = n!, where f(0) = 1. Since n! = n(n - 1)!, f can be defined recursively as follows: f(0) = 1 ← initial condition f(n) = n∙f(n - 1), n ≥ 1 ← recurrence relation f(3) = 3 ∙ f(2) f(2) = 2 ∙ f(1) f(1) = 1 ∙ f(0) f(0) = 1 Suppose we would like to compute f(3) using this recursive definition.

3 Thus A(n) can be defined recursively as follows:
Judy deposits $1000 in a local savings bank at an annual interest rate of 8% compounded annually. Define recursively the compound amount A(n) she will have in her account at the end of n years. Solution Clearly, A(0) = initial deposit = $1000. Let n > 1 then: 𝐴 𝑛 = 𝑐𝑜𝑚𝑝𝑜𝑢𝑛𝑡 𝑎𝑚𝑜𝑢𝑛𝑡 𝑎𝑡 𝑡ℎ𝑒 𝑒𝑛𝑑 𝑜𝑓 𝑡ℎ𝑒 𝑛−1 𝑠𝑡 𝑦𝑒𝑎𝑟 + 𝑖𝑛𝑡𝑒𝑟𝑒𝑠𝑡 𝑒𝑎𝑟𝑛𝑒𝑑 𝑑𝑢𝑟𝑖𝑛𝑔 𝑡ℎ𝑒 𝑛𝑡ℎ 𝑦𝑒𝑎𝑟 =𝐴 𝑛− 𝐴 𝑛−1 =1.08𝐴(𝑛−1) Thus A(n) can be defined recursively as follows: 𝐴 0 = initial condition 𝐴 𝑛 =1.08𝐴 𝑛−1 , 𝑛≥1 recurrence relation

4 For instance, the compound amount Judy will have at the end of three years is
𝐴 3 =1.08𝐴(2) =1.08(1.08𝐴 1 ) =1.08(1.08(1.08(𝐴(0))) =1.08(1.08(1.08(1000))

5 The handshake problem: There are n guests at a ball
The handshake problem: There are n guests at a ball. Each person shakes hands with everybody else exactly once. Define recursively the number of handshakes h(n) that occur. SOLUTION: Clearly, h(1) = 0, so let n > 2. Let x be one of the guests. By definition, the number of handshakes made by the remaining n - 1 guests among themselves is h(n - 1). Now person x shakes hands with each of these n - 1 guests, yielding n additional handshakes. So the total number of handshakes made equals h(n - 1) + (n - 1), where n > 2. Thus h(n) can be defined recursively as follows: h(1) = 0 ← initial condition h(n) = h(n - 1) + (n - 1), n > 2 ← recurrence relation

6 Tower of Brahma (Tower of Hanoi)
According to a legend of India, at the beginning of creation, God stacked 64 golden disk on one of three diamond pegs on a brass platform in the temple of Brahma at Benares. The priest on duty were asked to move the disk for peg X to peg Z using Y as an auxiliary peg under the following condition only one disk can be moved at a time No disk can be placed on the top of a smaller disk The priests were told that the world would end when the job was completed. X Y Z

7 Suppose there are n disks on peg X
Suppose there are n disks on peg X. Let bn denote the number of moves need to move them form peg X to peg Z, using peg Y as an intermediary. Define bn recursively. Clearly b1=1. Assume n ≥ 2. Consider the top n-1 disk on peg X. By definition, it take bn-1 move to transfer them form X to Y using Z as an auxiliary. That leaves the largest disk at peg X; it take one move to transfer it from X to Z. Now the n-1 disk at Y can be moved from Y to Z using X as an intermediary in bn-1 moves, so than the total number of moves needed is 𝑏 𝑛−1 +1+ 𝑏 𝑛−1 =2 𝑏 𝑛−1 +1 The bn can be defined recursively as follows: 𝑏 𝑛 = 𝑖𝑓 𝑛=1 2 𝑏 𝑛−1 +1 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒 x y z

8 For example, 𝑏 4 =2 𝑏 3 +1=2 2 𝑏 =4 𝑏 =4 2 𝑏 =8 𝑏 = = 2 4 −1=15 so it takes 15 moves to transfer 4 disks from X to Z.

9 Example Imagine n lines in a plane such that no two lines are parallel, and no three are concurrent. Let fn denote the number of distinct regions into which the plane is divided by them. Define fn recursively. SOLUTION: If there is just one line l1 in the plane, then f1 = 2 (see Figure) Now consider a second line l2, it is intersected at exactly one point by l1. Each half of l2 divides an original region into two, adding two more regions (see Figure). Thus f2 = f1 + 2 = 4. Suppose we add a third line l3. It is intersected by l1 and l2 in two points; in other words, line l3 is divided by l1 and l2 into three parts. Each portion divides an existing region into two, yielding three new regions (see Figure). So f3 = f2 + 3 = 7.

10 𝑓 𝑛 = 1 𝑖𝑓 𝑛=0 𝑓 𝑛−1 +𝑛 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
Example More generally, suppose there are n – 1 lines l1, l2, …, ln-1 in the plane. They divide the plane into fn-1 disjoint regions, by definition. Now add one more line ln (see Figure). Since no three lines are concurrent, line ln must intersect lines l1, l2, …, ln-1 at new points and hence is divided by them into n segments. Each segment divides an existing region into two sub regions, contributing n more regions, so 𝑓 𝑛 = 𝑓 𝑛−1 +𝑛 . Thus fn can be defined recursively as follows: 𝑓 𝑛 = 𝑖𝑓 𝑛=0 𝑓 𝑛−1 +𝑛 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒

11 Example Let an denote the number of times the assignment statement x  x + 1 is executed by the following nested for loops. Defined an recursively. for i = 1 to n do for j = 1 to i do for k = 1 to j do x  x + 1 First, we must find the initial condition satisfied by an. When n = 1, i=j=k=1, so the assignment statement is executed exactly once. Thus a1 =1 Let n ≥ 2. As i runs from 1 through n-1, be definition, the statement is executed an-1 times. When i = n, the inner loops become: for j = 1 to n do for k = 1 to j do x  x + 1

12 Thus an can be defined as follows: 𝑎 1 =1 𝑎 𝑛 = 𝑎 𝑛−1 + 𝑛(𝑛+1) 2 , 𝑛≥2
For each value of j, where 1<= j <= n, the innermost loop executes the statement j times. So these nested loops execute it 𝑗=1 𝑛 𝑗 = 𝑛(𝑛+1) 2 times. Therefore, 𝑎 𝑛 = 𝑛𝑜. 𝑜𝑓 𝑡𝑖𝑚𝑒𝑠 𝑡ℎ𝑒 𝑠𝑡𝑎𝑡𝑒𝑚𝑒𝑛𝑡 𝑖𝑠 𝑒𝑥𝑒𝑐𝑢𝑡𝑒𝑑 𝑎𝑠 𝑖 𝑟𝑢𝑛 𝑓𝑟𝑜𝑚 1 𝑡ℎ𝑟𝑜𝑢𝑔ℎ 𝑛−1 + 𝑛𝑜. 𝑜𝑓 𝑡𝑖𝑚𝑒 𝑡ℎ𝑒 𝑠𝑡𝑎𝑡𝑒𝑚𝑒𝑛𝑡 𝑖𝑠 𝑒𝑐𝑒𝑐𝑢𝑡𝑒𝑑 𝑤ℎ𝑒𝑛 𝑖=𝑛 = 𝑎 𝑛−1 + 𝑛(𝑛+1) 2 Thus an can be defined as follows: 𝑎 1 =1 𝑎 𝑛 = 𝑎 𝑛−1 + 𝑛(𝑛+1) 2 , 𝑛≥2

13 Fibonacci number Fibonacci considers the growth of an idealized (biologically unrealistic) rabbit population, assuming that: a newly born pair of rabbits, one male, one female, are put in a field; rabbits are able to mate at the age of one month so that at the end of its second month a female can produce another pair of rabbits; rabbits never die and a mating pair always produces one new pair (one male, one female) every month from the second month on. The puzzle that Fibonacci posed was: how many pairs will there be in one year?

14 At the end of the first month, they mate, but there is still only 1 pair.
At the end of the second month the female produces a new pair, so now there are 2 pairs of rabbits in the field. At the end of the third month, the original female produces a second pair, making 3 pairs in all in the field. At the end of the fourth month, the original female has produced yet another new pair, and the female born two months ago also produces her first pair, making 5 pairs. At the end of the nth month, the number of pairs of rabbits is equal to the number of new pairs (which is the number of pairs in month n − 2) plus the number of pairs alive last month (n − 1). This is the nth Fibonacci number.

15 𝐹 1 = 𝐹 2 =1 𝑖𝑛𝑖𝑡𝑖𝑎𝑙 𝑐𝑜𝑛𝑑𝑖𝑡𝑖𝑜𝑛 𝐹 𝑛 = 𝐹 𝑛−1 + 𝐹 𝑛−2 , 𝑛≥3
No of pair/month 1 2 3 4 5 6 7 8 Adults 18 Babies Total 13 21 𝐹 1 = 𝐹 2 =1 𝑖𝑛𝑖𝑡𝑖𝑎𝑙 𝑐𝑜𝑛𝑑𝑖𝑡𝑖𝑜𝑛 𝐹 𝑛 = 𝐹 𝑛−1 + 𝐹 𝑛−2 , 𝑛≥3

16 Mathematical Induction Examples

17 Basis step To verify that P(1) is true
Proof by induction Let 𝑃 𝑛 = 𝑖=1 𝑛 𝑖= 𝑛(𝑛+1) 2 Basis step To verify that P(1) is true When n = 1, RHS = 1(1+1) 2 =1= 𝑖=1 1 𝑖 =1 = LHS; so P(1) is true. Induction step Let k be an arbitrary positive integer. We would like to show that P(k)  P(k+1): Assume P(k) is true that is 𝑖=1 𝑘 𝑖= 𝑘(𝑘+1) 2 induction hypothesis To establish that P(k)  P(k+1) is true, that is 𝑖=1 𝑘+1 𝑖= (𝑘+1)(𝑘+2) 2

18 We start with the LHS of this equation :
= 𝑘(𝑘+1) 2 + 𝑘+1 , by induction hypothesis = 𝑘 𝑘+1 +2(𝑘+1) 2 = 𝑘 2 +3𝑘+2 2 = (𝑘+1)(𝑘+2) 2 = RHS

19 Prove that 2 𝑛 3 +3 𝑛 2 +𝑛 is divisible by 6 for every integer 𝑛≥1.
Proof : Let P 𝑛 :2 𝑛 3 +3 𝑛 2 +𝑛 is divisible by 6. Basis step: When n = 1, 2 𝑛 3 +3 𝑛 2 +𝑛= =6 is clearly divisible by 6. Therefore, P(1) is true. Induction step: Assume P(k) is true, that is, 2 𝑘 3 +3 𝑘 2 +𝑘 is divisible by 6 for any k≥1. Then 2 𝑘 3 +3 𝑘 2 +𝑘=6𝑚 for some integer m (induction hypothesis). We must show that P(k+1) is true: that 2 (𝑘+1) 3 +3 (𝑘+1) 2 + 𝑘+1 is divisible by 6. Notice that

20 2 (𝑘+1) 3 +3 (𝑘+1) 2 + 𝑘+1 =2 𝑘 3 + 3𝑘 2 +3𝑘 𝑘 2 +2𝑘+1 + 𝑘+1 =2 𝑘 3 +6 𝑘 2 +6𝑘+2+3 𝑘 2 +6𝑘+3+𝑘+1 = 2 𝑘 3 + 3𝑘 2 +𝑘 +6 𝑘 2 +2𝑘+1 =6𝑚+6 𝑘 2 +2𝑘+1 by the induction hypothesis =6 𝑚+ 𝑘 2 +2𝑘+1 Which is clearly divisible by 6. Thus P(k+1) is true. Thus, by induction, the given statement is true for every 𝑛≥1.

21 Proof by Induction: Ex: THEOREM 1.3 𝐼𝑓 𝑁≥1 𝑡ℎ𝑒𝑛 𝑖=1 𝑁 𝑖 2 = 𝑁(𝑁+1)(2𝑁+1) 6 PROOF: สำหรับ base case เห็นได้ชัดว่าเป็นจริงที่ n = 1. จาก inductive hypothesis ทฤษฎีเป็นจริงที่ 1  k  N. จากนี้พิสูจน์ว่าทฤษฎีเป็นจริงสำหรับ N + 1

22 เรามี ใช้ inductive hypothesis, จะได้
Proof by Induction: เรามี 𝑖=1 𝑁+1 𝑖 2 = 𝑖=1 𝑁 𝑖 2 + (𝑁+1) 2 ใช้ inductive hypothesis, จะได้ 𝑖=1 𝑁+1 𝑖 2 = 𝑁(𝑁+1)(2𝑁+1) 6 + (𝑁+1) 2 = 𝑁+1 𝑁 2𝑁 𝑁+1 = 𝑁 𝑁 2 +7𝑁+6) 6 = (𝑁+1)(𝑁+2)(2𝑁+3) 6 = (𝑁+1)( 𝑁+1 +1)(2 𝑁+1 +1) 6

23 Bernoulli’s Inequality
Let x be any real number greater than -1. Prove that (1+𝑥) 𝑛 ≥1+𝑛𝑥 for every 𝑛≥0. Proof: Let x be any real number > -1. Let 𝑃 𝑛 : (1+𝑥) 𝑛 ≥1+𝑛𝑥 .(Note: The induction is on the discrete variable n and not on the “continuous” variable x.) Basis step: Te verify that P(0) is true: Notice that (1+𝑥) 0 =1 ≥1+0𝑥 So P(0) is true. Induction step: Assume P(k) is true; that is, (1+𝑥) 𝑘 ≥1+𝑘𝑥 for arbitrary integer 𝑘 ≥0. We need to show that P(k+1) is true; that is (1+𝑥) 𝑘+1 ≥1+(𝑘+1)𝑥.

24 By the induction hypothesis, we have (1+𝑥) 𝑘 ≥1+𝑘𝑥 then
(1+𝑥) 𝑘+1 = 1+𝑥 1+𝑥 𝑘 ≥(1+𝑥)(1+𝑘𝑥) by induction hypothesis since 1+x >0 =1+ 𝑘+1 𝑥+𝑘 𝑥 2 ≥1+ 𝑘+1 𝑥, 𝑠𝑖𝑛𝑐𝑒 𝑘 𝑥 2 ≥0 Therefor, P(k+1) is also true. Thus, (1+𝑥) 𝑛 ≥1+𝑛𝑥 for every 𝑛≥0.

25 พิสูจน์ Fibonacci numbers,
Proof by Induction: พิสูจน์ Fibonacci numbers, F0 = 1, F1 = 1, F2 = 2, F3 = 3, F4 = 5, , Fi = Fi-1 + Fi-2, แล้ว Fi < (5/3)i, for i  1 เป็นจริง (บางครั้งนิยามให้ F0 = 0) เริ่มด้วย base case ซึ่งเป็นกรณีที่ชัดเจนคือ F1 = 1 < 5/3 และ F2 = 2 < 25/9; สมมติในทฤษฎีเป็นจริงสำหรับค่า i = 1, 2, , k; (นี่คือ inductive hypothesis) พิสูจน์กรณี k+1 คือ Fk+1 < (5/3)k+1.

26 จาก Fk + 1= Fk + Fk-1 ใช้ inductive hypothesis กับด้านขวามือ จะได้
Proof by Induction: จาก Fk + 1= Fk + Fk-1 ใช้ inductive hypothesis กับด้านขวามือ จะได้ Fk+1 < (5/3)k + (5/3)k-1 < (3/5)(5/3)k+1 + (3/5)2(5/3)k+1 < (3/5)(5/3)k+1 + (9/25)(5/3)k+1 ซึ่งเขียนใหม่ได้ Fk+1 < (3/5 + 9/25)(5/3)k+1 < (24/25)(5/3)k+1 < (5/3)k+1


Download ppt "1A Recursively Defined Functions"

Similar presentations


Ads by Google