1 / 21

Problem of the Day

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.

marcin
Download Presentation

Problem of the Day

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Problem of the Day • At what times do the minute and hour hands on an analog clock line up?

  2. Problem of the Day • At what times do the minute and hour hands on an analog clock line up?

  3. Lecture 21:Writing Recursive Solutions

  4. Recursion re-cur-sion:Solving problems by breaking into identical, smaller problems andcombining solutions after base case(s) reached.

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. Check It Out public static intfindSum(int[] a) { }

  15. Check It Out publicstatic intfindSum(int[] a) {return findSum(a, 0); } privatestatic intfindSum(int[] a, int j) { }

  16. 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];} }

  17. 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;} }

  18. 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);} }

  19. 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;} }

  20. Your Turn • Get into your groups and complete activity

  21. 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

More Related