Download presentation
Presentation is loading. Please wait.
Published byPhoebe Lane Modified over 9 years ago
1
Main Index Contents 11 Main Index Contents Week 10 – Recursive Algorithms
2
Divide-and-conquer algorithms Divide-and-conquer is a problem-solving technique that makes use of recursion. 1.Divide 2.Conquer 3.Combine
4
Recursion Solution to a problem depends on solutions to smaller instances of the same problem. Recursive function: a function that calls itself. As a tree is a self-referential (recursively defined) data structure, traversal can naturally be described by recursion.recursion 4
5
Concept of Recursion A function exhibits recursive behavior when it can be defined by two properties: 1.A simple base case (or cases) (stopping condition) 2.A set of rules that reduce all other cases toward the base case
6
In-order scan example L N R L N R Recursively! In-order scan: B, D, A, E, C
9
Example: Computing x!
10
Fibonacci sequence Fibonacci numbers are the sequence of integers 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, …
11
Fibonacci sequence Computing fib(n), Fibonacci element with index n Base case: fib(0) = 0, fib(1) = 1 Rule: fib(n) = fib(n-1) + fib(n-2) Stopping condition Recursive step
12
Fib(n): Recursion tree
13
Iterative & Recursive version Which version is more time consuming?
14
Let’s do an experiment
15
Big-O of recursive version
16
Big-O of iterative version Only need to record the two most recently computed Fib numbers. This yields the O(n) linear complexity.
17
Experiment result Same answer, different time consuming!
18
Why is recursive Fib so slow? To compute F(n). If one traces out the entire algorithm, we can see that F(n-3) is computed three times, F(n-4) s computed five times, F(n-5) is computed eight times, and so on. As this figure shows, the growth of redundant calculations is explosive.
19
Recursion isn’t necessary Computers don’t have “recursive hardware”! When a higher-level language is compiled, recursive calls are implemented with a stack: – When you enter a method, all its local variables (including its formal parameters) are created and put onto a stack – The method operates on its own copies of the local variables – When the method exits, its local variables are removed from the stack The compiler changes recursive code into non-recursive code It follows, then, that anything you could do recursively, you could also do non-recursively by using loops and a stack
20
Loops aren’t necessary You can replace any recursion with loops (and a stack for local variables) In addition, you can replace any loop with a recursion It can be proved (we won’t do it here) that loops can always be replaced by recursions
21
Searching a binary search tree Searching a binary search tree for a specific key can be a recursive or an iterative process.
22
Closing comments The intent of this set of slides is to get you more familiar with some of the uses of recursion Recursion and loops are, in some sense, equivalent--anything you can do with one, you can do with the other Once you understand recursion, though, it is often simpler to use recursion than to use loops Recursion can save your coding effort, but it is often very slow.
23
Reminder: HW4 Due Wednesday 11/5/2014 Submission before Thursday 10/30/2014 will receive 30 extra points
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.