Top Banner
Main Index Conten ts 1 1 Main Index Conten ts Week 10 – Recursive Algorithms
23

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

Dec 17, 2015

Download

Documents

Phoebe Lane
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Main Index Contents 11 Main Index Contents Week 10 – Recursive Algorithms.

Main IndexMain Index ContentsContents11 Main IndexMain Index ContentsContents

Week 10– Recursive Algorithms

Page 2: 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. Divide2. Conquer3. Combine

Page 3: Main Index Contents 11 Main Index Contents Week 10 – Recursive Algorithms.
Page 4: Main Index Contents 11 Main Index Contents Week 10 – Recursive Algorithms.

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

Page 5: Main Index Contents 11 Main Index Contents Week 10 – Recursive Algorithms.

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

Page 6: Main Index Contents 11 Main Index Contents Week 10 – Recursive Algorithms.

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

Page 7: Main Index Contents 11 Main Index Contents Week 10 – Recursive Algorithms.

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

Page 8: Main Index Contents 11 Main Index Contents Week 10 – Recursive Algorithms.

Power

• Another approach, since = , we can split the problem to smaller problems.

• For example, could be computed as .

Page 9: Main Index Contents 11 Main Index Contents Week 10 – Recursive Algorithms.

Example: Computing x!

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

Fibonacci sequence

• Fibonacci numbers are the sequence of integers0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, …

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

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

Page 12: Main Index Contents 11 Main Index Contents Week 10 – Recursive Algorithms.

Fib(n): Recursion tree

Page 13: Main Index Contents 11 Main Index Contents Week 10 – Recursive Algorithms.

Iterative & Recursive version

• Which version is more time consuming?

Page 14: Main Index Contents 11 Main Index Contents Week 10 – Recursive Algorithms.

Let’s do an experiment

Page 15: Main Index Contents 11 Main Index Contents Week 10 – Recursive Algorithms.

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.

Page 16: Main Index Contents 11 Main Index Contents Week 10 – Recursive Algorithms.

Big-O of iterative version

• Only need to record the two most recently computed Fib numbers. This yields the O(n) linear complexity.

Page 17: Main Index Contents 11 Main Index Contents Week 10 – Recursive Algorithms.

Experiment result

• Same answer, different time consuming!

Page 18: Main Index Contents 11 Main Index Contents Week 10 – Recursive Algorithms.

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.

Page 19: Main Index Contents 11 Main Index Contents Week 10 – Recursive Algorithms.

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

Page 20: Main Index Contents 11 Main Index Contents Week 10 – Recursive Algorithms.

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

Page 21: Main Index Contents 11 Main Index Contents Week 10 – Recursive Algorithms.

Searching a binary search tree for a specific key can be a recursive or an iterative process.

Page 22: Main Index Contents 11 Main Index Contents Week 10 – Recursive Algorithms.

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.

Page 23: Main Index Contents 11 Main Index Contents Week 10 – Recursive Algorithms.

Reminder:

HW4 Due Wednesday 11/5/2014

Submission before Thursday 10/30/2014 will receive 30 extra

points