recursive algorithms a recursive algorithm calls itself to do part of its work the “call to...

13
Recursive Algorithms • A recursive algorithm calls itself to do part of its work • The “call to itself” must be on a smaller problem than the one originally attempted

Upload: elizabeth-townsend

Post on 18-Jan-2016

213 views

Category:

Documents


1 download

TRANSCRIPT

Recursive Algorithms

• A recursive algorithm calls itself to do part of its work

• The “call to itself” must be on a smaller problem than the one originally attempted

Recursive Algorithms (cont’d)

• Recursive solutions consist of:– Base case(s)

• The problem is explicitly solved for certain values of the input (generally small values)

– Recursive step• Divide the problem into one or more simpler or smaller

parts• Solve each part recursively

– Combine the solutions of the parts into a solution to the problem

IMPORTANT!If this is missing, or notimplemented correctly,your program will not terminate.

IMPORTANT!

If the "part" is as big as the whole, then your program will not terminate

Recursive Algorithm Examples

• Add 1..n • Factorial

Recursive Algorithms (cont’d)

• Easy to start from a recursive definition of a function to its implementation as a recursive algorithm

EXAMPLE:

f(n) = n! (the factorial of n) and

the corresponding recursive algorithm

Time Complexity

• T(1) = 2• T(n) = T(n-1) + 4• Recurrence relation

• T(N) = 4n-2

The Towers of Hanoi

• Only one disc may be moved at a timeA disc may be placed on top of another only if the disc being moved has a smaller diameter than the one upon which it is placed

3-disc Towers of Hanoi Solution

Solving the 3-disc puzzle requires 23-1 = 7 moves

Solving the n-disc puzzle requires 2n -1 moves

The 64-disc Towers of Hanoi

The 64-disc puzzle requires

264 = 18,446,744,073,709,551,616 moves

Suppose the monks can make 1 move per second. This translates into

60 moves per minute3600 moves per hour86400 moves per day31536000 moves per year

At this rate it will take about 584,942,417,355 years to solve the problem

Towers of Hanoi Recursively

• Suppose the pegs are numbered 1, 2, and 3 and the initial configuration is n discs on one of those pegs, call it start

start goal tmp

Towers of Hanoi Recursively

Write a recursive algorithm for solving the n-disc Towers of Hanoi puzzle

Suppose we want to move the n discs from the start peg to the goal peg, denote the other peg by tmp

Algorithm

• To move n discs from A to C, B as tmp• If n>0 then

– Move n-1 from A to B, C as tmp– Move the remaining disc from A to C– Move n-1 from B to C, A as tmp

• EndIf

Recursive Procedures

• Pros– Often intuitive, more elegant– Result in shorter programs– Sometimes, a recursive solution may result in a

faster algorithm– Usually easier to prove correctness

• Cons– More overhead due to function calls– More memory used at runtime– Sometimes, not as fast as an iterative version of

the algorithm

Recursive vs. Iteration

• Any problem that can be solved recursively can be solved iteratively

• Choose recursion when– you have a recursive data structure– the recursive solution is easier to understand/debug

• Do not choose recursion when– performance is an issue

• Examples where recursion is better– Towers of Hanoi, certain sorting algorithms