Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Programming in Java: Recursion

106 views

Published on

Programming in Java: Recursion

Published in: Education
  • See how I make over $7,293 a month from home doing REAL online jobs! ◆◆◆ http://t.cn/AisJWCv6
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Programming in Java: Recursion

  1. 1. REVISION Martin Chapman martin.chapman@kcl.ac.uk
  2. 2. •By demand, from highest to lowest: •Recursion •Break •Recursion •Objects and Classes •Break •Arrays and Array Lists
  3. 3. RECURSION Martin Chapman martin.chapman@kcl.ac.uk
  4. 4. What do we need to make a loop run? for (int i = 0; i<=10; i++) { System.out.println(i); }
  5. 5. for (int i = 0; i<=10; i++) { System.out.println(i); } What do we need to make a loop run? the ‘movement’
  6. 6. for (int i = 0; i<=10; i++) { System.out.println(i); } What do we need to make a loop run? the ‘change’
  7. 7. for (int i = 0; i<=10; i++) { System.out.println(i); } What do we need to make a loop run? ‘when to stop’
  8. 8. for (int i = 0; i<=10; i++) { System.out.println(i); } What do we need to make a loop run? the content
  9. 9. How do we loop without a loop?
  10. 10. How do we loop without a loop? the ‘movement’
  11. 11. the ‘change’ How do we loop without a loop?
  12. 12. ‘when to stop’ How do we loop without a loop?
  13. 13. the content How do we loop without a loop?
  14. 14. How do we loop without a loop? base case recursive case
  15. 15. Reverse, reverse. What happens here?
  16. 16. Reverse, reverse. methodA 3 3
  17. 17. Reverse, reverse. methodA 3 3 3 3 3
  18. 18. Reverse, reverse. methodA 3 3 3
  19. 19. Reverse, reverse. methodA 3 2 2 methodA 3
  20. 20. Reverse, reverse. methodA 3 2 ! ! 2 3 methodA
  21. 21. If we tell the compiler to execute non- conditional code then at some point it must be executed.
  22. 22. Reverse, reverse. methodA ! 3
  23. 23. Reverse, reverse. methodA 3 2 ! 2 methodA
  24. 24. Reverse, reverse. methodA 3 2 ! 2 methodA 2 2 2
  25. 25. Reverse, reverse. methodA 3 2 ! methodA 2 2
  26. 26. Reverse, reverse. methodA 3 2 ! methodA 1 1 methodA 2
  27. 27. Reverse, reverse. methodA 3 2 ! methodA 1 1 ! ! methodA 2
  28. 28. Reverse, reverse. methodA ! methodA ! 3 2
  29. 29. Reverse, reverse. methodA 1 ! methodA 0 ! methodA ! -1 methodA -1
  30. 30. Reverse, reverse. methodA 1 ! methodA 0 ! methodA ! -1 methodA -1 -1
  31. 31. Reverse, reverse. methodA 1 ! methodA 0 ! methodA ! -1 methodA End of the method, so return
  32. 32. Reverse, reverse. methodA 1 ! methodA 0 ! methodA ! -1 methodAbase case
  33. 33. Reverse, reverse. methodA ! methodA ! methodA ! methodA ! 0 1 2 3
  34. 34. Reverse, reverse. methodA ! methodA ! methodA ! methodA ! 0 1 2 3
  35. 35. Reverse, reverse. methodA ! methodA ! methodA ! methodA ! 0 1 2 3
  36. 36. Reverse engineering is fine, but how do I build something from scratch?
  37. 37. 0 1 2 3 4 5 6 7 8 9 10 0 1 1 2 3 5 8 13 21 34 55
  38. 38. 0 1 2 3 4 5 0 1 1 2 3 5 Fibonacci Sequence Every number is the sum of the two previous, when possible.nth fib
  39. 39. base case Ask, when do we not need to do any computation? When can we simply return a number rather than doing any addition? Determining the 0 1 2 3 4 5 0 1 1 2 3 5
  40. 40. Ask, when do we not need to do any computation? When can we simply return a number rather than doing any addition? Determining the 0 1 2 3 4 5 0 1 1 2 3 5 base case
  41. 41. 0 0
  42. 42. 0 1 2 3 4 5 base case Ask, when do we not need to do any computation? When can we simply return a number rather than doing any addition? Determining the 0 1 1 2 3 5
  43. 43. 1 1
  44. 44. 0 1 1 2 3 5 Determining the recursive case 0 1 2 3 4 5 What is the general case? Can this case be carried on indefinitely?
  45. 45. 0 1 1 2 3 5 0 1 2 3 4 5 Determining the + = recursive case What is the general case? Can this case be carried on indefinitely?
  46. 46. 0 1 1 2 3 5 0 1 2 3 4 5 Determining the + = recursive case + = What is the general case? Can this case be carried on indefinitely?
  47. 47. + 1 1 0 0 fib(0) fib(1) fib(2) =
  48. 48. static int fib(int n) { if (n == 0) { return 0; } if (n == 1) { return 1; } else { return fib(n-1) + fib(n-2); } } 2 1 0
  49. 49. Determining the recursive case 0 1 1 2 3 5 0 1 2 3 4 5 + = + = fib ( ) fib ( ) fib ( )
  50. 50. 0 1 2 3 4 5 fib ( ) fib ( ) fib ( ) Determining the recursive case 0 1 1 2 3 5+ = + =
  51. 51. 0 1 2 3 4 5 fib ( ) fib ( ) fib ( ) Determining the recursive case 0 1 1 2 3 5+ = + =
  52. 52. 0 1 5 fib ( n ) fib (n-2) fib (n-1) Determining the recursive case 0 1 1 2 3 5+ = + = This operation is consistent, so we can generalise.
  53. 53. 0 1 5 fib ( n ) fib (n-2) fib (n-1) Determining the recursive case 0 1 1 2 3 5+ = + =
  54. 54. static int fib(int n) { if (n == 0) { return 0; } if (n == 1) { return 1; } else { return fib(n-1) + fib(n-2); } }
  55. 55. static int fib(int n) { if (n == 0) { return 0; } if (n == 1) { return 1; } else { return fib(n-1) + fib(n-2); } } 5
  56. 56. Recursion creates a tree
  57. 57. 1 = actual value 1 0 1 1 0 01 1
  58. 58. 1 = actual value 1 0 1 1 0 01 + 1
  59. 59. 1 = actual value 1 0 1 1 0 01 + + 1
  60. 60. 1 = actual value 1 0 1 1 0 01 + + + ! ! 1
  61. 61. static int fib(int n) { if (n == 0) { return 0; } if (n == 1) { return 1; } else { return fib(n-1) + fib(n-2); } } !
  62. 62. 1 = actual value 1 0 1 1 0 01 + + + ! ! 1
  63. 63. 1 = actual value 1 0 1 1 0 01 + + + ! !! 1 +
  64. 64. 1 = actual value 1 0 1 1 0 01 1 + + + ! !! 1 +
  65. 65. 0 1 2 3 4 5 0 1 1 2 3 5 Fibonacci Sequence Every number is the sum of the two previous, when possible.
  66. 66. 1 = actual value 1 0 1 1 0 01 1 + + + ! !! 1 +
  67. 67. 1 = actual value 1 0 1 1 0 01 1 + + + ! !! 1 +
  68. 68. 1 = actual value 1 0 1 1 0 01 1 + + + 2 ! !! 1 +
  69. 69. 0 1 2 3 4 5 0 1 1 2 3 5 Fibonacci Sequence Every number is the sum of the two previous, when possible.
  70. 70. 1 = actual value 1 0 1 1 0 01 1 + + + 2 ! !! 1 +
  71. 71. 1 = actual value 1 0 1 1 0 01 1 + + + 2 ! !! + 1 +
  72. 72. 1 = actual value 1 0 1 1 0 01 1 + + + 2 ! ! 1 + 1 +
  73. 73. 1 = actual value 1 0 1 1 0 01 1 + + + 2 ! ! 1 + 1 +
  74. 74. 1 = actual value 1 0 1 1 0 01 1 + + + 2 ! ! 1 3 + 1 +
  75. 75. + 1 = actual value 1 0 1 1 0 01 1 + + + 2 ! ! 1 3 + + 1
  76. 76. 1 = actual value 1 0 1 1 0 01 1 + + + 2 ! ! 1 3 + ++ 1 +
  77. 77. 1 = actual value 1 0 1 1 0 01 1 + + + 2 ! 1 3 + ++ 1 1 +
  78. 78. + 1 = actual value 1 0 1 1 0 01 1 + + + 2 ! 1 3 + ++ 1 1
  79. 79. + 1 = actual value 1 0 1 1 0 01 1 + + + 2 1 3 + ++ 1 1 2
  80. 80. 1 = actual value 1 0 1 1 0 01 1 + + + 2 1 3 + ++ 1 1 2+
  81. 81. 1 = actual value 1 0 1 1 0 01 1 + + + 2 1 3 + ++ 1 1 2 5 +
  82. 82. static int fib(int n) { if (n == 0) { return 0; } if (n == 1) { return 1; } else { return fib(n-1) + fib(n-2); } } Think, when do I not have to do any computation? This will also be when my code stops. base case SUMMARY base case
  83. 83. static int fib(int n) { if (n == 0) { return 0; } if (n == 1) { return 1; } else { return fib(n-1) + fib(n-2); } } When I do have to compute something, what is the pattern? If I can prove it for one case, it should work for them all. recursive case SUMMARY base case base case
  84. 84. During the break...
  85. 85. SUMMARY • Recursion is just another way of looping, with information changing each time. • Recursion consists of a recursive call and a base case. • If there is code after a recursive call it must be executed and will be executed in reverse order. • Recursion is a natural solution if you can visualise the call in any instance i.e. Fibonacci numbers.
  86. 86. WILL I SEE RECURSION AGAIN? • Yes, sorry. • But it will give you more of a chance to understand it. • Modules which touch on recursion (at least): DST (Semester 2, 1st year) and Algorithm Modules.

×