+ All Categories
Home > Documents > Main Index Contents 11 Main Index Contents Week 10 – Recursive Algorithms.

Main Index Contents 11 Main Index Contents Week 10 – Recursive Algorithms.

Date post: 17-Dec-2015
Category:
Upload: phoebe-lane
View: 233 times
Download: 1 times
Share this document with a friend
23
Main Index Conten ts 1 1 Main Index Conten ts Week 10 – Recursive Algorithms
Transcript

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

In-order scan example

L

N

R

L N R

L N RL N RL N R

Recursively!In-order scan: B, D, A, E, C

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 .

Example: Computing x!

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

Fib(n): Recursion tree

Iterative & Recursive version

• Which version is more time consuming?

Let’s do an experiment

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.

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 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


Recommended