Date post: | 17-Dec-2015 |
Category: |
Documents |
Upload: | phoebe-lane |
View: | 233 times |
Download: | 1 times |
Main IndexMain Index ContentsContents11 Main IndexMain Index ContentsContents
Week 10– Recursive Algorithms
Divide-and-conquer algorithms
• Divide-and-conquer is a problem-solving technique that makes use of recursion.
1. Divide2. Conquer3. 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.
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
Power
• Evaluating the power where x is real number and n is a non-negative integer.
• Iterative control structures: uses looping to repeat a set of statements
Power
• Another approach, since = , we can split the problem to smaller problems.
• For example, could be computed as .
Fibonacci sequence
• Fibonacci numbers are the sequence of integers0, 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
Big-O of recursive version
• When n <= 1, T(n) = O(1).
• For n > 1, T(n) = T(n−1)+T(n−2). In this case T(n)=O(). Where ϕ is the golden ratio (ϕ=), which is exponential.
Big-O of iterative version
• Only need to record the two most recently computed Fib numbers. This yields the O(n) linear complexity.
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 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.