Download presentation
Presentation is loading. Please wait.
Published byVictoria Aleesha Austin Modified over 6 years ago
1
Recursion Gerry Howser, April 23, 2015
2
Example: Linear Search of an unordered list
Iteration Usually do a series of sequential steps on a piece of data and then repeat on a new piece of data Often involve loops Example: Linear Search of an unordered list
3
Iteration vs Recursion
Some problems have both iterative and recursive solutions Some problems lend themselves to recursive solutions Some problems do not lend themselves to recursion
4
Recursive Solutions Only specific types of problems can have recursive solutions The problem can be broken into sub-problems that exactly resemble the original problem Often the problem can be split in half Binary search Merge sort The solutions to the sub-problems combine to form a solution to the original problem
5
Two Quotes to Remember “Begin at the beginning”, the King said, very gravely, “and go on till you come to the end: then stop.” Lewis Carroll “If all you have is a hammer, everything looks like a nail.” Maslow’s Law
6
Problems with Recursive Solutions
The Tower of Hanoi Merge Sort Matrix Problems Factorials Others…..
7
Tower of Hanoi
8
Tower of Hanoi Pseudo Code
Solve(N, Src, Aux, Dst) if N is 0 exit else Solve(N - 1, Src, Dst, Aux) Move from Src to Dst Solve(N - 1, Aux, Src, Dst)
9
Recursive Merge Sort Merge_sort(A[ ],low, high) if low < high q = floor((low+high)/2.0) Merge_sort(A[ ], low, q) Merge_sort(A[ ], q+1, high) Merge(A[ ], low, q, high) return
10
Binary Tree of Pool Balls
Given 15 pool balls, build a tree such that: Any parent is larger than its left child Any parent is less than its right child Why does this hold for 7?
11
Pool Ball Binary Tree
12
Binary Tree of Pool Balls
Problem: Write an algorithm to list the pool balls in ascending order (In-order tree walk) Difficult to do with loops But if we choose wisely….. Can break the problem into identical sub-problems RECURSION!!!!
13
Pseudo Code to List Tree In Order
list_in_order(Tree, node){ if (node.left exists) list_in_order(Tree, node.left); list(node); if(node.right exists) list_in_order(Tree, node.right); return;}
14
Listing the Tree All we need now is a way to start this at the root:
in_order(Tree,root){ list_in_order(Tree, root); return;}
15
Factorials n!=n(n-1)(n-2)…1 (n-1)!=(n-1)(n-2)…1 n!=n * (n-1)!
All sub-problems are identical to original problem … and contribute to the solution RECURSION!!!
16
Pseudo Code for n! int fact(int i) { int result, n; n=i; if (n<0) result = -1; else result = 1; if(n>1) result = n * fact(n-1); return result; }
17
Recursion and n! (continued…)
Is this the only solution? Is this the best solution? What does “best” mean? Is there a better solution? Loop version
18
Loops and n! int fact(int n) { int result; int i=n; result=1; if(i<0) result = -1; while (i>0){ result = result * I; i--; }; return result; }
19
Generic Recursion recurse(A[ ],n) If(not base_case) recalculate n recurse(new_n) //or multiple times smash_together_answer return
20
Conclusions Many problems have recursive solutions
Must be able to break the problem into sub-problems with exactly the same solution method Must be able to smash sub-solutions together to form the complete solution Not all recursive solutions are best Not all problems have recursive solutions
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.