recursion
play

Recursion Tessema M. Mengistu Department of Computer Science - PowerPoint PPT Presentation

Recursion Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale tessema.mengistu@siu.edu Room - 3131 1 Outline What Is Recursion? Tracing a Recursive Method Recursive Methods that Return a


  1. Recursion Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale tessema.mengistu@siu.edu Room - 3131 1

  2. Outline • What Is Recursion? • Tracing a Recursive Method • Recursive Methods that Return a Value • Recursively Processing an Array • Recursively Processing a Linked List • The Time Efficiency of Recursive Methods 2

  3. Recursion • Repetition is a major feature of many algorithms – Iterative – Recursive • We often solve a problem by breaking it into smaller problems – divide and conquer • When the smaller problems are identical (except for size) – This is called “recursion” • Repeated smaller problems – Until problem with known solution is reached 3

  4. Recursion • Example – factorial of a positive number n fact(4) = 4 * fact(3) = 24 3* fact(2) = 6 2 *fact(1) = 2 fact(1) =1 • In general fact(n) = n* fact(n-1) n-1*fact(n-2) . . . 2*fact(1) 4

  5. Recursion • Recursion is a problem solving process that breaks a problem into identical but smaller problems. • A method that calls itself is a recursive method. • The invocation is a recursive call or recursive invocation. 5

  6. Recursion • Example 6

  7. Design of Recursive Solution • What part of solution can you contribute directly? • What smaller (identical) problem has solution that … – When taken with your contribution – Provides the solution to the original problem • When does process end? – What smaller but identical problem has known solution – Have you reached this problem, or base case? 7

  8. Design Guidelines • Method must receive input value • Must contain logic that involves this input value and leads to different cases • One or more cases should provide solution that does not require recursion – Base case or stopping case • One or more cases must include recursive invocation of method – recursion call 8

  9. Recursive Method the returns a value • Example – Write a recursive method that displays the sum of the first n positive integers – Compute the sum 1 + 2 + . . . + n 9

  10. Design Guidelines • A recursive method that does not check for a base case, or that misses the base case is a logic error – Infinite recursion 10

  11. Tracing Recursive Method • Given recursive countDown method • For 3 11

  12. 12

  13. Tracing Recursive Method 13

  14. Tracing Recursive Method • A recursive method uses more memory than an iterative method, in general, because each recursive call generates an activation record. – Results in “stack overflow” – Infinite recursion or large-size problems are the likely causes of this error 14

  15. Display an Array Recursively • Using first element • Using last element 15

  16. Display an Array Recursively • Using the middle element 16

  17. Displaying a Bag Recursively • When implemented with an array 17

  18. Recursively Processing a Linked Chain • Method display , implemented recursively 18

  19. Recursively Processing a Linked Chain • Recursive method to display the linked chain in reverse order 19

  20. Time Efficiency of Recursive Methods • Consider the method countDown() that displays all positive numbers up to n • It has time complexity of 20

  21. Time Efficiency of Recursive Methods • The equation for t(n) is called a recurrence relation – The definition of the function t contains an occurrence of itself • How can we proof t(n) = n? – Proof by induction • Assume t(n-1) = n-1 for n>1 • t(n) = 1+t(n-1) = 1 +n-1 =n • Therefore t(n) = n for n>1 – So countDown() is O(n) 21

  22. Time Efficiency of Computing x n • Recursive calculation for x n • Can be shown • Thus efficiency is O(log n) • Proof – Reading Assignment 22

  23. Simple Solution to a Difficult Problem • Consider Towers of Hanoi puzzle 23

  24. Towers of Hanoi • Rules 1. Move one disk at a time. Each disk you move must be a topmost disk. 2. No disk may rest on top of a disk smaller than itself. 3. You can store disks on the second pole temporarily, as long as you observe the previous two rules. 24

  25. 25

  26. 26

  27. Solution • Move a disk from pole 1 to pole 3 • Move a disk from pole 1 to pole 2 • Move a disk from pole 3 to pole 2 • Move a disk from pole 1 to pole 3 • Move a disk from pole 2 to pole 1 • Move a disk from pole 2 to pole 3 • Move a disk from pole 1 to pole 3 27

  28. Recursive Solution • To solve for n disks … – Ask friend to solve for n – 1 disks – He in turn asks another friend to solve for n – 2 – Etc. – Each one lets previous friend know when their simpler task is finished 28

  29. 3 29

  30. Towers of Hanoi 30

  31. Towers of Hanoi 31

  32. Algorithm Efficiency • Moves required for n disks • We note and conjecture (proved by induction) 32

  33. Algorithm Efficiency • The number of moves required to solve the Towers of Hanoi problem grows exponentially with the number of disks n. – m(n) = O(2 n ) 33

  34. Poor Solution to a Simple Problem • Fibonacci sequence 1, 1, 2, 3, 5, 8, 13, … 34

  35. Poor Solution to a Simple Problem • A recursive solution Note the two recursive calls 35

  36. 36

  37. Tail Recursion • When the last action performed by a recursive method is a recursive call • Example: • Repeats call with change in parameter or variable

  38. Indirect Recursion • Consider chain of events – Method A calls Method B – Method B calls Method C – and Method C calls Method A • Mutual recursion – Method A calls Method B – Method B calls Method A 38

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend