Week 12 - Recursion B
6.201 Recursive definitions
- Recursion
- When you must define a math object (set, func, sequence) in terms of object itself.
- Recursively defined functions
- Recursively defined function f with domain is defined by:
- BASIS STEP: specify an initial value of function.
- RECURSIVE STEP: rule for defining value of function when given integers, in terms of smaller integers.
- We call this definition: recursive or inductive.
- Defining a function from set to set is same as sequence: ... where ,
- Examples
- Give recursive definition of sequence in following cases:
- May be more than one correct answer to each sequence
-
- As each term in the sequence is greater than the previous term by 4, this sequence can be defined by setting and declaring that
-
- As each term is 4 times its predecessor, this sequence can be defined as and
-
- Sequence can be defined as and
-
- Give recursive definition of sequence in following cases:
- Recursively defined function f with domain is defined by:
- Recursively defined sets
- Like sequences, for some sets, defining recursively is easier than defining explicitly.
- Sets can also be defined recursively, by defining two steps:
- BASIS STEP: where we specify some initial elements.
- RECURSIVE STEP: provide a rule for constructing new elements from those we already have.
- Example
- Consider the subset of S of the set of integers recursively defined by:
-
- BASIS STEP: (4 is an element of S)
-
- RECURSIVE STEP: if and then
-
- We will be shown later how we can prove that set S is the set of all positive integers that are multiples of 4.
- Consider the subset of S of the set of integers recursively defined by:
- Recursive algorithms
- Algorithm
- A finite sequence of precise instructions for performing a computation or solving a problem.
- An algorithm is considered recursive if it solves a problem by reducing to an instance of the same problem with smaller input.
-
Example
- Let's give a recursive algorithm for computing where n is a nonnegative integer:
- n! can be recursively defined by the following 2 steps:
- BASIS STEP:
- RECURSIVE STEP: where is a positive integer.
- The pseudocode of the algo can be formalised as:
- n! can be recursively defined by the following 2 steps:
procedure factorial(n: nonnegative integer) { if n = 0 then return 1 else return n factorial (n - 1) }
- Let's give a recursive algorithm for computing where n is a nonnegative integer:
- Algorithm
Lesson 6.204 Recurrence relations
- Recurrence Relation
- An equation that defines a sequence based on a rule which gives the next term as a function of the previous term.
- Infinite Sequence
- Function from the set of positive integers to set of real numbers.
- It can be useful to formalise the problem as sequence before solving it.
- Example: [[Towers of Hanoi]]
- ![[week-12-hanoi-tower.png]]
- Want to get discs from spoke A to C.
- Can only move one disk at a time.
- You cannot place a larger disc on a smaller one.
- It is an example of a recursive function.
- Number of moves:
- Let be min number of moves to transfer n from one spoke to another.
- To move n discs from A to C, we must move n-1 discs from A to B by moves.
- Then, move last (and largest) disc from A to C by 1 move.
- Then, remove the n-1 discs again from B to C by moves.
- Thus, total moves is
- Linear Recurrence
- In which each term of a sequence is a linear function of earlier terms in the sequence.
- Two types of linear recurrence:
- Linear homogeneous recurrence:
- formalised as
- where , and k is the degree of relation.
- Linear non-homogeneous recurrences:
- formalised as
- where , and f(n) is a function.
- Depending only on n, and k is the degree of relation.
- Linear homogeneous recurrence:
- Example: first order recurrence
- Consider:
- a country has 50 million people that:
- has population growth rate (birth rate - death rate) of 1% per year.
- receives 50,000 immigrants per year.
- a country has 50 million people that:
- Question: find this country's population in 10 years.
- This case can be modelled as the following first-order linear recurrence:
- where is the population in n years from now.
- , is expressed as
- .
- Consider:
- Example: 2nd order recurrence
- Consider:
- 0, 1, 1 2, 3, 5, 8, 13, 21, 34, ...
- where each number is found by adding up the two numbers before it.
- We can model the sequence as 2nd-order linear recurrence:
- This is called Fibonacci sequence.
- Consider:
- Arithmetic sequences
- We consider a sequence arithmetic if distance between consecutive terms is a constant c
- , is expressed as and
- Example:
- Sequence 2, 5, 8, 11, 14, ... is arithmetic with initial term of and a common difference of 3.
- 30, 25, 20, 15, ... is arithmetic with initial term of and common difference of -5.
- Geometric sequences
- Sequence is geometric if ratio between consecutive terms is a constant r.
- , is expressed as and
- Example:
- Sequence 3, 6, 12, 24, 48, ... is geometric with an initial term of and a common ratio of 2.
- 125, 25, 5, 1, 1/5, 1/25, ... is geometric with initial term of and common ratio of 1/5.
- Example:
- Divide and conquer recurrence
- Divide and conquer algorithm consists of three steps:
- dividing problem into smaller subproblems.
- solving (recursively) each subproblem.
- combining solutions to find a solution to original problem.
- Example:
- Consider the problem of finding minimum of a sequence where
- if n=1, the number is itself min or max.
- if n > 1, divide the numbers into 2 lists.
- order the sequence.
- find min and max in first list.
- find min and max in second.
- infer the min and max of entire list.
- Consider the problem of finding minimum of a sequence where
- Divide and conquer algorithm consists of three steps:
Lesson 6.206 Solving recurrence relations
- Solving linear recurrence
- Let be a linear homogeneous recurrence.
- If a combination of the geometric sequence is a solution to this recurrence, it satifies
- By dividing both sides by , we get:
- We call this equation the Characteristic Equation.
- Solving this equation is first step towards finding a solution to linear homogeneous recurrence:
- If is a solution equation with multiplicity p, then the combination satisifes the recurrence.
- Solving this equation is first step towards finding a solution to linear homogeneous recurrence:
- Solving Fibonacci recurrence
- Let's consider solving the Fibonacci recurrence relation: , with and
- Solution:
- The characteristic equation of the Fibonacci recurrence relation is:
- It has 2 distinct roots, of multiplicity 1:
- and
- So, is a solution.
- To find and we need to use the initial conditions.
- From:
- By solving the 2 equations, we can find and
- From:
- The solution is then formalise as:
- Solution:
- Let's consider solving the Fibonacci recurrence relation: , with and
- Example 2
- Consider another sequence
- Solution:
- The characteristic equation of this relation is:
- It has a distinct root, whose multiplicity is 3,
- So, is a solution
- To find and we need to use initial conditions.
- From:
- We can find
- From:
- The solution is then formalised as:
- Consider another sequence
- Using strong induction to solve recurrence
- It is sometimes easier to solve recurrence relation solutions using strong induction.
- Example:
- Prove:
- P(n): the sequence f_n = 1/\sqrt{5}(r_1^{n} -r_2^{n}) verifies the Fibonacci recurrence, where:
- are the roots of $
- First, verify for P(2):
- because
- which verifies the initial condition.
- Let , where P(2), P(3) ... P(k) are all true.
- Let's verify :
- which equals
- We can conclude that is true and the strong induction is verified.
- P(n): the sequence f_n = 1/\sqrt{5}(r_1^{n} -r_2^{n}) verifies the Fibonacci recurrence, where:
- Prove:
Assignment 6.209 - Induction and Recursion
Use mathematical induction to prove that
Answer
- BASIS STEP
The basis step is true because:
- INDUCTION STEP
Let be an arbitrary element, where is true.
Verify
Which means that is true and the induction step is verified.
Problem Sheet
Question 1
Prove that the sum of any even integers is even. In an other way show that:
, if and are even numbers then is also an even number.
Proof
Let and assume that and are even. We need to show that n + m is also even. n and m are two even integers, it follows by definition of even numbers that there exists 2 integers and such that and . Thus, . Hence, there exists an integer such that , it follows by definition of even numbers that n + m is even.
Question 2
Use direct proof to show that: , if is an even number and m is an odd number, then 3n + 2m is also an even number.
Proof
Let and assume that is an even number and is an odd number. We need to show that 3n + 2m is also an even number.
Assume that is even and is odd, this implies that there exists 2 integers such that: and .
Thus, 3n + 2m = 3(2i) + 2(2j + 1) = 2(3i) + 2(2j + 1) = 2(3i + 2j + 1)
Hence, there exists an integer k = (3i + 2j + 1) such that 3n + 2m = 2k.
By the definition of even numbers, we can see 3n + 2m number is even.
Question 3
Prove that the sum of any 2 odd integers is odd. In an other way show that: if and are odd numbers then is an even number.
Proof
Let and assume that is odd and is odd. We need to show that is even.
and are odd, which follows by definition of odd numbers that there exists 2 integers and such that and
Thus,
Hence, there is an integer such that , which proves is even by the definition of even numbers.
Question 4
Show that for any odd number integer , is also odd.
In another way show that: if is odd then is odd.
Proof
Let and assume that is an odd number. We need to show that is also odd, which means show there's an integer such that .
By definition of odd, is odd which means there exists an integer such that .
It follows that
is an integer as it's the sum of products of integers.
Therefore, there exists such that .
It follows by definition of odd numbers that is an odd number.