&kdswhu 5hfxuvlrq dqg 5hfxuuhqfh 5hodwlrqvftang/courses/cs130/notes/recursion.pdf · &6...

12
CS 130 – Discrete Structures Chapter 3 Recursion and Recurrence Relations

Upload: others

Post on 08-Sep-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: &KDSWHU 5HFXUVLRQ DQG 5HFXUUHQFH 5HODWLRQVftang/courses/CS130/notes/recursion.pdf · &6 ±'lvfuhwh 6wuxfwxuhv 5hfxuvlrq 5hfxuvlyh ghilqlwlrqv ghilqh dq hqwlw\ lq whupv ri lwvhoi 7khuh

CS 130 – Discrete Structures

Chapter 3 Recursion and Recurrence Relations

Page 2: &KDSWHU 5HFXUVLRQ DQG 5HFXUUHQFH 5HODWLRQVftang/courses/CS130/notes/recursion.pdf · &6 ±'lvfuhwh 6wuxfwxuhv 5hfxuvlrq 5hfxuvlyh ghilqlwlrqv ghilqh dq hqwlw\ lq whupv ri lwvhoi 7khuh

CS 130 – Discrete Structures 2

Recursive Examples

Page 3: &KDSWHU 5HFXUVLRQ DQG 5HFXUUHQFH 5HODWLRQVftang/courses/CS130/notes/recursion.pdf · &6 ±'lvfuhwh 6wuxfwxuhv 5hfxuvlrq 5hfxuvlyh ghilqlwlrqv ghilqh dq hqwlw\ lq whupv ri lwvhoi 7khuh

CS 130 – Discrete Structures 3

Recursion

• Recursive definitions: define an entity in terms of itself.• There are two parts:

– Basic case (basis): the most primitive case(s) of the entity are defined without reference to the entity.

– Recursive (inductive) case: new cases of the entity are defined in terms of simpler cases of the entity.

• Recursive sequences– A sequence is an ordered list of objects, which is

potentially infinite. s(k) denotes the kth object in the sequence.

– A sequence is defined recursively by explicitly naming the first value (or the first few values) in the sequence and then define later values in the sequence in terms of earlier values.

Page 4: &KDSWHU 5HFXUVLRQ DQG 5HFXUUHQFH 5HODWLRQVftang/courses/CS130/notes/recursion.pdf · &6 ±'lvfuhwh 6wuxfwxuhv 5hfxuvlrq 5hfxuvlyh ghilqlwlrqv ghilqh dq hqwlw\ lq whupv ri lwvhoi 7khuh

CS 130 – Discrete Structures 4

Examples of Recursive Sequence

• A recursively defined sequence:– S(1) = 2 …… basis case– S(n) = 2 * S(n-1), for n >= 2 …… recursive case– what does the sequence look like?

– T(1) =1 …… basis case– T(n) = T(n-1) + 3, for n >= 2 …… recursive case– what does the sequence look like?

Page 5: &KDSWHU 5HFXUVLRQ DQG 5HFXUUHQFH 5HODWLRQVftang/courses/CS130/notes/recursion.pdf · &6 ±'lvfuhwh 6wuxfwxuhv 5hfxuvlrq 5hfxuvlyh ghilqlwlrqv ghilqh dq hqwlw\ lq whupv ri lwvhoi 7khuh

CS 130 – Discrete Structures 5

Fibonacci Sequence

• The Fibonacci Sequence is defined by:• F(1) = 1, F(2) = 1 …… basis case• F(n) = F(n-2) + F(n-1), for n > 2 …… recursive case• the sequence?• http://www.mcs.surrey.ac.uk/Personal/R.Knott/Fibonacci/fibnat.html#Rabbits

Page 6: &KDSWHU 5HFXUVLRQ DQG 5HFXUUHQFH 5HODWLRQVftang/courses/CS130/notes/recursion.pdf · &6 ±'lvfuhwh 6wuxfwxuhv 5hfxuvlrq 5hfxuvlyh ghilqlwlrqv ghilqh dq hqwlw\ lq whupv ri lwvhoi 7khuh

CS 130 – Discrete Structures 6

Proofs Related To Recursively Defined Sequence

• Proofs of properties about recursively defined entities are typically inductive proofs.

• Prove directly from the definition• For example:

– prove that in the Fibonacci sequence: – F(n+4) = 3F(n+2) – F(n), for n >= 1– F(n+1) + F(n-2) = 2F(n), for n >= 3

Page 7: &KDSWHU 5HFXUVLRQ DQG 5HFXUUHQFH 5HODWLRQVftang/courses/CS130/notes/recursion.pdf · &6 ±'lvfuhwh 6wuxfwxuhv 5hfxuvlrq 5hfxuvlyh ghilqlwlrqv ghilqh dq hqwlw\ lq whupv ri lwvhoi 7khuh

CS 130 – Discrete Structures 7

Recursively Defined Operations

• Certain operations performed on objects can be defined recursively

• Examples:– A recursive definition of multiplication:

• m * 1 = m• m * n = m * (n-1) + m, for n >= 2

– A recursive definition of exponentiation:• a0 = 1• an = (an-1)*a, for n > 0

– A recursive definition of factorial operation:• F(0) = 1• F(n) = n * F(n-1) for n >= 1

Page 8: &KDSWHU 5HFXUVLRQ DQG 5HFXUUHQFH 5HODWLRQVftang/courses/CS130/notes/recursion.pdf · &6 ±'lvfuhwh 6wuxfwxuhv 5hfxuvlrq 5hfxuvlyh ghilqlwlrqv ghilqh dq hqwlw\ lq whupv ri lwvhoi 7khuh

CS 130 – Discrete Structures 8

Examples

• Give a recursive definition of the following sets of objects:– 1, 3, 9, 27, 81, …– 2, 1, ½, ¼, 1/8, …– 1, 2, 4, 7, 11, 16, 22, …

Page 9: &KDSWHU 5HFXUVLRQ DQG 5HFXUUHQFH 5HODWLRQVftang/courses/CS130/notes/recursion.pdf · &6 ±'lvfuhwh 6wuxfwxuhv 5hfxuvlrq 5hfxuvlyh ghilqlwlrqv ghilqh dq hqwlw\ lq whupv ri lwvhoi 7khuh

CS 130 – Discrete Structures 9

Recursively Defined Algorithms

• If a recurrence relation exists for an operation, then the algorithm for such a relation can be written either iteratively or recursively

• Example: factorial, multiplication, etc.• Factorial: S(1) = 2, S(n) = 2S(n-1) for n >= 2

Calculate S(n):if n = 1 then output 2 and returnj = 2S = 2while j <= n

S = S*2j = j + 1

end whileoutput S

Calculate S(n):if n = 1 then

return 2else

return 2*S(n-1)endif

Page 10: &KDSWHU 5HFXUVLRQ DQG 5HFXUUHQFH 5HODWLRQVftang/courses/CS130/notes/recursion.pdf · &6 ±'lvfuhwh 6wuxfwxuhv 5hfxuvlrq 5hfxuvlyh ghilqlwlrqv ghilqh dq hqwlw\ lq whupv ri lwvhoi 7khuh

CS 130 – Discrete Structures 10

Solving Recurrence Relations

• Closed-form solution– S(1) = 2– S(n) = 2*S(n-1) for n >= 2– Since S(1) = 2 = 21, S(2) = 4 = 22, S(3) = 8 = 23, …– We can see S(n) = 2n, and it is called a closed-form

solution to the recurrence relation– Finding a closed-form solution is called solving

recurrence relation

• General method: expand, guess and verify• Another method: solution formula (will not be

covered here)

Page 11: &KDSWHU 5HFXUVLRQ DQG 5HFXUUHQFH 5HODWLRQVftang/courses/CS130/notes/recursion.pdf · &6 ±'lvfuhwh 6wuxfwxuhv 5hfxuvlrq 5hfxuvlyh ghilqlwlrqv ghilqh dq hqwlw\ lq whupv ri lwvhoi 7khuh

CS 130 – Discrete Structures 11

Example of Expand, Guess, and Verify

• The factorial example: – S(1) = 2– S(n) = 2S(n-1) for n >= 2

• Expand:S(n) = 2S(n-1) = 2[2S(n-2)] = 22S(n-2)

= 22[2S(n-3)] = 23S(n-3)

• Guess: after k such expansions, the equation has the form: S(n) = 2kS(n-k)– it stops when n-k = 1, that is when k = n-1, replace

k with (n-1)– S(n) = 2n-1S(n-(n-1)) = 2n-1S(1) = 2n

• Verify: proof by induction

Page 12: &KDSWHU 5HFXUVLRQ DQG 5HFXUUHQFH 5HODWLRQVftang/courses/CS130/notes/recursion.pdf · &6 ±'lvfuhwh 6wuxfwxuhv 5hfxuvlrq 5hfxuvlyh ghilqlwlrqv ghilqh dq hqwlw\ lq whupv ri lwvhoi 7khuh

CS 130 – Discrete Structures 12

Exercise

• Solve the recurrence relation:– F(1) = 1– F(n) = n*F(n-1) for n >= 2

• Given the Fib. definition, prove that– F(n) = 5*F(n-4) + 3*F(n-5) for n >= 6