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 Hanoi 6. Example: Fast Exponentiation 2 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
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 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
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 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
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 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
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 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
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 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
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 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
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 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
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 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
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 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
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 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
Recommend
More recommend