Main Index Contents 11 Main Index Contents Week 10 – Recursive Algorithms
Divide-and-conquer algorithms Divide-and-conquer is a problem-solving technique that makes use of recursion. 1.Divide 2.Conquer 3.Combine
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
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
In-order scan example L N R L N R Recursively! In-order scan: B, D, A, E, C
Example: Computing x!
Fibonacci sequence Fibonacci numbers are the sequence of integers 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, …
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
Fib(n): Recursion tree
Iterative & Recursive version Which version is more time consuming?
Let’s do an experiment
Big-O of recursive version
Big-O of iterative version Only need to record the two most recently computed Fib numbers. This yields the O(n) linear complexity.
Experiment result Same answer, different time consuming!
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.
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
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
Searching a binary search tree Searching a binary search tree for a specific key can be a recursive or an iterative process.
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.
Reminder: HW4 Due Wednesday 11/5/2014 Submission before Thursday 10/30/2014 will receive 30 extra points