210 likes | 291 Views
Problem of the Day. At what times do the minute and hour hands on an analog clock line up?. Problem of the Day. At what times do the minute and hour hands on an analog clock line up?. Lecture 21: Writing Recursive Solutions. Recursion.
E N D
Problem of the Day • At what times do the minute and hour hands on an analog clock line up?
Problem of the Day • At what times do the minute and hour hands on an analog clock line up?
Recursion re-cur-sion:Solving problems by breaking into identical, smaller problems andcombining solutions after base case(s) reached.
See Recursion Work • Recursive stepsimplifies problem to base case(s) • Recast using slightly easier version in recursive step4! = 4 *3! = 4 *(3 * 2!) = 4 * (3 * (2 * 1!)) • Base case(s) handle and solve obvious cases = 4 * (3 * (2 * 1)) • Solve in recursive step using results from base case= 4 * (3 * 2) = 4 * 6 = 24
Writing Recursive Code • No different than usual coding had been doing • At some point, recursive method calls itself • Parameters & locals in frame & so specific to each call • Once complete, execution returns to calling frame NOT
Starting Recursive Solution • Identify obvious solution needing little/no work • Should only need one or two operations to identify • Simple return statement(s) with literal or no value • Consider every input: can have many base case(s) • If you cannot determine, stop immediately • There are many problems without recursive solutions
Example Base Case(s) • Range of indices leaves 0 or 1 entries in array • If specific item searched for in array, stop at 0 entries • 1 entry used as base casewhen each item will be used • At last Node or null reference in Linked List • Just like with arrays, base case(s)are comparable • int reaches 0if working linearly through data • If multiplying int each pass, base caseat 1 • 0 or 1 characters left in a String
Starting Recursive Step • Determine state calling each of the base case(s) • These cases also easy to solve using base case result • Must be one step away from reaching base case(s) • Stand-alone base case may not have prior state • Identify single step advancing toward base case • Coding much easier when same step used in all cases • Know when each is used if different steps needed
Consider All Possible Inputs • Determine how to solve within recursive steps • Must check that solution works for all possible inputs • For each input you examine, must consider • Check that recursive call moves toward base case • No conflicting recursive steps that cycle within space
Consider All Possible Inputs • Determine how to solve within recursive steps • Must check that solution works for all possible inputs • For each input you examine, must consider • Check that recursive call moves toward base case • No conflicting recursive steps that cycle within space
Next to Last Step • Examine how method combines recursive result • No result for somerecursive solutions, so can skip this • Some other recursive solutions just return result • Others require even more work to be performed • When using recursion, fairly simple actions required • If many or complex actions required, then… • First ask yourself: is this solution actually recursive? • Could it be more recursive & simpler is next question
Last Step • Do not want to be locked into recursive solution • May need extra parameter to work correctly • Public method only has required parameters • Linked list instance or array to be processed • User is interested in testing some value • Use private method to hold the extra variable • Value or array index where method currently working • Node in linked listto be processed by method • Range or indices where method starts & stops
Check It Out public static intfindSum(int[] a) { }
Check It Out publicstatic intfindSum(int[] a) {return findSum(a, 0); } privatestatic intfindSum(int[] a, int j) { }
Check It Out publicstatic intfindSum(int[] a) {return findSum(a, 0); } privatestatic intfindSum(int[] a, int j) { if (j == a.length - 1) { return a[j];} }
Check It Out publicstatic intfindSum(int[] a) {return findSum(a, 0); } privatestatic intfindSum(int[] a, int j) { if (j == a.length - 1) { return a[j];} else {intnextJ = j + 1;} }
Check It Out publicstatic intfindSum(int[] a) {return findSum(a, 0); } privatestatic intfindSum(int[] a, int j) { if (j == a.length - 1) { return a[j];} else {intnextJ = j + 1;intsumRemain = findSum(a, nextJ);} }
Check It Out publicstatic intfindSum(int[] a) {return findSum(a, 0); } privatestatic intfindSum(int[] a, int j) { if (j == a.length - 1) { return a[j];} else {intnextJ = j + 1;intsumRemain = findSum(a, nextJ);intmyData = a[j]; return myData + sumRemain;} }
Your Turn • Get into your groups and complete activity
For Next Lecture • Read GT5.1 – 5.1.1, 5.1.4, 5.1.5 for next class • What is an ADT and how are they defined? • How does a Stack work? • Also available is week #8 weekly assignment • Programming assignment #1 also on Angel • Pulls everything together and shows off your stuff • Better get moving on it, since will be due within week