Docsity

Recursion and Recursively Defined Sequences, Functions, and Sets, Slides of Discrete Structures and Graph Theory

Partial preview of the text

Download Recursion and Recursively Defined Sequences, Functions, and Sets and more Slides Discrete Structures and Graph Theory in PDF only on Docsity!

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.

Recursively Defined Functions

  • We can use the following method to define a function with the natural numbers as its domain:
  1. Specify the value of the function at zero.
  2. Give a rule for finding its value at any integer from its values at smaller integers.
  • Such a definition is called recursive or inductive definition.

Recursively Defined Functions

  • โ€ข f(0) = โ€ข Example:
  • โ€ข f(n + 1) = 2f(n) +
  • โ€ข f(0) =
  • โ€ข f(1) = 2f(0) + 3 = 2โ‹…3 + 3 =
  • โ€ข f(2) = 2f(1) + 3 = 2โ‹…9 + 3 =
  • โ€ข f(3) = 2f(2) + 3 = 2โ‹…21 + 3 =
  • โ€ข f(4) = 2f(3) + 3 = 2โ‹…45 + 3 =

Recursively Defined Functions

  • A famous example: The Fibonacci numbers
  • f(0) = 0, f(1) = 1
  • f(n) = f(n โ€“ 1) + f(n - 2)
  • f(0) = 0
  • f(1) = 1
  • f(2) = f(1) + f(0) = 1 + 0 = 1
  • f(3) = f(2) + f(1) = 1 + 1 = 2
  • f(4) = f(3) + f(2) = 2 + 1 = 3
  • f(5) = f(4) + f(3) = 3 + 2 = 5
  • f(6) = f(5) + f(4) = 5 + 3 = 8

Recursively Defined Sets

  • If we want to recursively define a set, we need to provide two things:
  • an initial set of elements,
  • rules for the construction of additional elements from elements in the set.
  • Example: Let S be recursively defined by:
  • 3 โˆˆ S
  • (x + y) โˆˆ S if (x โˆˆ S) and (y โˆˆ S)
  • S is the set of positive integers divisible by 3.

Recursively Defined Sets

  • Let P(n) be the statement โ€œ3n belongs to Sโ€.
  • Basis step: P(1) is true, because 3 is in S.
  • Inductive step: To show: 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 S, it follows from the recursive definition of S that 3n + 3 = 3(n + 1) is also in S.
  • Conclusion of Part I: A โІ S.

Recursively Defined Sets

  • Part II: To show: S โІ A.
  • Basis step: To show: All initial elements of S are in A. 3 is in A. True.
  • Inductive step: To show: (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 and 3 | y. From Theorem I, Section 2.3, it follows that 3 | (x + y).
  • Conclusion of Part II: S โІ A.
  • Overall conclusion: A = S.

Recursively Defined Sets

  • With this definition, we can construct formulae such as:
  • (x โ€“ y)
  • ((z / 3) โ€“ y)
  • ((z / 3) โ€“ (6 + 5))
  • ((z / (2 * 4)) โ€“ (6 + 5))

Recursive Algorithms

  • An algorithm is called recursive if it solves a problem by reducing it to an instance of the same problem with smaller input.
  • Example I: Recursive Euclidean Algorithm
  • procedure gcd(a, b: nonnegative integers with a < b)
  • if a = 0 then gcd(a, b) := b
  • else gcd(a, b) := gcd(b mod a, a)

Recursive Algorithms

  • Recursive Fibonacci Evaluation:

f(4)

f(3)

f(2)

f(1) (^) f(0)

f(1)

f(2)

f(1) f(0)

Recursive Algorithms

  • procedure iterative_fibo(n: nonnegative integer)
  • if n = 0 then y := 0
  • else
  • begin
  • x := 0
  • y := 1
  • for i := 1 to n-
  • begin
  • z := x + y
  • x : = y
  • y := z
  • end
  • end {y is the n-th Fibonacci number}