Skip to content

Recursively Defined Functions

  • We use two steps to define a function with the set of nonnegative integers as its domain:

INFO

BASIS STEP: Specify the value of the function at zero.

RECURSIVE STEP: Give a rule for finding its value at an integer from its values at smaller integers.

  • Note that,

INFO

Recursive function on integers = sequence defined by recurrence

  • Example, f is defined recursively by
    • BASIS STEP f(0)=3
    • RECURSIVE STEP f(n+1)=2f(n)+3

Recursively Defined Sets and Structures

  • Recursive definitions play an important role in the study of strings
  • Example, consider the subset S of the set of integers recursively defined by
    • BASIS STEP 3S
    • RECURSIVE STEP If xS and yS, then x+yS

Recursive Definition of Strings

INFO

Let Σ be an alphabet. The set Σ of all strings over Σ is defined recursively as follows:

BASIS STEP: λΣ (where λ is the empty string.)

RECURSIVE STEP: If wΣ and xΣ, then wxΣ

Recursive Definition of Concatenation

Let Σ be an alphabet and Σ the set of all strings over Σ.
The concatenation of two strings is denoted by and defined recursively as follows:

INFO

BASIS STEP:

If wΣ, then wλ=w,

where λ is the empty string.

RECURSIVE STEP:

If w1Σ, w2Σ, and xΣ, then w1(w2x)=(w1w2)x.

Intuition

  • Nối chuỗi với chuỗi rỗng λ không làm thay đổi chuỗi
  • Để nối w1 với w2x:
    • trước tiên nối w1 với w2
    • sau đó thêm ký tự x vào cuối

Recursive Definition of Rooted Trees

  • A rooted tree is defined recursively as follows:

INFO

BASIS STEP:

A single vertex r is a rooted tree.

RECURSIVE STEP: Suppose that T1,T2,,Tn are disjoint rooted trees with roots r1,r2,,rn, respectively.

Form a new graph by:

  • introducing a new vertex r (not in any Ti), and
  • adding edges from r to each ri for i=1,,n

Then this graph is also a rooted tree.

Exclusion Rule

No graph is a rooted tree unless it can be formed using the basis step and recursive step.

Recursive Definition of Extended binary trees

INFO

BASIS STEP:

 is an extended binary tree.

RECURSIVE STEP:

If T1 and T2 are disjoint extended binary trees, then T1T2is an extended binary tree formed by introducing a root r and connecting itto the roots of T1 (left subtree) and T2 (right subtree), when these are nonempty.

Recursive Definition of Full Binary Trees

INFO

BASIS STEP:

There is a full binary tree consisting of a single vertex r.

RECURSIVE STEP:

If T1 and T2 are disjoint full binary trees, then T1T2is a full binary tree formed by introducing a root r and connecting itto the roots of T1 (left subtree) and T2 (right subtree).

Recursive Algorithms

Definition

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

  • Some examples of recursive algorithms:
    • Fibonacci numbers
    • Power an
    • Factorials n!
    • gcd(a,b)
    • Binary search
  • Proving recursive algorithms correctly requires a strong induction approach.

Structural Induction

  • Structural induction can be used to prove that all members of a set constructed recursively have a particular property.

Proof template on Strings (Σ*)

INFO

BASIS STEP: Show that P(λ) is true.

RECURSIVE STEP: Assume P(w) is true. Show that for any xΣ, P(wx) is also true.

  • Build string by adding characters at the end
  • If property holds for w → must hold for wx

Proof template on Full Binary Trees

TIP

BASIS STEP: Show that P(T) is true for a single-node tree.

RECURSIVE STEP: Assume P(T1) and P(T2) are true. Show that P(T1T2) is also true.

  • Tree is built by combining smaller trees
  • If property holds for subtrees → must hold for the combined tree

Example on Well-Formed Formulae

Goal

Prove that every well-formed formula (WFF) has an equal number of left and right parentheses.

Basis Step

  • T, F, and propositional variables (s) contain no parentheses
  • ⇒ number of left = number of right = 0 ✔️

Recursive Step

Induction hypothesis:

  • p and q are WFF
  • p has equal left/right parentheses
  • q has equal left/right parentheses

Check new constructions:

  1. (¬p)
  • left: lp + 1
  • right: rp + 1
  • ⇒ still equal ✔️
  1. (p ∨ q), (p ∧ q), (p → q), (p ↔ q)
  • left: lp + lq + 1
  • right: rp + rq + 1
  • since lp = rp and lq = rq
  • ⇒ still equal ✔️

Conclusion

  • Property holds for basis
  • Preserved under recursive construction
    ⇒ True for all WFF (by structural induction)

Comparison of Induction Methods

TypeCore IdeaWhen to UseBasisInduction Step"Build" Pattern
Mathematical InductionProgress step-by-step over integersProblems on ℕ (numbers)Prove P(0) or P(1)Assume P(n) → prove P(n+1)n → n+1
Strong InductionUse all previous casesWhen current case depends on multiple earlier onesProve initial cases (P(0), P(1), ...)Assume P(0)...P(n) → prove P(n+1)depends on entire history
Structural InductionFollow how objects are constructedRecursively defined structures (strings, trees, sets)Prove for simplest objectAssume true for components → prove for constructed objectbuild from smaller parts

Built with ❤️ and curiosity