C SC 205 Lecture 7: Recursion, Part 2
major resources: Data Structures and the Java Collections Framework Second Edition,
William Collins, McGraw-Hill, 2005
Introduction to Programming and OO Design
using Java, Niño and Hosch, Wiley & Sons, 2002
[ previous
| schedule
| next ]
Monday October 1 (week 4)
Recursive problem solving strategy
- handout of short PPT
- PPT developed by Dave Stucki, modified by me (Dr. Pete) but available only in printed form
- problem solving strategy applied to problems that are self-similar
- larger problem composed of instances of smaller problems of same kind
- example: fractals
- example: factorial function. For example, 6! = 6 * 5!
- example: Towers of Hanoi (previous class)
- Generic top-down approach to problem solving (recall it from CSC 100)
- Decompose problem into smaller subproblems
- solve smaller problem
- combine smaller solutions into larger one
- Approach to recursion is similar, from a function's point of view
- problem enters as parameter value
- extract smaller values of same kind from parameter
- apply the function recursively to solve smaller values
- combine solutions into larger solution and return as function return value
- gotta have a base case, a ready-made solution to the simplest subproblem.
- inductive reasoning also applies: does it work for base case? If it works for base case will
it work for input from which base case was extracted? If it works for case i extracted from case i+1, will it work for case i+1?
String Reversal case study (PPT and Collins chap 5)
- sequence of diagrams illustrating steps
- problem decomposition : remove and save first character
- subproblem solving : reverse the remaining string
- subsolution combination : append saved character to end of reversed string
- worked through cases inductively using paper sheets each representing a stack frame
- first sheet represented reversal of empty string; we all laughed (just like 1-disk Tower of Hanoi)
- second sheet (with first) represented reversal of single-character string; similar reaction
- third sheet (with first two) represented reversal of two-character string
- fourth sheet (with first three) represented reversal of three-character string
- we're feeling pretty confident at this point that it will work for "any" length string
Execution (stack) frames and recursion
- an execution frame is created upon each method call
- frame is then pushed to runtime stack, only to be popped upon method return
- yes, frames are created/pushed when a method calls itself recursively (otherwise you couldn't have recursion, right Fortran?)
- the generic structure of a frame was outlined (return address, return value, parameters, locals)
Recursive vs iterative: string reversal
- demo featuring automatically timed runs of three string reversal algorithms
- recursive, the one described above, that used String objects
- iterative, that used String objects
- iterative, that used StringBuffer objects
- the results were very interesting -- and will not be revealed here...
Recursive vs iterative: binary search
- similar demo of timed searches -- this required many repetitions to get measurable times
- searching was perfomed on array of String with comparisons using compareTo()
- a short discussion of Comparable interface ensued
- similarly compared iterative versus recursive binary search
- note that source code for iterative is very similar in structore to recursive, can convert from one to
the other by modifying fewer statements than there are fingers on one hand.
Recursive vs iterative: Fibonacci sequence
- brief review of how sequence is formed
- demo of timed program
- given a position in the sequence, method returned the value occupying that position
- compared three algorithms
- straightforward iterative
- straightforward recursive -- this was very short and elegant compared to iterative
- recursive coupled with technique from dynamic programming
- The recursion step will first check to see if this position in the sequence has already
been calculated.
- If so, it will return the previously-calculated value (which has been stored
in a list).
- Otherwise, it will recursively calculate the new value, then store it, then return it.
- This technique is called memoization
- Memoization requires additional space to store calculated values but oh how it runs!
- The timed results here were much more interesting than the others, and even exciting!
Backtracking: Eight Queens
- brief discussion of backtracking concept
- backtracking in solving maze games
- backtracking and the Eight Queens problem
- backtracking and searching solution space in general
- backtracking and artificial intelligence
- think of strategies used by chess-playing software
[ C
SC 205 | Peter
Sanderson | Math Sciences server
| Math Sciences home page
| Otterbein ]
Last updated:
Peter Sanderson (PSanderson@otterbein.edu)