Fundamentals of Programming (Python)...
-
Upload
nguyenngoc -
Category
Documents
-
view
232 -
download
0
Transcript of Fundamentals of Programming (Python)...
![Page 1: Fundamentals of Programming (Python) Recursionce.sharif.edu/courses/96-97/2/ce153-4/resources/root/Slides/9... · Recursive function Example: the factorial function != ... Write a](https://reader031.fdocuments.in/reader031/viewer/2022021901/5b897d5a7f8b9ae7298c8b46/html5/thumbnails/1.jpg)
Fundamentals of Programming(Python)
Recursion
Ali TaheriSharif University of Technology
Spring 2018
![Page 2: Fundamentals of Programming (Python) Recursionce.sharif.edu/courses/96-97/2/ce153-4/resources/root/Slides/9... · Recursive function Example: the factorial function != ... Write a](https://reader031.fdocuments.in/reader031/viewer/2022021901/5b897d5a7f8b9ae7298c8b46/html5/thumbnails/2.jpg)
Outline1. Recursive Functions
2. Example: Fibonacci
3. Memoization
4. Example: Binomial Coefficient
5. Example: Towers of Hanoi
6. Example: Fast Exponentiation
2ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018
![Page 3: Fundamentals of Programming (Python) Recursionce.sharif.edu/courses/96-97/2/ce153-4/resources/root/Slides/9... · Recursive function Example: the factorial function != ... Write a](https://reader031.fdocuments.in/reader031/viewer/2022021901/5b897d5a7f8b9ae7298c8b46/html5/thumbnails/3.jpg)
Recursive Function
A function which calls itself is referred to as a Recursive function◦ Example: the factorial function
𝑛! = 𝑛 ∗ 𝑛 − 1 ! 𝑖𝑓 𝑛 > 01 𝑜𝑡ℎ𝑒𝑟𝑤ℎ𝑖𝑠𝑒
A recursive function always consists of two parts◦ Base Case: one or more cases for which no recursion is applied
◦ Recursive Step: all chains of recursion eventually end up in one of the base cases
The concept is very similar to mathematical induction
3ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018
![Page 4: Fundamentals of Programming (Python) Recursionce.sharif.edu/courses/96-97/2/ce153-4/resources/root/Slides/9... · Recursive function Example: the factorial function != ... Write a](https://reader031.fdocuments.in/reader031/viewer/2022021901/5b897d5a7f8b9ae7298c8b46/html5/thumbnails/4.jpg)
Example: Fibonacci
The Fibonacci sequence:◦ 0,1,1,2,3,5,8,13,21,34,55,89,134
𝐹𝑖𝑏 𝑛 =
0 𝑖𝑓 𝑛 = 01 𝑖𝑓 𝑛 = 1
𝐹𝑖𝑏 𝑛 − 1 + 𝐹𝑖𝑏 𝑛 − 2 𝑜𝑡ℎ𝑒𝑟𝑤𝑖𝑠𝑒
4ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018
def fib(n):
if n == 0: # Base Case
return 0
if n == 1: # Base Case
return 1
return fib(n-1) + fib(n-2) # Recursion Step
![Page 5: Fundamentals of Programming (Python) Recursionce.sharif.edu/courses/96-97/2/ce153-4/resources/root/Slides/9... · Recursive function Example: the factorial function != ... Write a](https://reader031.fdocuments.in/reader031/viewer/2022021901/5b897d5a7f8b9ae7298c8b46/html5/thumbnails/5.jpg)
Example: Binomial Coefficient
The binomial coefficient counts the number of ways to select 𝑘 items out of 𝑛:
𝑛
𝑘=
𝑛
𝑘 − 1+
𝑛 − 1
𝑘 − 1𝑛
0=
𝑛
𝑛= 1
5ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018
def bin(n,k):
if k == 0 or k == n: # Base Cases
return 1
return bin(n-1,k) + bin(n-1,k-1) # Recursion Step
![Page 6: Fundamentals of Programming (Python) Recursionce.sharif.edu/courses/96-97/2/ce153-4/resources/root/Slides/9... · Recursive function Example: the factorial function != ... Write a](https://reader031.fdocuments.in/reader031/viewer/2022021901/5b897d5a7f8b9ae7298c8b46/html5/thumbnails/6.jpg)
Example: Towers of Hanoi
An elegant application of recursion is to the Towers of Hanoi problem:◦ There are three pegs and a pyramid-shaped stack of 𝑛 disks
◦ The goal is to move all disks from the source peg into the target peg with the help of the third peg under some constraints
6ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018
![Page 7: Fundamentals of Programming (Python) Recursionce.sharif.edu/courses/96-97/2/ce153-4/resources/root/Slides/9... · Recursive function Example: the factorial function != ... Write a](https://reader031.fdocuments.in/reader031/viewer/2022021901/5b897d5a7f8b9ae7298c8b46/html5/thumbnails/7.jpg)
Example: Towers of Hanoi
Constraints:◦ Only one disk may be moved at a time
◦ A disk may not be “set aside”. It may only be stacked on one of the three pegs
◦ A larger disk may never be placed on top of a smaller one.
7ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018
![Page 8: Fundamentals of Programming (Python) Recursionce.sharif.edu/courses/96-97/2/ce153-4/resources/root/Slides/9... · Recursive function Example: the factorial function != ... Write a](https://reader031.fdocuments.in/reader031/viewer/2022021901/5b897d5a7f8b9ae7298c8b46/html5/thumbnails/8.jpg)
Example: Towers of Hanoi
8ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018
def hanoi(n, source, target, third):
if n == 1: # Base case (we have only one disk)
print('Moving disk %d from %s to %s'
% (n, source, target))
else: # Recursive Step
# Move n-1 disk from source to third
hanoi(n - 1, source, third, target)
# Move nth disk from source to target
print('Moving disk %d from %s to %s'
% (n, source, target))
# Move n-1 disk from third to target
hanoi(n - 1, third, target, source)
![Page 9: Fundamentals of Programming (Python) Recursionce.sharif.edu/courses/96-97/2/ce153-4/resources/root/Slides/9... · Recursive function Example: the factorial function != ... Write a](https://reader031.fdocuments.in/reader031/viewer/2022021901/5b897d5a7f8b9ae7298c8b46/html5/thumbnails/9.jpg)
Some Problems
9ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018
Problem #1 Assume a table 𝑇 having following value in cell (𝑖, 𝑗):
𝑇(𝑖, 𝑗) =
𝑗 𝑖 = 1
𝑘=1
𝑗
𝑇(𝑖 − 1, 𝑘) 𝑖 > 1
Write a function to calculate 𝑇(𝑖, 𝑗) recursively.
Problem #2We have a game board having ∞ rows and 𝑁 columns. Having a marble in row 1 and column 𝑐 (cell (1, 𝑐)).
In each phase, we can move the marble to the next row of current column or one of the columns beside.
Write a recursive function to compute the number of ways we can move the marble to cell (𝑖, 𝑗).
![Page 10: Fundamentals of Programming (Python) Recursionce.sharif.edu/courses/96-97/2/ce153-4/resources/root/Slides/9... · Recursive function Example: the factorial function != ... Write a](https://reader031.fdocuments.in/reader031/viewer/2022021901/5b897d5a7f8b9ae7298c8b46/html5/thumbnails/10.jpg)
Memoization
Recursive step often computes similar results multiple times which is inefficient
We can save partial results for further use◦ This technique is called memoization
10ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018
memo = {} # Partial results
def fib(n):
if n == 0: # Base Case
return 0
if n == 1: # Base Case
return 1
if n in memo: # Return the result if
return memo[n] # it has already computed
memo[n] = fib(n-1) + fib(n-2) # Save the result
return memo[n]
![Page 11: Fundamentals of Programming (Python) Recursionce.sharif.edu/courses/96-97/2/ce153-4/resources/root/Slides/9... · Recursive function Example: the factorial function != ... Write a](https://reader031.fdocuments.in/reader031/viewer/2022021901/5b897d5a7f8b9ae7298c8b46/html5/thumbnails/11.jpg)
Example: Fast Exponentiation
Given a matrix 𝐴, we want to calculate 𝐴𝑛
◦ Suppose we have written a function mat_prod to calculate the product of two matrices
◦ We can use mat_prod 𝑛 times to calculate 𝐴𝑛, but it is inefficient for large 𝑛
◦ Divide and conquer technique can lead to a more efficient solution
𝐴𝑛 =
𝐴𝑛/2 × 𝐴𝑛/2 𝑖𝑓 𝑛 𝑖𝑠 𝑒𝑣𝑒𝑛
𝐴 𝑛/2 × 𝐴 𝑛/2 × 𝐴 𝑖𝑓 𝑛 𝑖𝑠 𝑜𝑑𝑑𝐼 𝑖𝑓 𝑛 = 0
11ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018
![Page 12: Fundamentals of Programming (Python) Recursionce.sharif.edu/courses/96-97/2/ce153-4/resources/root/Slides/9... · Recursive function Example: the factorial function != ... Write a](https://reader031.fdocuments.in/reader031/viewer/2022021901/5b897d5a7f8b9ae7298c8b46/html5/thumbnails/12.jpg)
Example: Fast Exponentiation
12ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018
def exp(A, n):
if n == 0: # Base Case
return [[1, 0],
[0, 1]]
temp = exp(A, n // 2) # A**(n/2)
prod = mat_prod(temp, temp) # A**n
if n % 2 == 0:
return prod
else:
return mat_prod(prod, A)