51
Discrete Mathematics Recursion and Sequences

Discrete Mathematics Recursion and Sequences. Recursion ^ Recursion is a powerful computer programming technique in which a procedure (e.g. a subroutine)

  • View
    245

  • Download
    1

Embed Size (px)

Citation preview

Discrete Mathematics

Recursion and Sequences

Recursion Recursion is a powerful computer programming

technique in which a procedure (e.g. a subroutine) is defined so that it calls itself.

Note that recursion is not available in all computer languages

To introduce recursion, look at sequences of numbers.

DefinitionsSequence: is just an infinite list of numbers in a

particular orderLike a set, but:

Elements can be duplicated Elements are ordered

A sequence is a function from a subset of Z to a set S

an is a term in the sequence{an} means the entire sequence

The same notation as sets!

Sequence examples an = 3n

The terms in the sequence are a1, a2, a3, …

The sequence {an} is { 3, 6, 9, 12, … }

Arithmetic Progression a, a+d, a+2d, …, a+nd, … an = a + (n-1) d

bn = 2n

The terms in the sequence are b1, b2, b3, …

The sequence {bn} is { 2, 4, 8, 16, 32, … }

Geometric Progression a, ar, ar2, ar3, …, arn-1, … an = arn-1

Determining the sequence formula Given values in a sequence, how do you determine

the formula?

Steps to consider: Is it an arithmetic progression (each term a constant

amount from the last)? Is it a geometric progression (each term a factor of the

previous term)? Does the sequence it repeat (or cycle)? Does the sequence combine previous terms? Are there runs of the same value?

Determining the sequence formula 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, …

The sequence alternates 1’s and 0’s, increasing the number of 1’s and 0’s each time

1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8, … This sequence increases by one, but repeats all even

numbers once 1, 0, 2, 0, 4, 0, 8, 0, 16, 0, …

The non-0 numbers are a geometric sequence (2n) interspersed with zeros

3, 6, 12, 24, 48, 96, 192, … Each term is twice the previous: geometric

progression an = 3*2n-1

Non-Recursive Definition of a Sequence 15, 8, 1, -6, -13, -20, -27, …

Each term is 7 less than the previous term an = 22 - 7n

3, 5, 8, 12, 17, 23, 30, 38, 47, … The difference between successive terms increases by one each

time a1 = 3, an = an-1 + n an = n(n+1)/2 + 2

2, 16, 54, 128, 250, 432, 686, … Each term is twice the cube of n an = 2*n3

2, 3, 7, 25, 121, 721, 5041, 40321 Each successive term is about n times the previous an = n! + 1

When a sequence is defined by a rule (may be arithmetic or geometric progression) in this way, we say the definition is non-recursive.

What is an algorithm?An algorithm is “a finite set of precise

instructions for performing a computation or for solving a problem”A program is one type of algorithm

Directions to somebody’s house is an algorithmA recipe for cooking a cake is an algorithmThe steps to compute the cosine of 90° is an

algorithm

Some algorithms are harder than others

Some algorithms are easyFinding the largest (or smallest) value in a listFinding a specific value in a list

Some algorithms are a bit harderSorting a list

Some algorithms are very hardFinding the shortest path between Edirne and

Diyarbakır

Some algorithms are essentially impossibleFactoring large composite numbers

Algorithms to Generate Sequences A non-recursive definition can be used in algorithm to obtain the first m

terms of a sequence. The algorithm that generates the first m terms of the sequence

15, 8, 1, -6, -13, -20, -27, …

1. Input m2. For n = 1 to m do 2.1 t:=22-7n 2.2 output t

This algorithm uses the non-recursive definition an = 22 - 7n

Note that the algortihm is written in pseudocode, a form of structured English which can be easily converted to code in the chosen programming language.

Algorithms to Generate Sequences A different algorithm to generate first m terms of

15, 8, 1, -6, -13, -20, -27, …1. Input m2. t:=153. Output t4. For n = 2 to m do 4.1. t:=t – 7 4.2. Output t

A trace of this algorithm for m = 5 shows that it does, indeed, output the first 5 terms of the sequence.

Recursive definition of this sequence is: a1 = 15 (base)

an = an-1 – 7 (n > 1)

It is recursive, because the formula an contains the previous term an, so the formula calls itself.

More Examples Find the recursive and non-recursive definitions for the sequence

1, 3, 7, 15, 31, 63, 127, … The non-recursive formula is more difficult to find than the recursive formula, but

it is easier to use once it has been found – this is fairly typical.

Non-recursive formula: an = 2 n - 1 Recursive formula a1 = 1, an = 2an-1 + 1 (n > 1) This algorithm produces the above sequence. It is not a recursive algorithm.

Instead it is an iterative algorithm.1. Input m2. t:=13. Output t4. For n = 1 to m do 4.1 t:= 2t + 1 4.2 Output t

The word “iterative” means to do something repeatedly, as in a For-do loop

Fibonacci Sequence This sequence was posed

by the Italian mathematician Leonardo Pisano in 1202.

A man put a pair of newborn rabbits in a place surrounded on all sides by a wall. How many pairs of rabbits will be present at later times if it is supposed that every month each pair begets a new pair which from the second month on becomes productive?

The resulting sequence is

1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, …

Time No of non-prod. pairs

No of Prod. pairs

Total No of Pairs

Initial 1 0 1

1 month 0 1 1

2 month 1 1 2

3 month 1 2 3

4 month 2 3 5

5 month 3 5 8

6 month 5 8 13

Fibonacci Sequence This is known as the Fibonacci sequence which was

the second name for Leonardo. The recursive definition of this sequence is

a1 = 1, a2 = 1,

an = an-1 + an-2 (n>2) It’s not straight forward to obtain a non-recursive

definition of the Fibonacci sequence In fact, the formula is

nn

na 2

51

2

51

5

1

The Collatz Sequence Let a1 = any chosen positive integer, and for n>1, define an by

an = 3an-1 +1 if an-1 is odd

an = 0.5an-1 if an-1 is even

This is called the Collatz sequence (after Lothar Collatz (1910-90), a German mathematician)

It is also known as the 3X + 1 sequence e.g. İf we choose a1 = 3, then the sequence is

3, 10, 5, 16, 8, 4, 2, 1, 4, 2, 1, 4, 2, 1, … In fact for any starting number, if the sequence ever reaches

1, it will cycle through 1, 4 and 2. İf we choose a1 = 13, then the sequence is

13, 40, 20, 10, 5, 16, 8, 4, 2, 1, 4, 2, 1, 4, 2, 1, … In all examples, the Collatz sequence eventually reached the

value of 1.

Recursive FunctionsFactorial

A simple example of a recursively defined function is the factorial function:

n! = 1· 2· 3· 4 ···(n –2)·(n –1)·n i.e., the product of the first n positive numbers (by

convention, the product of nothing is 1, so that 0! = 1). Q: Find a recursive definition for n!

Recursive FunctionsFactorial

Initialization: 0!= 1 Recursion: n != n · (n -1)! To compute the value of a recursive function, e.g. 5!, one

plugs into the recursive definition obtaining expressions involving lower and lower values of the function, until arriving at the base case.

EG: 5! =

Recursive FunctionsFactorial

Initialization: 0!= 1 Recursion: n != n · (n -1)! To compute the value of a recursive function, e.g. 5!, one

plugs into the recursive definition obtaining expressions involving lower and lower values of the function, until arriving at the base case.

EG: 5! = 5.4!

Recursive FunctionsFactorial

Initialization: 0!= 1 Recursion: n != n · (n -1)! To compute the value of a recursive function, e.g. 5!, one

plugs into the recursive definition obtaining expressions involving lower and lower values of the function, until arriving at the base case.

EG: 5! = 5.4! = 5.4.3!

Recursive FunctionsFactorial

Initialization: 0!= 1 Recursion: n != n · (n -1)! To compute the value of a recursive function, e.g. 5!, one

plugs into the recursive definition obtaining expressions involving lower and lower values of the function, until arriving at the base case.

EG: 5! = 5.4! = 5.4.3! = 5.4.3.2!

Recursive FunctionsFactorial

Initialization: 0!= 1 Recursion: n != n · (n -1)! To compute the value of a recursive function, e.g. 5!, one

plugs into the recursive definition obtaining expressions involving lower and lower values of the function, until arriving at the base case.

EG: 5! = 5.4! = 5.4.3! = 5.4.3.2! =5.4.3.2.1!

Recursive FunctionsFactorial

Initialization: 0!= 1 Recursion: n != n · (n -1)! To compute the value of a recursive function, e.g. 5!, one

plugs into the recursive definition obtaining expressions involving lower and lower values of the function, until arriving at the base case.

EG: 5! = 5.4! = 5.4.3! = 5.4.3.2! =5.4.3.2.1! = 5.4.3.2.1.0!

An Iterative Algorithm for n! 1. Input n

2. t:=13. For i = 1 to n do

3.1 t:=i x t4. Output tThis is an iterative algorithm because of the For-do loop,but it is not recursive (the algorithm does not call itself)

Function factorial (n)1. t:=12. For i = 1 to n do

2.1 t:= i x t3. factorial:=t

A Recursive Algorithm for n! The function algorithm for n! İs an iterative algorithm, based

on the recursive definition0!= 1n != n · (n -1)! (n>0)

Function factorial (n)1. If n = 0 then

1.1. factorial:=1else1.2. factorial:= n x factorial(n-1)

This is now a genuine recursive algorithm, because it contains a call to itself.

How does the Algorithm work? Function factorial (n)

1. If n = 0 then1.1. factorial:=1else1.2. factorial:= n x factorial(n-1)

Compute 5!

f(5)=5·f(4)

Function factorial (n) 1. If n = 0 then 1.1. factorial:=1 else 1.2. factorial:= n x factorial(n-1)

How does the Algorithm work?

Function factorial (n) 1. If n = 0 then 1.1. factorial:=1 else 1.2. factorial:= n x factorial(n-1)

How does the Algorithm work?

f(4)=4·f(3)

f(5)=5·f(4)

Function factorial (n) 1. If n = 0 then 1.1. factorial:=1 else 1.2. factorial:= n x factorial(n-1)

How does the Algorithm work?

f(3)=3·f(2)

f(4)=4·f(3)

f(5)=5·f(4)

Function factorial (n) 1. If n = 0 then 1.1. factorial:=1 else 1.2. factorial:= n x factorial(n-1)

How does the Algorithm work?

f(2)=2·f(1)

f(3)=3·f(2)

f(4)=4·f(3)

f(5)=5·f(4)

Function factorial (n) 1. If n = 0 then 1.1. factorial:=1 else 1.2. factorial:= n x factorial(n-1)

How does the Algorithm work?

f(1)=1·f(0)

f(2)=2·f(1)

f(3)=3·f(2)

f(4)=4·f(3)

f(5)=5·f(4)

Function factorial (n) 1. If n = 0 then 1.1. factorial:=1 else 1.2. factorial:= n x factorial(n-1)

How does the Algorithm work?

f(0)=1

f(1)=1·f(0)

f(2)=2·f(1)

f(3)=3·f(2)

f(4)=4·f(3)

f(5)=5·f(4)

Function factorial (n) 1. If n = 0 then 1.1. factorial:=1 else 1.2. factorial:= n x factorial(n-1)

How does the Algorithm work?

1·1=1

f(2)=2·f(1)

f(3)=3·f(2)

f(4)=4·f(3)

f(5)=5·f(4)

Function factorial (n) 1. If n = 0 then 1.1. factorial:=1 else 1.2. factorial:= n x factorial(n-1)

How does the Algorithm work?

2·1=2

f(3)=3·f(2)

f(4)=4·f(3)

f(5)=5·f(4)

Function factorial (n) 1. If n = 0 then 1.1. factorial:=1 else 1.2. factorial:= n x factorial(n-1)

How does the Algorithm work?

3·2=6

f(4)=4·f(3)

f(5)=5·f(4)

Function factorial (n) 1. If n = 0 then 1.1. factorial:=1 else 1.2. factorial:= n x factorial(n-1)

How does the Algorithm work?

4·6=24

f(5)=5·f(4)

Function factorial (n) 1. If n = 0 then 1.1. factorial:=1 else 1.2. factorial:= n x factorial(n-1)

How does the Algorithm work?

5·24=120

Function factorial (n) 1. If n = 0 then 1.1. factorial:=1 else 1.2. factorial:= n x factorial(n-1)

How does the Algorithm work?

Return 5! = 120

Another Recursive Algorithm Example: The sequence 2, 4, 9, 23, 64, 186, … can be defined

recursively by: a1 = 2

an = 3an-1 – n (n>1)

Write a recursive algorithm for this sequence. Solution:

Function t(n)1. If n = 1 then 1.1. t:=2 else 1.2. t:= 3t(n-1) - n

To Output the First m terms A way to output the 1st m terms of this sequence is to use an

algorithm that calls the function:1. Input m2. For n = 1 to m do 2.1 x:= t(n) 2.2 Output x

Although this works, it is not very efficiente.g. If m = 4, t(1) would be calculated four times, t(2) three times, and so on.This is because our algorithm don’t provide a means storing results that can be used in subsequent passes through the For-do loop.

An Efficient Approach Using the algorithm we already have, output the first m-1

terms, omit this step if m = 1 Evaluate t(m), the mth term of the sequence Output the value of t(m)

output_sequence (m)1. If m = 1 then 1.1 t:= 2 else 1.2 output_sequence(m-1) 1.3 t:= 3t – m2. Output t

How does the Algorithm work? output_sequence (m)

1. If m = 1 then 1.1 t:= 2 else 1.2 output_sequence(m-1) 1.3 t:= 3t – m2. Output t

Output the first 5 terms

How does the Algorithm work? output_sequence (m)

1. If m = 1 then 1.1 t:= 2 else 1.2 output_sequence(m-1) 1.3 t:= 3t – m2. Output t

os(5)

1.2 os(4)

How does the Algorithm work? output_sequence (m)

1. If m = 1 then 1.1 t:= 2 else 1.2 output_sequence(m-1) 1.3 t:= 3t – m2. Output t

os(4)

1.2 os(3)

os(5)

1.2 os(4)

How does the Algorithm work? output_sequence (m)

1. If m = 1 then 1.1 t:= 2 else 1.2 output_sequence(m-1) 1.3 t:= 3t – m2. Output t

os(3)

1.2 os(2)

os(4)

1.2 os(3)

os(5)

1.2 os(4)

How does the Algorithm work? output_sequence (m)

1. If m = 1 then 1.1 t:= 2 else 1.2 output_sequence(m-1) 1.3 t:= 3t – m2. Output t

os(2)

1.2 os(1)

os(3)

1.2 os(2)

os(4)

1.2 os(3)

os(5)

1.2 os(4)

How does the Algorithm work? output_sequence (m)

1. If m = 1 then 1.1 t:= 2 else 1.2 output_sequence(m-1) 1.3 t:= 3t – m2. Output t

os(1)1.1.t:=22.

output

os(2)

1.2 os(1)

os(3)

1.2 os(2)

os(4)

1.2 os(3)

os(5)

1.2 os(4)

How does the Algorithm work? output_sequence (m)

1. If m = 1 then 1.1 t:= 2 else 1.2 output_sequence(m-1) 1.3 t:= 3t – m2. Output t

2

os(2)1.3.t:=

3.2-22.

output

os(3)

1.2 os(2)

os(4)

1.2 os(3)

os(5)

1.2 os(4)

How does the Algorithm work? output_sequence (m)

1. If m = 1 then 1.1 t:= 2 else 1.2 output_sequence(m-1) 1.3 t:= 3t – m2. Output t

2 4

os(3)1.3.t:=

3.4-32.

output

os(4)

1.2 os(3)

os(5)

1.2 os(4)

How does the Algorithm work? output_sequence (m)

1. If m = 1 then 1.1 t:= 2 else 1.2 output_sequence(m-1) 1.3 t:= 3t – m2. Output t

2 4 9

os(4)1.3.t:=

3.9-42.

output

os(5)

1.2 os(4)

How does the Algorithm work? output_sequence (m)

1. If m = 1 then 1.1 t:= 2 else 1.2 output_sequence(m-1) 1.3 t:= 3t – m2. Output t

2 4 9 23

os(5)1.3.t:=

3.23-52.

output

How does the Algorithm work? output_sequence (m)

1. If m = 1 then 1.1 t:= 2 else 1.2 output_sequence(m-1) 1.3 t:= 3t – m2. Output t

2 4 9 23 64