1 / 18

Recursive Definitions: functions, structures, languages

Recursive Definitions: functions, structures, languages. Sections 5.3 & 5.4 Tuesday, 10 June. Recursively Defined Functions. A recursive definition (also called an inductive definition ) for a function from f : N  S: Specifies the initial value of the function (value at 0).

bao
Download Presentation

Recursive Definitions: functions, structures, languages

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Recursive Definitions: functions, structures, languages Sections 5.3 & 5.4 Tuesday, 10 June RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  2. Recursively Defined Functions • A recursive definition (also called an inductive definition) for a function from f :N S: • Specifies the initial value of the function (value at 0). • Gives a rule for finding the value of at an integer from the values at smaller integers. 2 RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  3. Example • Let f : N  N be defined recursively by: • f (0) = 3, • f (n) = 2 f (n - 1) + 3 for n≥ 1. • Let’s find f (1), f (2), f (3), f (4). • f (1) = 2f (0) + 3 = 2  3 + 3 = 9 • f (2) = 2f (1) + 3 = 2  9 + 3 = 21 • f (3) = 2f (2) + 3 = 2  21 + 3 = 45 • f (4) = 2f (3) + 3 = 2  45 + 3 = 93 … 3 RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  4. Example: An inductive definition for F(n) = 2n • F(0) = 1 • F(n) = 2 F(n-1) , for n > 0 4 RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  5. Example • Define the factorial function F: N  N as follows: • F(0) = 1 • F(n) = n  F(n - 1) for n≥ 1. • Exercise: Use this definition to calculate • F(5) 5 RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  6. Example • The Fibonacci numbers, f0, f1, f2, …, are defined recursively by: • f0 = 1, • f1 = 1, • fn = fn-1 + fn-2 n≥ 2. • Let’s find f2, f3, f4, f5: • f2 = f1 + f0 = 1 + 1 = 2 • f3 = f2 + f1 = 2 + 1 = 3 • f4 = f3 + f2 = 3 + 2 = 5 • f5 = f4 + f3 = 5 + 3 = 8 • Note the alternative notation: fn instead of f (n) Note: because the recursive rule uses two previous values to compute the next one, we need two initial conditions. 6 RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  7. Python recursive fibonacci function def fib(n): if n == 0: return 1 elif n == 1: return 1 else: return fib(n-1) + fib(n-2) RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  8. The sequence grows fast, so does the execution time for recursive computation. RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  9. The sequence grows fast, so does the execution time for recursive computation. Computation of fib(n) repeats all the work to compute fib(n – 1). Number of recursive calls grows exponentially. Computing fib(100) requires 708449696358523830149 function calls, which would take longer than the age of the universe to execute. (According tohttp://en.literateprograms.org/Fibonacci_numbers_(Python), Oct 2011.) RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  10. Instead, use “memoization” to cache previous computations. # Cache computed values in a dictionary, entries composed of a key n and # the corresponding value fib(n). memo = {0:1, 1:1} # initialize the cache (dictionary) with the values for # fib(0) and fib(1) def fib2(n): if not n in memo: memo[n] = fib2(n-1) + fib2(n-2) # save the computed value in the cache return memo[n] Computes fib2(100) in negligible time: >>> fib2(100) 573147844013817084101L RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  11. Exercise:Tower of Hanoi • Starting position: • n disks on 1st peg • ordered by (decreasing) size (bottom to top) • Goal: • move all disks onto 3rd peg, ordered by size • Legal moves: • the top disk on any peg may be moved to the top of another peg so long as it is not placed on top of a smaller disk • How many moves are required for a puzzle with n disks? 11 RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  12. Recursive step for Tower of Hanoi: suppose you can solve puzzle for k disks Move the top k disks to middle post. (Ignore the biggest disk!) k disks k disks Disk k+1 Disk k+1 Disk k+1 Disk k+1 Move disk k + 1 to 3rd post. k disks Disk k+1 Disk k+1 Move the top k disks to 3rd post. (Ignore the biggest disk again!) RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  13. AnalyzingTower of Hanoi (TOH) • Let Tnbe the minimum number of moves to transfer n disks from one peg to another, following the rules. • Previous slide shows:Tn ≤ 2 Tn-1+ 1, for n > 1 • How to show: Tn ≥ 2 Tn-1+ 1, for n > 1 13 RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  14. AnalyzingTower of Hanoi (TOH) • Let Tnbe the minimum number of moves to transfer n disks from one peg to a another, following the rules. • To prove: Tn≥ 2 Tn-1+ 1, for n > 1 Let k be the number of the move at which disk n is moved to the other peg. Since the kthmove is legal, disks 1… n – 1are on the remaining peg. Since this transfer cannot be done in fewer than Tn-1 moves, k ≥ Tn-1. 14 RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  15. AnalyzingTower of Hanoi (TOH) Moreover, after move k, disks 1…n – 1must be transferred to the peg. Since this transfer cannot be done in fewer than Tn-1 moves,we know that Tn– (k + 1) ≥ Tn-1. Thus, Tn≥ Tn-1 + (k + 1) ≥ Tn-1 + (Tn-1+ 1) = 2 Tn-1+ 1 15 RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  16. AnalyzingTower of Hanoi (TOH) • Let Tnbe the minimum number of moves to transfer n disks from one peg to another, following the rules. • We conclude that can be defined recursively by: • T1 = 1, and • Tn= 2 Tn-1+ 1, for n > 1 • Next, we use this recurrence relation to prove: Tn= 2n – 1, for n ≥ 1. 16 RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  17. AnalyzingTower of Hanoi (TOH) • Assume: • T1 = 1, and • Tn = 2 Tn-1+ 1, for n > 1 • To prove Tn = 2n – 1, for n ≥ 1, we use induction: • Basis: T1 = 1 = 21– 1 proves the equation holds when n = 1. • Induction step: Assume n ≥ 1 and Tn = 2n – 1 (IH). Then, n + 1> 1, and so, by assumption, Tn+1= 2 Tn+ 1. We conclude Tn+1= 2 Tn+ 1 = 2(2n – 1) + 1 = 2n+1– 1, where the second equality follows from the IH. This completes the induction step. 17 RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  18. Comments on Recursion • There are several common applications of recursion where an iterative solution may not be obvious or easy to develop. Classic examples of such include Towers of Hanoi, path generation, multidimensional searching and backtracking. • However, many common textbook examples of recursion are tail-recursive, i.e. the last statement in the recursive function is a single recursive invocation. • Tail-recursive functions can be written more efficiently using a loop. RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

More Related