Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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

2 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 the "part" is as big as the whole, then your program will not terminate IMPORTANT! If this is missing, or not implemented correctly, your program will not terminate.

3 Recursive Algorithm Examples
Add 1..n Factorial

4 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

5 Time Complexity T(1) = 2 T(n) = T(n-1) + 4 Recurrence relation
T(N) = 4n-2

6 The Towers of Hanoi Only one disc may be moved at a time
A 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

7 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

8 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 minute moves per hour moves per day moves per year At this rate it will take about 584,942,417,355 years to solve the problem

9 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

10 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

11 Algorithm To move n discs from A to C, B as tmp If n>0 then EndIf
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

12 Recursive Procedures Pros Cons 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

13 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


Download ppt "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."

Similar presentations


Ads by Google