Docsity

Recursion - Discrete Math - Lecture Slides, Slides of Discrete Mathematics

Partial preview of the text

Download Recursion - Discrete Math - Lecture Slides and more Slides Discrete Mathematics in PDF only on Docsity!

Discrete Maths

  • Objective
    • to show the close connection between recursive definitions, recursive functions, and recursive (inductive) proofs

3. Recursion

Docsity.com

Overview

  1. Recursive Definitions

  2. Recursive Functions

  3. Lists Recursively

  4. Trees Recursively

  5. Hilbert Curves

  6. Proving Recursive Programs Work

  7. Further Information

Docsity.com

Examples

  • A tree is made from left and right subtrees.

23

7 19 5

5 4 left subtree

right subtree

the leaves are the basis

Docsity.com

Sierpinski Gasket

  • Start with a triangle and cut out the middle piece as shown. This results in three smaller triangles to which the process is continued.

Use recursion to create strange shapes

Docsity.com

Menger Sponge

remove center square

repeat for the 8 small squares

Docsity.com

Why Bother with Recursive Definitions?

  • For many problems, recursive definitions are the natural way of specifying the problems - e.g. search over trees/graphs, parsing,
  • Recursive definitions are very close to

inductive statements, and so are usually easier to prove than loop invariants.

Docsity.com

Prove the Specification Correct

  • Prove the inductive statement S(n):
    • the recursive definition of n!, as defined on the last slide, equals 1 23...n
  • Basis. S(1) is clearly true.

continued

Show recursive n! is the same as a series of multiplications.

Docsity.com

  • Induction. Assume that S(n) is true, which means that n! = 123...*n
  • The recursive definition states that:
    • (n+1)! = (n+1) * n!
    • so, (n+1)! = n! * (n+1)
    • substitute in n! value from S(n), so (n+1)! = 123...n(n+1)
  • This is S(n+1), so S(n) --> S(n+1) is true.

continued Docsity.com

1 .2. Recursive Definition of Expressions

  • We will look at expressions involving binary operators (e.g. +, , /) - e.g X2, (Y/3)*(W+2), X
  • The variables/numbers in an expressions are called operands.
  • Basis. An operand on its own is an expression (e.g. X).

continued Docsity.com

Inductive Rules

    1. If E 1 and E 2 are expression, and @ is a binary

operator (e.g., +, *), then E 1 @E 2 is an expression.

    1. If E is an expression, then (E) is an

expression.

  • Examples: 5, X, 2+s, (X*Y) - 1

Docsity.com

  • Examples:
    • S(1) 1 y X a 2
    • S(3) 2+a X*Y 3-s (x)
    • S(5) (1+d) 5*6-
  • Note : the examples suggest that S(2), S(4), S(6), etc. may not exist.

the length of the expression

continued Docsity.com

  • The proof of S(n) is by complete induction on the length of the expression: - length is counted as the number of operators, operands and parentheses
  • Basis. n=1. E must be a single operand (e.g. X). Since there are no operators, the basis holds. - e.g. the first examples on the last slide

continued Docsity.com

  • a) If by rule (2), E = (E 1 )
  • Assume true: no of operands no of ops| E 1 x+1 x
  • Prove E E = (E 1 ) x+1 x
  • So S(n+1) holds when E has the form (E 1 )

continued Docsity.com

  • b) If by rule (1), then E = E 1 @E 2
  • Assume true: no of operands no of ops| E 1 a+1 a E 2 b+1 b
  • Prove E E = E 1 @E 2 a+b+2 a+b +
  • So S(n+1) holds when E has the form E 1 @E 2

continued Docsity.com