Ch5discrete Maths
Ch5discrete Maths
5.1 Introduction
A recurrence relation is an infinite sequence a1, a2, a3,, an, in which the formula for the nth term an depends on one or more preceding terms, with a finite set of start-up values or initial conditions
Example 1:
Initial condition a0 = 1 Recursive formula: a n = 1 + 2a n-1 for n > 2 First few terms are: 1, 3, 7, 15, 31, 63,
Example 2:
Initial conditions a0 = 1, a1 = 2 Recursive formula: a n = 3(a n-1 + a n-2) for n > 2 First few terms are: 1, 2, 9, 33, 126, 477, 1809, 6858, 26001,
Fibonacci sequence
Initial conditions:
f1
= 1, f2 = 2
Recursive formula:
1
1
2
2
3
3
4
5
5
8
6
13
7
21
8
34
9
55
10
89
11
144
Compound interest
Given P = initial amount (principal) n = number of years r = annual interest rate A = amount of money at the end of n years At the end of: 1 year: A = P + rP = P(1+r) 2 years: A = P + rP(1+r) = P(1+r)2 3 years: A = P + rP(1+r)2 = P(1+r)3
Eugene Catalan
Catalan numbers are generated by the formula: Cn = C(2n,n) / (n+1) for n > 0 The first few Catalan numbers are:
n 0 1 2 3 4 5 6 7 8 9 10 11
Cn
14
42
132
429
1430
4862
16796
58786
The number of ways in which a polygon with n+2 sides can be cut into n triangles The number of ways in which parentheses can be placed in a sequence of numbers, to be multiplied two at a time The number of rooted trivalent trees with n+1 nodes The number of paths of length 2n through an n by n grid that do not rise above the main diagonal The number of nonisomorphic binary trees with n vertices
Towers of Hanoi
Start with three pegs numbered 1, 2 and 3 mounted on a board, n disks of different sizes with holes in their centers, placed in order of increasing size from top to bottom. Object of the game: find the minimum number of moves needed to have all n disks stacked in the same order in peg number 3.
Start with all disks stacked in peg 1 with the smallest at the top and the largest at the bottom Use peg number 2 for intermediate steps Only a disk of smaller diameter can be placed on top of another disk
A problem in Economics
Demand equation: p = a - bq Supply equation: p = kq There is a time lag as supply reacts to changes in demand Use discrete time intervals as n = 0, 1, 2, 3, Given the time delayed equations
Ackermanns function
Initial conditions: A(0,n) = n + 1, for n = 0, 1, 2, 3, Recurrence relations: A(m,0) = A(m 1, 1), for m = 1, 2, 3, A(m,n) = A(m -1, A(m, n -1)) for m = 1, 2, 3, and n = 1, 2, 3,
Method 1: Iteration
Problem: Given a recursive expression with initial conditions a0, a1 try to express an without dependence on previous terms.
Solution: an = 2n
for n = 0, 1, 2, 3,
Binary search
2. Problem: Search for a value in an increasing sequence. Return the index of the value, or 0 if not found. Initial condition a1 = 2 Recurrence relation an = 1 + an/2 Result: an = (lg n)
Theorem 5.3.7: To merge two sequences the sum of whose lengths is n, the number of comparisons required is n-1.
Merge sort
4. A recursive algorithm is used to sort a sequence into increasing order using the algorithm for merging two increasing sequences into one increasing sequence (merge sort).