recursion
play

Recursion Ali Taheri Sharif University of Technology Fall 2018 - PowerPoint PPT Presentation

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


  1. Fu Fundamentals of Pr Programming (Py Python) Recursion Ali Taheri Sharif University of Technology Fall 2018

  2. Outline 1. Recursive Functions 2. Example: Fibonacci 3. Memoization 4. Example: Binomial Coefficient 5. Example: Towers of Hanoi 6. Example: Fast Exponentiation 2 Fall 2018 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  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 Fall 2018 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  4. Example: Fibonacci The Fibonacci sequence: β—¦ 0,1,1,2,3,5,8,13,21,34,55,89,134 0 𝑗𝑔 π‘œ = 0 1 𝑗𝑔 π‘œ = 1 𝐺𝑗𝑐 π‘œ = 𝐺𝑗𝑐 π‘œ βˆ’ 1 + 𝐺𝑗𝑐 π‘œ βˆ’ 2 π‘π‘’β„Žπ‘“π‘ π‘₯𝑗𝑑𝑓 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 4 Fall 2018 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  5. Example: Binomial Coefficient The binomial coefficient counts the number of ways to select 𝑙 items out of π‘œ : π‘œ 𝑙 βˆ’ 1 + π‘œ βˆ’ 1 π‘œ 𝑙 = 𝑙 βˆ’ 1 0 = π‘œ π‘œ π‘œ = 1 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 5 Fall 2018 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  6. 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 6 Fall 2018 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  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 Fall 2018 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  8. Example: Towers of Hanoi 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) 8 Fall 2018 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  9. Some Problems 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 one of the columns beside. Write a recursive function to compute the number of ways we can move the marble to cell (𝑗, π‘˜) . 9 Fall 2018 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  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 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] 10 Fall 2018 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  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 Fall 2018 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  12. Example: Fast Exponentiation 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) 12 Fall 2018 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

Recommend


More recommend