1A Recursively Defined Functions

Slides:



Advertisements
Similar presentations
Analysis of Algorithms II
Advertisements

Chapter Recurrence Relations
MAT 4 – Kompleks Funktionsteori MATEMATIK 4 INDUKTION OG REKURSION MM 1.4 MM 1.4: Induktion og Rekursion Topics: Mathematical induction Example of Towers.
Recursive Definitions and Structural Induction
CSE115/ENGR160 Discrete Mathematics 04/19/12 Ming-Hsuan Yang UC Merced 1.
CSE115/ENGR160 Discrete Mathematics 04/21/11 Ming-Hsuan Yang UC Merced 1.
Recursive Definitions Rosen, 3.4. Recursive (or inductive) Definitions Sometimes easier to define an object in terms of itself. This process is called.
Recursion.
Lecture Recursive Definitions. Fractals fractals are examples of images where the same elements is being recursively.
1 Section 6.1 Recurrence Relations. 2 Recursive definition of a sequence Specify one or more initial terms Specify rule for obtaining subsequent terms.
Copyright © Cengage Learning. All rights reserved. CHAPTER 5 SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION SEQUENCES, MATHEMATICAL INDUCTION, AND RECURSION.
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.
Induction and Recursion by: Mohsin tahir (GL) Numan-ul-haq Waqas akram Rao arslan Ali asghar.
Induction and recursion
Chapter 8 With Question/Answer Animations 1. Chapter Summary Applications of Recurrence Relations Solving Linear Recurrence Relations Homogeneous Recurrence.
Copyright © Cengage Learning. All rights reserved.
Applied Discrete Mathematics Week 9: Relations
Advanced Counting Techniques
Induction and recursion
Chapter 8. Section 8. 1 Section Summary Introduction Modeling with Recurrence Relations Fibonacci Numbers The Tower of Hanoi Counting Problems Algorithms.
Chapter 4: Induction and Recursion
Section 5.3. Section Summary Recursively Defined Functions Recursively Defined Sets and Structures Structural Induction.
Fall 2015 COMP 2300 Discrete Structures for Computation Donghyun (David) Kim Department of Mathematics and Physics North Carolina Central University 1.
CSC201 Analysis and Design of Algorithms Asst.Proof.Dr.Surasak Mungsing Oct-151 Lecture 2: Definition of algorithm and Mathematical.
CSE 2813 Discrete Structures Recurrence Relations Section 6.1.
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.
Recurrence Relation Models
Lecture 4,5 Mathematical Induction and Fibonacci Sequences.
CS 103 Discrete Structures Lecture 13 Induction and Recursion (1)
22C:19 Discrete Math Advanced Counting Fall 2010 Sukumar Ghosh.
CHAPTER TWO RECURRENCE RELATION
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.
Divide and Conquer Faculty Name: Ruhi Fatima Topics Covered Divide and Conquer Matrix multiplication Recurrence.
CompSci 102 Discrete Math for Computer Science March 13, 2012 Prof. Rodger Slides modified from Rosen.
Proofs, Recursion and Analysis of Algorithms Mathematical Structures for Computer Science Chapter 2 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesProofs,
Chapter 5 With Question/Answer Animations 1. Chapter Summary Mathematical Induction - Sec 5.1 Strong Induction and Well-Ordering - Sec 5.2 Lecture 18.
Mathematical Induction And Recursion Discrete Math Team KS MATEMATIKA DISKRIT (DISCRETE MATHEMATICS ) 1.
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design Advanced Algorithms Analysis and Design By Dr. Nazir Ahmad Zafar.
“Toy” Model: Fibonacci The Fibonacci sequence first appears in the book Liber Abaci (1202) by Leonardo of Pisa, known as Fibonacci.
Hubert Chan (Chapters 1.6, 1.7, 4.1)
Chapter 4: Induction and Recursion
CMSC Discrete Structures
Recursion CENG 707.
Introduction to Recurrence Relations
Induction and recursion
Induction and Recursion
Hubert Chan (Chapters 1.6, 1.7, 4.1)
Mathematical Induction Recursion
Chapter 5 Induction and Recursion
Module #17: Recurrence Relations
Proofs, Recursion and Analysis of Algorithms
Induction and recursion
Induction: One Step At A Time
Induction: One Step At A Time
Module #17: Recurrence Relations
CS 2210 Discrete Structures Advanced Counting
CMSC Discrete Structures
CMSC Discrete Structures
Mathematical Induction
Recurrence Relations Discrete Structures.
Mathematical Induction
Chapter 7 Advanced Counting Techniques
Recurrence Relations Rosen 5th ed., §6.2 5/22/2019
Copyright © Cengage Learning. All rights reserved.
Mathematical Induction II
Recursion: The Mirrors
Presentation transcript:

1A Recursively Defined Functions

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.

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 + 0.08 𝐴 𝑛−1 =1.08𝐴(𝑛−1) Thus A(n) can be defined recursively as follows: 𝐴 0 =1000 - initial condition 𝐴 𝑛 =1.08𝐴 𝑛−1 , 𝑛≥1 recurrence relation

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

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

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

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 𝑖𝑓 𝑛=1 2 𝑏 𝑛−1 +1 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒 x y z

For example, 𝑏 4 =2 𝑏 3 +1=2 2 𝑏 2 +1 +1 =4 𝑏 2 +2+1=4 2 𝑏 1 +1 +2+1 =8 𝑏 1 +4+2+1=8 1 +4+2+1 = 2 4 −1=15 so it takes 15 moves to transfer 4 disks from X to Z. https://www.mathsisfun.com/games/towerofhanoi.html

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.

𝑓 𝑛 = 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: 𝑓 𝑛 = 1 𝑖𝑓 𝑛=0 𝑓 𝑛−1 +𝑛 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒

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

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

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?

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.

𝐹 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

Mathematical Induction Examples

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

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

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 +𝑛=2 1 +3 1 +1=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

2 (𝑘+1) 3 +3 (𝑘+1) 2 + 𝑘+1 =2 𝑘 3 + 3𝑘 2 +3𝑘+1 +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.

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

เรามี ใช้ 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 6 + 𝑁+1 = 𝑁+1 2 𝑁 2 +7𝑁+6) 6 = (𝑁+1)(𝑁+2)(2𝑁+3) 6 = (𝑁+1)( 𝑁+1 +1)(2 𝑁+1 +1) 6

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

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.

พิสูจน์ 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.

จาก 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