0% found this document useful (0 votes)
6 views

6.python Recursion

Uploaded by

Kwok Shi Ming
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

6.python Recursion

Uploaded by

Kwok Shi Ming
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Recursion

A recursive function calls itself in its function body


An example

2
Another example: s(n)
Recall that s(n) = 1 + 2 + ... + (n-1) + n.
We can define it recursively as follows:
s(1) = 1,
s(n) = s(n-1) + n
We've implemented a for loop to compute s(n).
Now, we define a recursive function to compute s(n)

Key questions:
(1) How to design recursive functions?
(2) Why are they correct?

3
Start with small input instances
def s(n):
if n==1 return 1
if n==2 return s(1) + 2
if n==3 return s(2) + 3
if n==4 return s(3) + 4

Key: No matter what we add below, the


function is ALWAYS correct for s(1), s(2),
s(3), s(4)

4
Python will do the necessary Book-keeping
Activation record
def s(n):
if n==1 return 1 s(4): ...
return s(3)+4
if n==2 return s(1) + 2
if n==3 return s(2) + 3
if n==4 return s(3) + 4

5
Python will do the necessary Book-keeping
Activation record
def s(n):
if n==1 return 1 s(4): ...
return s(3)+4
if n==2 return s(1) + 2
if n==3 return s(2) + 3 s(3): ...
return s(2)+3
if n==4 return s(3) + 4

6
Python will do the necessary Book-keeping
Activation record
def s(n):
if n==1 return 1 s(4): ...
return s(3)+4
if n==2 return s(1) + 2
if n==3 return s(2) + 3 s(3): ...
return s(2)+3
if n==4 return s(3) + 4
s(2): ...
return s(1)+2

7
Python will do the necessary Book-keeping
Activation record
def s(n):
if n==1 return 1 s(4): ...
return s(3)+4
if n==2 return s(1) + 2
if n==3 return s(2) + 3 s(3): ...
return s(2)+3
if n==4 return s(3) + 4
s(2): ...
return s(1)+2

s(1): ...
return 1

8
Python will do the necessary Book-keeping
Activation record
def s(n):
if n==1 return 1 s(4): ...
return s(3)+4
if n==2 return s(1) + 2
if n==3 return s(2) + 3 s(3): ...
return s(2)+3
if n==4 return s(3) + 4
s(2): ...
return s(1)+2
1
s(1): ...
return 1

9
Python will do the necessary Book-keeping
Activation record
def s(n):
if n==1 return 1 s(4): ...
return s(3)+4
if n==2 return s(1) + 2
if n==3 return s(2) + 3 s(3): ...
return s(2)+3
if n==4 return s(3) + 4 3
s(2): ...
return 1+2

10
Python will do the necessary Book-keeping
Activation record
def s(n):
if n==1 return 1 s(4): ...
return s(3)+4
if n==2 return s(1) + 2 6
if n==3 return s(2) + 3 s(3): ...
return 3+3
if n==4 return s(3) + 4

11
Python will do the necessary Book-keeping
Activation record
def s(n): 10
if n==1 return 1 s(4): ...
return 6+4
if n==2 return s(1) + 2
if n==3 return s(2) + 3
if n==4 return s(3) + 4

12
Unify the “body”
def s(n): def s(n):
if n==1 return 1 if n==1 return 1
if n==2 return s(1) + 2 if n==2 return s(n-1) + n
if n==3 return s(2) + 3 if n==3 return s(n-1) + n
if n==4 return s(3) + 4  if n==4 return s(n-1) + n

13
Leap of faith
def s(n): def s(n):
if n==1 return 1 if n==1 return 1
if n==2 return s(n-1) + n if n==2 return s(n-1) + n
if n==3 return s(n-1) + n if n==3 return s(n-1) + n
if n==4 return s(n-1) + n if n==4 return s(n-1) + n
Recall: No matter what we add below, the if n==5 return s(n-1) + n
function will ALWAYS return correct
answer for s(1), s(2), s(3), s(4)
...
if n==M return s(n-1) +n
The function will return correct answer
for s(1), s(2), s(3), s(4), ..., s(M). If M is the
largest int value supported by Python, s(n)
is correct for all int. 14
Simplify your function
def s(n):
if n==1 return 1
if n==2 return s(n-1) + n
if n==3 return s(n-1) + n
if n==4 return s(n-1) + n ≡
if n==5 return s(n-1) + n
...
if n==M return s(n-1) +n

Impractical!!
15
Fibonacci Numbers F(n)
F(1) = 1
F(2) = 1
F(n) = F(n-1) + F(n-2) for all n > 2

Problem: Design a recursive function to compute F(n)

16
Start small instances
def fib(n): def fib(n):
if n==1: return 1 if n==1: return 1
if n==2: return 1 if n==2: return 1
if n==3: return fib(2)+fib(1) if n==3: return fib(n-1)+fib(n-2)
if n==4: return fib(3)+fib(2) if n==4: return fib(n-1)+fib(n-2)
if n==5: return fib(4)+fib(3) if n==5: return fib(n-1)+fib(n-2)
Key: No matter what we add below,
the function will ALWAYS return the
correct answer for fib(1), fib(2),
fib(3), fib(4) and fib(5)

17
leap of faith
def fib(n):
if n==1: return 1
if n==2: return 1
if n==3: return fib(n-1)+fib(n-2)
if n==4: return fib(n-1)+fib(n-2)
if n==5: return fib(n-1)+fib(n-2)

if n==M: return fib(n-1)+fib(n-2)

18
The recursion tree for fib(5) 5
fib(5)
3 + 2

fib(4) fib(3)

2 + 1 1 + 1

fib(2) fib(1)
fib(3) fib(2)
+
1 1

fib(2) fib(1)

19
In-class exercise: An exam question
There is a staircase with N steps, and you can climb 1 or 2
steps a time. Given N, write a function that returns the
number of ways you can climb the staircase.
For example, if N is 4, then there are 5 unique ways:
1,1,1,1
2,1,1
1,2,1
1,1,2
2,2

20
A variant of the exam question
There is a staircase with N steps, and you can climb 1 or 2
steps a time. Given N, write a function that prints all the
possible ways you can climb the staircase.
For example, if N is 4, then there are 5 unique ways:
1,1,1,1
2,1,1
1,2,1
1,1,2
2,2

21
A variant of the exam question

22
A variant of the exam question
A simpler solution:

23

You might also like