Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE 116/504 – Intro. To Computer Science for Majors II

Similar presentations


Presentation on theme: "CSE 116/504 – Intro. To Computer Science for Majors II"— Presentation transcript:

1 CSE 116/504 – Intro. To Computer Science for Majors II
Lecture 27: Recursion Pictured used under a Creative Commons license thanks to robad0b. Original file available at:

2 Announcements Project phase #2 due tonight at 11:59PM
Submit in Autolab; need to submit ZIP file with code Do NOT add partners to submission; does not work well In your recitations this week are self & peer evaluations Project demos to TAs next week to show what you did Test #2 in 8 days (Tuesday, November 7th) Second test will covers material through this week Let me know of conflicts – make-up is following day

3 “A journey begins with single step”

4 “A journey begins with single step”
Picture used under a Creative Commons License thanks to neilalderney123. Original file available at:

5 “A journey begins with single step”
This file is licensed under the Creative Commons Attribution-Share Alike 2.5 Generic license. Image was created by Asier Solana Bermejo and is available at:

6 Solving Problems Large problems hard and scary to even start
Find working on small, easy problems much nicer Divide large problems into smaller ones for the win Before you start coding, write JUnit test cases

7 Solving Problems Large problems hard and scary to even start
Find working on small, easy problems much nicer Divide large problems into smaller ones for the win Before you start coding, write JUnit test cases Split long methods to combine smaller method results Move repeated actions into small (private) methods

8 Smaller is Better

9 Smaller is Better CENSORED

10 Smaller is Better Easier to use, quicker to finish, many fewer bugs

11 VERY, VERY CENSORED Smaller is Better
Easier to use, quicker to finish, many fewer bugs VERY, VERY CENSORED

12 Method Basics Cannot cure cancer

13 Cannot cure cancer but great solving 1 problem
Method Basics Cannot cure cancer but great solving 1 problem

14 Assume methods magic when calling them
Programming Pro Tip #1 Assume methods magic when calling them

15 Method Coding Pro Tip #2

16 clear inputs, simple actions,
Method Pro Tip #2 Write boring methods: clear inputs, simple actions, predictable results, solving 1 problem

17 Warning

18 Recursion re-cur-sion: Method of solving problem by combining solutions to identical, smaller problems

19 Recursion re-cur-sion: Method of solving problem by combining solutions to identical, smaller problems

20 See Recursion Work Recursive step simplifies problem to base case(s)
Use easy action to simplify problem in recursive step 4! = 4 * 3!

21 See Recursion Work Recursive step simplifies problem to base case(s)
Use easy action to simplify problem in recursive step 4! = 4 * 3! = 4 * (3 * 2!)

22 See Recursion Work Recursive step simplifies problem to base case(s)
Use easy action to simplify problem in recursive step 4! = 4 * 3! = 4 * (3 * 2!) = 4 * (3 * (2 * 1!))

23 See Recursion Work Recursive step simplifies problem to base case(s)
Use easy action to simplify problem in recursive step 4! = 4 * 3! = 4 * (3 * 2!) = 4 * (3 * (2 * 1!)) Base case(s) handle and solve obvious cases

24 See Recursion Work Recursive step simplifies problem to base case(s)
Use easy action to simplify problem in recursive step 4! = 4 * 3! = 4 * (3 * 2!) = 4 * (3 * (2 * 1!)) Base case(s) handle and solve obvious cases = 4 * (3 * (2 * 1))

25 See Recursion Work Recursive step simplifies problem to base case(s)
Use easy action to simplify problem in recursive step 4! = 4 * 3! = 4 * (3 * 2!) = 4 * (3 * (2 * 1!)) Base case(s) handle and solve obvious cases = 4 * (3 * (2 * 1))

26 See Recursion Work Recursive step simplifies problem to base case(s)
Use easy action to simplify problem in recursive step 4! = 4 * 3! = 4 * (3 * 2!) = 4 * (3 * (2 * 1!)) Base case(s) handle and solve obvious cases = 4 * (3 * (2 * 1)) Simple action combines & completes recursive step = 4 * (3 * 2)

27 See Recursion Work Recursive step simplifies problem to base case(s)
Use easy action to simplify problem in recursive step 4! = 4 * 3! = 4 * (3 * 2!) = 4 * (3 * (2 * 1!)) Base case(s) handle and solve obvious cases = 4 * (3 * (2 * 1)) Simple action combines & completes recursive step = 4 * (3 * 2) = 4 * 6

28 See Recursion Work Recursive step simplifies problem to base case(s)
Use easy action to simplify problem in recursive step 4! = 4 * 3! = 4 * (3 * 2!) = 4 * (3 * (2 * 1!)) Base case(s) handle and solve obvious cases = 4 * (3 * (2 * 1)) Simple action combines & completes recursive step = 4 * (3 * 2) = 4 * = 24

29 Recursion Key Concept #1
Base case(s) clear & easy to solve

30 Recursion Key Concept #2
Recursive step solves 1 problem using call to itself

31 Assume method magic when calling it
Recursion Tip #1 Assume method magic when calling it

32 Assume method magic when calling it
Recursion Tip #1 Assume method magic when calling it

33 For Recursion To Work

34 For Recursion To Work Very easy to create solution that does not work
Infinite recursion occurs if base case never reached Frame-by-frame stack grows from method calls StackOverflowError thrown by program

35 For Recursion To Work Very easy to create solution that does not work
Recursive step must advance toward base case Not curing cancer: one step closer each recursive call Check all possible inputs to verify nothing overlooked

36 Recursion in Java A method is recursive if it calls itself:
public static int factorial(int i) { if (i <= 1) { return 1; } else { int nextI = i – 1; int result = factorial(nextI); return i * result; } }

37 Recursion in Java A method is recursive if it calls itself:
public static int factorial(int i) { if (i <= 1) { return 1; } else { int nextI = i – 1; int result = factorial(nextI); return i * result; } } Base case: Solution is simple

38 Recursion in Java A method is recursive if it calls itself:
public static int factorial(int i) { if (i <= 1) { return 1; } else { int nextI = i – 1; int result = factorial(nextI); return i * result; } } Recursive Step: Take 1 step to solution

39 Recursion in Java A method is recursive if it calls itself:
public static int factorial(int i) { if (i <= 1) { return 1; } else { int nextI = i – 1; int result = factorial(nextI); return i * result; } } Recursive Step: Take 1 step to solution Includes 1 or more recursive calls

40 Recursion in Java A method is recursive if it calls itself:
public static int factorial(int i) { if (i <= 1) { return 1; } else { int nextI = i – 1; int result = factorial(nextI); return i * result; } } Recursive Step: Take 1 step to solution Includes 1 or more recursive calls (Simple process generates final result)

41 Recursive Method Basics
Start with check for base case(s) These cases must return blatantly obvious answer 1+ recursive calls found within recursive step(s) Should write code assuming recursive call works

42 Recursive Method Basics
Start with check for base case(s) These cases must return blatantly obvious answer 1+ recursive calls found within recursive step(s) Should write code assuming method works

43 Recursive Method Basics
Start with check for base case(s) These cases must return blatantly obvious answer 1+ recursive calls found within recursive step(s) Should write code assuming method works

44 Recursive Method Basics
Start with check for base case(s) These cases must return blatantly obvious answer 1+ recursive calls found within recursive step(s) Should write code assuming method works Take 1 step advancing toward base case

45 Recursive Step Key Concept

46 Recursion Method Calls
No different than methods using all along When method called, computer adds frame for call Frame for that call stores its locals & parameters (Processors also include line being executed)

47 Recursion Method Calls
No different than methods using all along When method called, computer adds frame for call Frame for that call stores its locals & parameters NOT Picture of the Nobel Prize has been released as public domain. Original can be downloaded from:

48 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 Transition to next slide with picture of infinite recursion

49 Example Base Case(s) int reaches 0 if working linearly through data
If multiplying int each pass, base case at 1 0 or 1 characters left in a String 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 case when each item will be used At last Node or null reference in linked list Just like with arrays, base case(s) are comparable

50 Example Base Case(s) int reaches 0 if working linearly through data
If multiplying or dividing ints, base case at 1 0 or 1 characters left in a String 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

51 Example Recursive Method
public static int factorial(int i) { if (i <= 1) { return 1; } else { int nextI = i – 1; int result = factorial(nextI); return i * result; } } Base case: Integer reaches 0

52 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 Identify when each is used if different steps needed

53 Example Recursive Method
public static int factorial(int i) { if (i <= 1) { return 1; } else { int nextI = i – 1; int result = factorial(nextI); return i * result; } } Recursive Step: Take 1 step towards base case

54 Example Base Case(s) int reaches 0 if working linearly through data
If multiplying int each pass, base case at 1 0 or 1 characters left in a String 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 case when each item will be used At last Node or null reference in Linked List Just like with arrays, base case(s) are comparable

55 Does This Work? Yes, this finds the smallest value
static int findMin(int[] a, int j) { if (j == a.length - 1) { return a[j]; } else { int minLeft = findMin(a, j++); return Math.min(a[j], minLeft); } } Yes, this finds the smallest value No, cannot use recursion with array No, will never reach base case Maybe, but only if smallest entry at index 0 1

56 Convince your neighbor your answer is correct
Does This Work? static int findMin(int[] a, int j) { if (j == a.length - 1) { return a[j]; } else { int minLeft = findMin(a, j++); return Math.min(a[j], minLeft); } } Convince your neighbor your answer is correct Yes, this finds the smallest value No, cannot use recursion with array No, will never reach base case Maybe, but only if smallest entry at index 0 1

57 Does This Work? Yes, this finds the smallest value
static int findMin(int[] a, int j) { if (j == a.length - 1) { return a[j]; } else { int minLeft = findMin(a, j++); return Math.min(a[j], minLeft); } } Yes, this finds the smallest value No, cannot use recursion with array No, will never reach base case Maybe, but only if smallest entry at index 0 1

58 Cannot resize array; but parameter tracks current position
Does This Work? static int findMin(int[] a, int j) { if (j == a.length - 1) { return a[j]; } else { int minLeft = findMin(a, j++); return Math.min(a[j], minLeft); } } Cannot resize array; but parameter tracks current position Yes, this finds the smallest value No, cannot use recursion with array No, will never reach base case Maybe, but only if smallest entry at index 0 1

59 Does This Work? Contains serious bug;
static int findMin(int[] a, int j) { if (j == a.length - 1) { return a[j]; } else { int minLeft = findMin(a, j++); return Math.min(a[j], minLeft); } } Contains serious bug; Yes, this finds the smallest value No, cannot use recursion with array No, will never reach base case Maybe, but only if smallest entry at index 0 1

60 Contains serious bug; increment only occurs AFTER call
Does This Work? static int findMin(int[] a, int j) { if (j == a.length - 1) { return a[j]; } else { int minLeft = findMin(a, j++); return Math.min(a[j], minLeft); } } Contains serious bug; increment only occurs AFTER call Yes, this finds the smallest value No, cannot use recursion with array No, will never reach base case Maybe, but only if smallest entry at index 0 1

61 Must be VERY careful with ++ & -- in recursion
Does This Work? static int findMin(int[] a, int j) { if (j == a.length - 1) { return a[j]; } else { int minLeft = findMin(a, j++); return Math.min(a[j], minLeft); } } Must be VERY careful with ++ & -- in recursion Yes, this finds the smallest value No, cannot use recursion with array No, will never reach base case Maybe, but only if smallest entry at index 0 1

62 Recursion Key Concept #1
Base case(s) clear & easy to solve

63 Recursion Key Concept #2
Recursive step solves 1 problem using call to itself

64 Assume method magic when calling it
Recursion Tip #1 Assume method magic when calling it

65 Recursive Step Key Concept

66 For Next Lecture Make certain 5.1 – 5.8 read for Wed. recursion
Wednesday's reading assignment includes fun web story Week#10 deadline (Mon. 12:45PM) still unchanged Do not procrastinate – phase #2 due tonight!


Download ppt "CSE 116/504 – Intro. To Computer Science for Majors II"

Similar presentations


Ads by Google