Download Recursive Definitions - Discrete Mathematics - Lecture Slides and more Slides Discrete Mathematics in PDF only on Docsity!
1
Recursive Definitions and
Structural Induction
2
Recursion
• Recursion means defining something, such as a
function, in terms of itself
- For example, let f ( x ) = x!
- We can define f ( x ) as f ( x ) = x * f( x -1)
4
- – Find f (1), f (2), f (3), and f (4), where f (0) = Recursion example
- a) Let f ( n +1) = f ( n ) +
- • f (1) = f (0) + 2 = 1 + 2 =
- • f (2) = f (1) + 2 = 3 + 2 =
- • f (3) = f (2) + 2 = 5 + 2 =
- • f (4) = f (3) + 2 = 7 + 2 =
- • f (1) = 3 * f (0) = 3*1 = b) Let f ( n +1) = 3 f ( n )
- • f (2) = 3 * f (1) = 3*3 =
- • f (3) = 3 * f (2) = 3*9 =
- • f (4) = 3 * f (3) = 3*27 =
- – Find f (1), f (2), f (3), and f (4), where f (0) = Recursion example
- • f (1) = 2 f (0) = 2^1 = c) Let f ( n +1) = 2 f ( n )
- • f (2) = 2 f (1) = 2^2 =
- • f (3) = 2 f (2) = 2^4 =
- • f (4) = 2 f (3) = 2^16 =
- d) Let f ( n +1) = f ( n ) 2 + f ( n ) +
- • f (1) = f (0) 2 + f (0) + 1 = 1^2 + 1 + 1 =
- • f (2) = f (1) 2 + f (0) + 1 = 3^2 + 3 + 1 =
- • f (3) = f (2) 2 + f (0) + 1 = 13^2 + 13 + 1 =
- • f (4) = f (3) 2 + f (0) + 1 = 183^2 + 183 + 1 =
5
Fractals
• A fractal is a pattern that uses recursion
- The pattern itself repeats indefinitely
7
Fibonacci sequence in Java
long Fibonacci (int n) {
if ( (n == 1) || (n == 2) ) return 1; else return Fibonacci (n-1) + Fibonacci (n-2);
}
long Fibonacci2 (int n) {
return (long) ((Math.pow((1.0+Math.sqrt(5.0)),n)- Math.pow((1.0-Math.sqrt(5.0)),n)) / (Math.sqrt(5) * Math.pow(2,n)));
}
8
Recursion definition
• From “The Hacker’s Dictionary”:
• recursion n. See recursion. See also tail
recursion.
10
Defining sets via recursion
• Same two parts:
- Base case (or basis step)
- Recursive step
• Example: the set of positive integers
- Basis step: 1 ∈ S
- Recursive step: if x ∈ S , then x +1 ∈ S
11
Defining sets via recursion
• Give recursive definitions for:
a) The set of odd positive integers
1 ∈ S
If x ∈ S , then x +2 ∈ S
b) The set of positive integer powers of 3
3 ∈ S
If x ∈ S , then 3* x ∈ S
c) The set of polynomials with integer coefficients
0 ∈ S
If p ( x ) ∈ S , then p ( x ) + cxn^ ∈ S
13
Defining strings via recursion
• Let Σ = { 0, 1 }
• Thus, Σ* is the set of all binary numbers
- Or all binary strings
- Or all possible computer files
14
String length via recursion
• How to define string length recursively?
- Basis step: l (λ) = 0
- Recursive step: l ( wx ) = l ( w ) + 1 if w ∈ Σ* and x ∈ Σ
• Example: l (“aaa”)
- l (“aaa”) = l (“aa”) + 1
- l (“aa”) = l (“a”) + 1
- l (“a”) = l (“”) + 1
- l (“”) = 0
- Result: 3
16
Recursion pros
• Easy to program
• Easy to understand
17
Recursion cons
- Consider the recursive Fibonacci generator
- How many recursive calls does it make?
- F (1): 1
- F (2): 1
- F (3): 3
- F (4): 5
- F (5): 9
- F (10): 109
- F (20): 13,
- F (30): 1,664,
- F (40): 204,668,
- F (50): 25,172,538,
- F (100): 708,449,696,358,523,830,149 ≈ 7 * 10 20
- At 1 billion recursive calls per second (generous), this would take over 22,000 years
- But that would also take well over 10 12 Gb of memory!
19
Rooted trees
- Recursive definition:
- Basis step: A single vertex r is a rooted tree
- Recursive step:
- Let T 1 , T 2 , …, T (^) n be rooted trees
- Form a new tree with a new root r that contains an edge to the root of each of the trees T 1 , T 2 , …, T (^) n
20
(Extended) Binary trees
- Recursive definition
- Basis step: The empty set is an extended binary tree
- Recursive step:
- Let T 1 , and T 2 be extended binary trees
- Form a new tree with a new root r
- Form a new tree such that T 1 is the left subtree, and T 2 is the right subtree