Recursion Ali Taheri Sharif University of Technology Spring 2019 - - PowerPoint PPT Presentation

β–Ά
recursion
SMART_READER_LITE
LIVE PREVIEW

Recursion Ali Taheri Sharif University of Technology Spring 2019 - - PowerPoint PPT Presentation

Fu Fundamentals of Pr Programming (Py Python) Recursion Ali Taheri Sharif University of Technology Spring 2019 Outline 1. Recursive Functions 2. Example: Fibonacci 3. Memoization 4. Example: Binomial Coefficient 5. Example: Towers of


slide-1
SLIDE 1

Fu Fundamentals of Pr Programming (Py Python)

Recursion

Ali Taheri Sharif University of Technology

Spring 2019

slide-2
SLIDE 2

Outline

  • 1. Recursive Functions
  • 2. Example: Fibonacci
  • 3. Memoization
  • 4. Example: Binomial Coefficient
  • 5. Example: Towers of Hanoi
  • 6. Example: Fast Exponentiation

2

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

slide-3
SLIDE 3

Recursive Function

A function which calls itself is referred to as a Recursive function

  • Example: the factorial function

π‘œ! = π‘œ βˆ— π‘œ βˆ’ 1 ! 𝑗𝑔 π‘œ > 0 1 π‘π‘’β„Žπ‘“π‘ π‘₯β„Žπ‘—π‘‘π‘“

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

3

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

slide-4
SLIDE 4

Example: Fibonacci

The Fibonacci sequence:

  • 0,1,1,2,3,5,8,13,21,34,55,89,134

𝐺𝑗𝑐 π‘œ = 𝑗𝑔 π‘œ = 0 1 𝑗𝑔 π‘œ = 1 𝐺𝑗𝑐 π‘œ βˆ’ 1 + 𝐺𝑗𝑐 π‘œ βˆ’ 2 π‘π‘’β„Žπ‘“π‘ π‘₯𝑗𝑑𝑓

4

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

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

slide-5
SLIDE 5

Example: Binomial Coefficient

The binomial coefficient counts the number of ways to select 𝑙 items out of π‘œ:

π‘œ 𝑙 = π‘œ 𝑙 βˆ’ 1 + π‘œ βˆ’ 1 𝑙 βˆ’ 1 π‘œ 0 = π‘œ π‘œ = 1

5

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

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

slide-6
SLIDE 6

Example: Towers of Hanoi

An elegant application of recursion is to the Towers

  • f 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

6

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

slide-7
SLIDE 7

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.

7

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

slide-8
SLIDE 8

Example: Towers of Hanoi

8

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

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)

slide-9
SLIDE 9

Some Problems

9

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

Problem #1

Assume a table π‘ˆ having following value in cell (𝑗, π‘˜):

π‘ˆ(𝑗, π‘˜) = π‘˜ 𝑗 = 1

𝑙=1 π‘˜

π‘ˆ(𝑗 βˆ’ 1, 𝑙) 𝑗 > 1

Write a function to calculate π‘ˆ(𝑗, π‘˜) recursively.

Problem #2

We 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

  • ne of the columns beside.

Write a recursive function to compute the number of ways we can move the marble to cell (𝑗, π‘˜).

slide-10
SLIDE 10

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

10

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

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]

slide-11
SLIDE 11

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

11

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

slide-12
SLIDE 12

Example: Fast Exponentiation

12

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

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)