If I told you once, it must be...

18
07/04/22 1 If I told you once, it If I told you once, it must be... must be... Recursion Recursion

description

If I told you once, it must be. Recursion. Recursive Definitions. Recursion is a principle closely related to mathematical induction. In a recursive definition , an object is defined in terms of itself. We can recursively define sequences , functions and sets. - PowerPoint PPT Presentation

Transcript of If I told you once, it must be...

Page 1: If I told you once, it must be...

04/21/23 1

If I told you once, it must If I told you once, it must be...be...

RecursionRecursion

Page 2: If I told you once, it must be...

04/21/23 2

Recursive DefinitionsRecursive Definitions

RecursionRecursion is a principle closely related to is a principle closely related to mathematical induction.mathematical induction.

In a In a recursive definitionrecursive definition, an object is , an object is defined in terms of itself.defined in terms of itself.

We can recursively define We can recursively define sequencessequences, , functionsfunctions and and setssets..

Page 3: If I told you once, it must be...

04/21/23 3

Recursively Defined Recursively Defined SequencesSequences

Example:Example:

The sequence {aThe sequence {ann} of powers of 2 is given by} of powers of 2 is given byaann = 2 = 2nn for n = 0, 1, 2, … . for n = 0, 1, 2, … .

The same sequence can also be defined The same sequence can also be defined recursivelyrecursively::

aa00 = 1 = 1

aan+1n+1 = 2a = 2an n for n = 0, 1, 2, …for n = 0, 1, 2, …

Obviously, induction and recursion are similar Obviously, induction and recursion are similar principles.principles.

Page 4: If I told you once, it must be...

04/21/23 4

Recursively Defined Recursively Defined FunctionsFunctions

We can use the following method to define a We can use the following method to define a function with the function with the natural numbersnatural numbers as its as its domain:domain:

1.1. Specify the value of the function at zero.Specify the value of the function at zero.

2.2. Give a rule for finding its value at any integer Give a rule for finding its value at any integer from its values at smaller integers. from its values at smaller integers.

Such a definition is called Such a definition is called recursiverecursive or or inductive definitioninductive definition. .

Page 5: If I told you once, it must be...

04/21/23 5

Recursively Defined Recursively Defined FunctionsFunctions

Example:Example:

f(0) = 3f(0) = 3

f(n + 1) = 2f(n) + 3f(n + 1) = 2f(n) + 3

f(0) = 3f(0) = 3

f(1) = 2f(0) + 3 = 2f(1) = 2f(0) + 3 = 23 + 3 = 93 + 3 = 9

f(2) = 2f(1) + 3 = 2f(2) = 2f(1) + 3 = 29 + 3 = 219 + 3 = 21

f(3) = 2f(2) + 3 = 2f(3) = 2f(2) + 3 = 221 + 3 = 4521 + 3 = 45

f(4) = 2f(3) + 3 = 2f(4) = 2f(3) + 3 = 245 + 3 = 9345 + 3 = 93

Page 6: If I told you once, it must be...

04/21/23 6

Recursively Defined Recursively Defined FunctionsFunctions

How can we recursively define the How can we recursively define the factorial function f(n) = n! ? factorial function f(n) = n! ?

f(0) = 1f(0) = 1f(n + 1) = (n + 1)f(n) f(n + 1) = (n + 1)f(n)

f(0) = 1f(0) = 1f(1) = 1f(0) = 1f(1) = 1f(0) = 11 = 11 = 1f(2) = 2f(1) = 2f(2) = 2f(1) = 21 = 21 = 2f(3) = 3f(2) = 3f(3) = 3f(2) = 32 = 62 = 6f(4) = 4f(3) = 4f(4) = 4f(3) = 46 = 246 = 24

Page 7: If I told you once, it must be...

04/21/23 7

Recursively Defined Recursively Defined FunctionsFunctions

A famous example: The Fibonacci A famous example: The Fibonacci numbers numbers

f(0) = 0, f(1) = 1f(0) = 0, f(1) = 1f(n) = f(n – 1) + f(n - 2) f(n) = f(n – 1) + f(n - 2)

f(0) = 0f(0) = 0f(1) = 1f(1) = 1f(2) = f(1) + f(0) = 1 + 0 = 1f(2) = f(1) + f(0) = 1 + 0 = 1f(3) = f(2) + f(1) = 1 + 1 = 2f(3) = f(2) + f(1) = 1 + 1 = 2f(4) = f(3) + f(2) = 2 + 1 = 3f(4) = f(3) + f(2) = 2 + 1 = 3f(5) = f(4) + f(3) = 3 + 2 = 5f(5) = f(4) + f(3) = 3 + 2 = 5f(6) = f(5) + f(4) = 5 + 3 = 8f(6) = f(5) + f(4) = 5 + 3 = 8

Page 8: If I told you once, it must be...

04/21/23 8

Recursively Defined SetsRecursively Defined Sets

If we want to recursively define a set, we If we want to recursively define a set, we need to provide two things:need to provide two things:

• an an initial setinitial set of elements, of elements,• rulesrules for the construction of for the construction of additionaladditional elements from elements in the set. elements from elements in the set.

Example:Example: Let S be recursively defined by: Let S be recursively defined by:

3 3 S S

(x + y) (x + y) S if (x S if (x S) and (y S) and (y S) S)

S is the set of positive integers divisible by 3.S is the set of positive integers divisible by 3.

Page 9: If I told you once, it must be...

04/21/23 9

Recursively Defined SetsRecursively Defined Sets

Proof:Proof:

Let A be the set of all positive integers divisible Let A be the set of all positive integers divisible by 3.by 3.

To show that A = S, we must show that To show that A = S, we must show that A A S and S S and S A. A.

Part I:Part I: To prove that A To prove that A S, we must show that S, we must show that every positive integer divisible by 3 is in S.every positive integer divisible by 3 is in S.

We will use mathematical induction to show We will use mathematical induction to show this. this.

Page 10: If I told you once, it must be...

04/21/23 10

Recursively Defined SetsRecursively Defined Sets

Let P(n) be the statement “3n belongs to S”.Let P(n) be the statement “3n belongs to S”.

Basis step:Basis step: P(1) is true, because 3 is in S. P(1) is true, because 3 is in S.

Inductive step:Inductive step: To show: To show:If P(n) is true, then P(n + 1) is true.If P(n) is true, then P(n + 1) is true.

Assume 3n is in S. Since 3n is in S and 3 is in Assume 3n is in S. Since 3n is in S and 3 is in S, it follows from the recursive definition of S S, it follows from the recursive definition of S thatthat3n + 3 = 3(n + 1) is also in S.3n + 3 = 3(n + 1) is also in S.

Conclusion of Part I:Conclusion of Part I: A A S. S.

Page 11: If I told you once, it must be...

04/21/23 11

Recursively Defined SetsRecursively Defined SetsPart II:Part II: To show: S To show: S A. A.

Basis step:Basis step: To show: To show: All initial elements of S are in A. 3 is in A. All initial elements of S are in A. 3 is in A. True.True.

Inductive step:Inductive step: To show: To show:(x + y) is in A whenever x and y are in S.(x + y) is in A whenever x and y are in S.

If x and y are both in A, it follows that 3 | x If x and y are both in A, it follows that 3 | x and 3 | y. From Theorem I, Section 2.3, it and 3 | y. From Theorem I, Section 2.3, it follows that 3 | (x + y).follows that 3 | (x + y).

Conclusion of Part II:Conclusion of Part II: S S A. A.Overall conclusion:Overall conclusion: A = S. A = S.

Page 12: If I told you once, it must be...

04/21/23 12

Recursively Defined SetsRecursively Defined Sets

Another example:Another example:

The well-formed formulae of variables, The well-formed formulae of variables, numerals and operators from {+, -, *, /, ^} numerals and operators from {+, -, *, /, ^} are defined by:are defined by:

x is a well-formed formula if x is a numeral or x is a well-formed formula if x is a numeral or variable.variable.

(f + g), (f – g), (f * g), (f / g), (f ^ g) are well-(f + g), (f – g), (f * g), (f / g), (f ^ g) are well-formed formulae if f and g are.formed formulae if f and g are.

Page 13: If I told you once, it must be...

04/21/23 13

Recursively Defined SetsRecursively Defined Sets

With this definition, we can construct With this definition, we can construct formulae such as:formulae such as:

(x – y)(x – y)

((z / 3) – y)((z / 3) – y)

((z / 3) – (6 + 5))((z / 3) – (6 + 5))

((z / (2 * 4)) – (6 + 5))((z / (2 * 4)) – (6 + 5))

Page 14: If I told you once, it must be...

04/21/23 14

Recursive AlgorithmsRecursive Algorithms

An algorithm is called An algorithm is called recursiverecursive if it solves a if it solves a problem by reducing it to an instance of the problem by reducing it to an instance of the same problem with smaller input.same problem with smaller input.

Example I:Example I: Recursive Euclidean Algorithm Recursive Euclidean Algorithm

procedureprocedure gcd(a, b: nonnegative integers with gcd(a, b: nonnegative integers with a < b)a < b)

ifif a = 0 a = 0 then then gcd(a, b) := bgcd(a, b) := b

elseelse gcd(a, b) := gcd(b gcd(a, b) := gcd(b modmod a, a) a, a)

Page 15: If I told you once, it must be...

04/21/23 15

Recursive AlgorithmsRecursive Algorithms

Example II:Example II: Recursive Fibonacci Algorithm Recursive Fibonacci Algorithm

procedureprocedure fibo(n: nonnegative integer) fibo(n: nonnegative integer)

ifif n = 0 n = 0 then then fibo(0) := 0fibo(0) := 0

else ifelse if n = 1 n = 1 then then fibo(1) := 1fibo(1) := 1

elseelse fibo(n) := fibo(n – 1) + fibo(n – 2) fibo(n) := fibo(n – 1) + fibo(n – 2)

Page 16: If I told you once, it must be...

04/21/23 16

Recursive AlgorithmsRecursive Algorithms

Recursive Fibonacci Evaluation:Recursive Fibonacci Evaluation:

f(4)f(4)

f(3)f(3)

f(2)f(2)

f(1)f(1) f(0)f(0)

f(1)f(1)

f(2)f(2)

f(1)f(1) f(0)f(0)

Page 17: If I told you once, it must be...

04/21/23 17

Recursive AlgorithmsRecursive Algorithmsprocedureprocedure iterative_fibo(n: nonnegative integer) iterative_fibo(n: nonnegative integer)ifif n = 0 n = 0 then then y := 0y := 0elseelsebeginbegin

x := 0x := 0y := 1y := 1for for i := 1 i := 1 toto n-1 n-1beginbegin

z := x + yz := x + yx : = yx : = yy := zy := z

endendendend {y is the n-th Fibonacci number}{y is the n-th Fibonacci number}

Page 18: If I told you once, it must be...

04/21/23 18

Recursive AlgorithmsRecursive Algorithms

For every recursive algorithm, there is an For every recursive algorithm, there is an equivalent iterative algorithm.equivalent iterative algorithm.

Recursive algorithms are often shorter, more Recursive algorithms are often shorter, more elegant, and easier to understand than their elegant, and easier to understand than their iterative counterparts.iterative counterparts.

However, iterative algorithms are usually However, iterative algorithms are usually more efficient in their use of space and time. more efficient in their use of space and time.