 Download Download Presentation Recursion

# Recursion

Télécharger la présentation ## Recursion

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. Recursion 0 • Phone books • Iteration • Recursion • Code examples • Box trace

2. Phone Book Problem How do you look up someone’s phone number in a phone book? At least two different algorithms (plans) to do this

3. Algorithm #1 • Look for the number on page 1 • If can’t find the number, look for the number on page 2 • If can’t find the number, look for the number on page 3 • And keep repeating this until the number is found

4. Algorithm #2 Open the phone book at the halfway point If can’t find number, open the 1st or 2nd half of the phone book at its halfway point Keep repeating this until the number is found

5. Terminology The 1st algorithm is an example of iteration Iteration is simple repetition or looping In computer programs, we can do iteration with a for or whileloop

6. Terminology The 2nd algorithm is an example of recursion Recursion breaks a problem into smaller subproblems until a stopping case is reached In computer programs, we can do recursion with function calls and ifstatements

7. Recursive Algorithm A more detailed algorithm for looking up a number in the phone book recursively Open the phone book at the halfway point Check the top of the page for the person’s name

8. Recursive Algorithm Base case (if name is on this page) Search a single page of the phone book for the person’s name and then get number

9. Recursive Algorithm Recursive case (if name is not on this page) If the name is before this page, start the algorithm again using the 1st part of the phonebook If the name is after this page, start the algorithm again using the 2nd part of the phonebook

10. General Characteristics Two basic steps to writing recursive functions Base case ends the function May have more than 1 base case

11. General Characteristics Recursive case calls itself Same function call May have more than 1 recursive case Recursive case creates smaller problem Recursive case eventually calls base case

12. How can we count 1 to 3? Iterative algorithm Loop from 1 to 3 While looping, display each digit See example program: recursion.c Counting Example 1,2,3

13. Counting Example How can we count 1 to 3? Recursive algorithm Base case: if the starting and ending numbers are equal, display the number Recursive case: display the starting number, add one to the starting number, and call the function again 1,2,3

14. How Does It Work? So how exactly does recursion work? We need to go over the basic structure of a computer And then go over how a program works Doing this will help you understand how to write recursive functions and how to trace recursive functions

15. 3 Parts of a Computer

16. 3 Parts of a Computer CPU – central processing unit Executes only one (1) instruction at a time

17. 3 Parts of a Computer Memory storage unit Stores the executing computer program Instructions & data must be piped (sent) to CPU and back via a bus (group of wires) Note that this is different than “secondary storage” such a disk drive

18. 3 Parts of a Computer I/O (input/output) devices Monitor, keyboard, mouse, etc.

19. 3 Parts of Program in Memory • The memory storage unit stores running programs • Divides the running program into 3 basic parts with 2 subdivisions Stack Segment Data Segment Code Segment runtime stackheap

20. 3 Parts of Program in Memory Data segment Contains global data (static variables) Code segment Contains the program code (machine language)

21. 3 Parts of Program in Memory Stack segment Top: Contains runtime stack (stack of activation records of active functions) Stores function’s local environment (return address, arguments, local variables, return value) Bottom: Contains the heap (can be used to store data)

22. How To Trace Function Calls Label all the function calls in the program code with the letters A, B, C, D, E, … Each letter represents the return address of each function

23. Return Address The return address is where program control returns to after a function ends execution Program control keeps track of the next instruction that will be executed

24. How To Trace Function Calls • Draw stack of boxes – one (1) box for each time a function is called by the executing program • This diagram shows 3 recursive function calls for function1() function1’s data function1’s data function1’s data

25. Runtime Stack • This stack of boxes represents the runtime stack in the stack segment in memory • Each box contains the data for the function’s local environment function1’s data function1’s data function1’s data

26. Local Environment 1. return address2. arguments 3. local variables4. return value 1. return address2. arguments 3. local variables4. return value 1. return address2. arguments 3. local variables4. return value Function’s local environment return address (step #1 labels) arguments (parameters) local variables return value

27. Activation Record Instance 1. return address2. arguments 3. local variables4. return value 1. return address2. arguments 3. local variables4. return value 1. return address2. arguments 3. local variables4. return value Another name for a function’s local environment is activation record instance (ARI) This diagram shows 3 ARIs for function1()

28. Example Box Trace See function recursiveAdd()in program recursion.c When we draw the runtime stack of ARI (activation record instances) of a running program, this is also called a box trace Basically, we are drawing boxes filled with data to help us understand how recursion works

29. Return Address Labels In the program code, type the return address labels for each function call See the comments in the program recursion.c This program has return address labels A, B, and Cto identify the function calls in the program

30. Return Values Usually when we write functions, we have a return value, so that the function can return the result of a calculation For example, see the program recursion.c, which has an iterative and a recursive function that add: 1 + 2 + 3 = 6, and return this value to the main() function

31. Box Trace Let’s do a box trace for the iterative function loopAdd() We already have return address label A for the function call As the program runs, we will draw a box (ARI = Activation Record Instance) for each function call

32. Box Trace first = 1 last = 3 result1 = 0 result2 = 0 return value = 0 • Program code • Program control is on line #19 with return address A • Once this function is finished, the program will assign the return value to local variable result1 Runtime stack

33. Box Trace first = 1 last = 3 result1 = 0 result2 = 0 return value = 0 return address=Astart = 1 end = 3 Program code The function call pushes return address A and arguments 1 and 3 to the runtime stack Program control jumps to line #28 Runtime stack

34. Box Trace first = 1 last = 3 result1 = 0 result2 = 0 return value = 0 return address=Astart = 1 end = 3i = 0 result = 0 Program code By line #29, two local variables (i and result) are added to the stack Runtime stack

35. Box Trace first = 1 last = 3 result1 = 0 result2 = 0 return value = 0 return address=Astart = 1 end = 3i = 3 result = 1 + 2 + 3 = 6 Program code Lines #30-32 loop 3 times, and change the values of local variables i and result Runtime stack

36. Box Trace first = 1 last = 3 result1 = 6 result2 = 0 return value = 0 Program code Lines #33 returns the value of result to the main() function at return address A (line #19) This return value is assigned to local variable result1 Runtime stack

37. Box Trace first = 1 last = 3 result1 = 6 result2 = 0 return value = 0 Program code At line #19, the ARI for function loopAdd() is popped off the runtime stack At line #20, the value of result1 is outputted Runtime stack

38. Box Trace Let’s do a box trace for the recursive function recursiveAdd() We already have return address label B & C for the function calls As the program runs, we will draw a box (ARI = Activation Record Instance) for each function call

39. Box Trace first = 1 last = 3 result1 = 6 result2 = 0 return value = 0 • Program code • Program control is on line #21 with return address B • Once this function is finished, the program will assign the return value to local variable result2 Runtime stack

40. Box Trace first = 1 last = 3 result1 = 6 result2 = 0 return value = 0 return address=Bstart = 1 end = 3 Program code The function call pushes return address B and arguments 1 and 3 to the runtime stack Program control jumps to line #37 Runtime stack

41. Box Trace first = 1 last = 3 result1 = 6 result2 = 0 return value = 0 return address=Bstart = 1 end = 3result = 0 Program code On line #37, local variable result is pushed on the stack The if-statement on line #39 is false Program control jumps to line #43 Runtime stack

42. Box Trace first = 1 last = 3 result1 = 6 result2 = 0 return value = 0 return address=Bstart = 1 end = 3 result = 0 return address=Cstart = 2 end = 3 Program code On line #44, a function call to recursiveAdd() pushes return address C and arguments 2 and 3 to the stack Program control jumps to line #37

43. Box Trace first = 1 last = 3 result1 = 6 result2 = 0 return value = 0 return address=Bstart = 1 end = 3 result = 0 return address=Cstart = 2 end = 3 result = 0 Program code On line #37, local variable result is pushed on the stack The if-statement on line #39 is false Program control jumps to line #43

44. Box Trace first = 1 last = 3 result1 = 6 result2 = 0 return value = 0 return address=Bstart = 1 end = 3 result = 0 return address=Cstart = 2 end = 3 result = 0 return address=Cstart = 3 end = 3 Program code On line #44, a function call to recursiveAdd() pushes return address C and arguments 3 and 3 to the stack Program control jumps to line #37

45. Box Trace first = 1 last = 3 result1 = 6 result2 = 0 return value = 0 return address=Bstart = 1 end = 3 result = 0 return address=Cstart = 2 end = 3 result = 0 return address=Cstart = 3 end = 3 result = 3 Program code On line #37, local variable result is pushed on the stack The if-statement on line #39 is true, so result is assigned to end

46. Box Trace first = 1 last = 3 result1 = 6 result2 = 0 return value = 0 return address=Bstart = 1 end = 3 result = 0 return address=Cstart = 2 end = 3 result = 0 return address=Cstart = 3 end = 3 result = 3 Program code Program control jumps to line #46 The return statement returns the value of result to return address C (line #44) & pops the ARI off the stack

47. Box Trace first = 1 last = 3 result1 = 6 result2 = 0 return value = 0 return address=Bstart = 1 end = 3 result = 0 return address=Cstart = 2 end = 3 result = 2 + 3 = 5 Program code On line #44, the return value of recursiveAdd() (value = 3)is added to variable start (value = 2)

48. Box Trace first = 1 last = 3 result1 = 6 result2 = 0 return value = 0 return address=Bstart = 1 end = 3 result = 0 return address=Cstart = 2 end = 3 result = 5 Program code On line #46, the return statement returns the value of result to return address C (line #44) & pops the ARI off the stack

49. Box Trace first = 1 last = 3 result1 = 6 result2 = 0 return value = 0 return address=Bstart = 1 end = 3 result = 1 + 5 = 6 Program code On line #44, the return value of recursiveAdd() (value = 5)is added to variable start (value = 1) Runtime stack

50. Box Trace first = 1 last = 3 result1 = 6 result2 = 0 return value = 0 return address=Bstart = 1 end = 3 result = 6 Program code On line #46, the return statement returns the value of result to return address B (line #21) in the main() function & pops the ARI off the stack Runtime stack