Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ics212-11-recursion

William Albritton
October 05, 2015
86

 ics212-11-recursion

William Albritton

October 05, 2015
Tweet

Transcript

  1. Memory Allocation  Iteration  Recursion  Adding code example

     Runtime Stack  Fibonacci numbers code example
  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 1. Look for the person’s name on page

    1 2. If can’t find the name, look for the name on page 2 3. If can’t find the name, look for the name on page 3 4. And keep repeating this until the name is found on a page
  4. Iteration  The 1st algorithm is an example of iteration

     Iteration is simple repetition or looping  In computer programs, we can do iteration with a for, while, or do-while loop
  5. Algorithm #2 1. Open the phone book at the halfway

    point 2. If the person’s name appears on that page, you have found the person (base case) 3. Otherwise, go back to step #1: open the 1st or 2nd part of the phone book at its halfway point (recursive case)
  6. Recursion  The 2nd algorithm is an example of recursion

     Recursion breaks a problem into smaller sub-problems until a stopping case is reached  In computer programs, we can do recursion with if-statements and function calls
  7. General Characteristics  Two basic steps to writing recursive functions

    1. Base case ends the function  Uses an if-statement to see if a variable equals a specific value (or another variable) and returns a value (or variable)  The base case is written first, then the recursive case  May have more than one base case
  8. General Characteristics 2. Recursive case calls itself  Returns the

    same function call  Recursive case creates smaller problem, by subtracting, adding, or somehow getting closer to the base case  Recursive case eventually calls base case  May have more than one recursive case
  9. Recursion Example  How can we add the numbers from

    1 to 3?  Base case:  If the first number is equal to the last number, return either number.  Recursive case:  Return the first number added to the function call.  Also be sure to add one to the first number and use it as the argument in the function call.  The second argument is the last number. 1+2+3=6
  10. Recursion Code  Base case (see program at recursion.c) int

    recursiveAdd(int start, int end){ int result = 0; //base case if(start == end){ result = end; }
  11. Recursion Code  Recursive case //recursive case else{ result =

    start + recursiveAdd(start + 1, end); } return result; }
  12. 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
  13. 3 Parts of a Computer 1. CPU – central processing

    unit  Executes only one (1) instruction at a time
  14. 3 Parts of a Computer 2. 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
  15.  The memory storage unit stores running programs  Divides

    the running program into 3 basic parts Program in Memory Data Segment Code Segment Stack Segment
  16.  Two parts to the stack segment: 1. Runtime stack

    on top 2. Heap on bottom Program in Memory Data Segment Code Segment runtime stack heap Stack Segment
  17. Program in Memory 1. Data segment  Contains global data

    (static variables) 2. Code segment  Contains the program code (machine language)
  18. Program in Memory 3. Stack segment A. Top: Contains runtime

    stack (stack of activation records instances (ARIs) of active functions), which stores each function’s local environment (return address, arguments, local variables, return value) B. Bottom: Contains the heap (a place to store data by using functions malloc() and free() in your program)
  19. Runtime Stack Representation  Each box contains the data of

    each function’s local environment  Has activation record instances (ARIs) for three functions 1. return address 2. arguments (actual parameters) 3. local variables 4. return value 1. return address 2. arguments (actual parameters) 3. local variables 4. return value 1. return address 2. arguments (actual parameters) 3. local variables 4. return value
  20. 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  See the comments in the program recursion.c  This program has return address labels A, B, and C to identify the function calls in the program
  21. 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
  22. Box Trace  Runtime stack first = 1 last =

    3 result1 = 6 result2 = 0 return value = 0 int first = 1; int last = 3; int result1 = 0; int result2 = 0; result1 = loopAdd( first, last); result2 = recursiveAdd( first, last); //return address B
  23. Box Trace  Runtime stack first = 1 last =

    3 result1 = 6 result2 = 0 return value = 0 return address = B start = 1 end = 3 result2 = recursiveAdd( first, last); //return address B int recursiveAdd(int start, int end){
  24. Box Trace  Runtime stack first = 1 last =

    3 result1 = 6 result2 = 0 return value = 0 return address = B start = 1 end = 3 result = 0 int recursiveAdd( int start, int end){ int result = 0; if(start == end){ result = end; } else{ result = start + recursiveAdd( start + 1, end); //return address C } return result;}
  25.  Runtime stack Box Trace first = 1 last =

    3 result1 = 6 result2 = 0 return value = 0 return address = B start = 1 end = 3 result = 0 int recursiveAdd( int start, int end){ int result = 0; if(start == end){ result = end; } else{ result = start + recursiveAdd( start + 1, end); //return address C } return result;}
  26. Box Trace return address = C start = 2 end

    = 3 first = 1 last = 3 result1 = 6 result2 = 0 return value = 0 return address = B start = 1 end = 3 result = 0 int recursiveAdd( int start, int end){ int result = 0; if(start == end){ result = end; } else{ result = start + recursiveAdd( start + 1, end); //return address C } return result;}
  27. Box Trace return address = C start = 2 end

    = 3 result = 0 first = 1 last = 3 result1 = 6 result2 = 0 return value = 0 return address = B start = 1 end = 3 result = 0 int recursiveAdd( int start, int end){ int result = 0; if(start == end){ result = end; } else{ result = start + recursiveAdd( start + 1, end); //return address C } return result;}
  28. Box Trace int recursiveAdd( int start, int end){ int result

    = 0; if(start == end){ result = end; } else{ result = start + recursiveAdd( start + 1, end); //return address C } return result;}
  29. Box Trace return address = C start = 3 end

    = 3 result = 3 int recursiveAdd( int start, int end){ int result = 0; if(start == end){ result = end; } else{ result = start + recursiveAdd( start + 1, end); //return address C } return result;} return address = C start = 2 end = 3 result = 0 first = 1 last = 3 result1 = 6 result2 = 0 return value = 0 return address = B start = 1 end = 3 result = 0
  30. return address = C start = 2 end = 3

    result = 0 first = 1 last = 3 result1 = 6 result2 = 0 return value = 0 return address = B start = 1 end = 3 result = 0 Box Trace 2 + 3 = 5 int recursiveAdd( int start, int end){ int result = 0; if(start == end){ result = end; } else{ result = start + recursiveAdd( start + 1, end); //return address C } return result;}
  31. return address = C start = 2 end = 3

    result = 0 first = 1 last = 3 result1 = 6 result2 = 0 return value = 0 return address = B start = 1 end = 3 result = 0 Box Trace int recursiveAdd( int start, int end){ int result = 0; if(start == end){ result = end; } else{ result = start + recursiveAdd( start + 1, end); //return address C } return result;} result = 2 + 3 = 5 5
  32. first = 1 last = 3 result1 = 6 result2

    = 0 return value = 0 return address = B start = 1 end = 3 result = 0 Box Trace 1 + 5 = 6 int recursiveAdd( int start, int end){ int result = 0; if(start == end){ result = end; } else{ result = start + recursiveAdd( start + 1, end); //return address C } return result;} return result;
  33. first = 1 last = 3 result1 = 6 result2

    = 0 return value = 0 return address = B start = 1 end = 3 result = 0 Box Trace 6 int recursiveAdd( int start, int end){ int result = 0; if(start == end){ result = end; } else{ result = start + recursiveAdd( start + 1, end); //return address C } return result;}
  34. first = 1 last = 3 result1 = 6 result2

    = 0 return value = 0 Box Trace int first = 1; int last = 3; int result1 = 0; int result2 = 0; result1 = loopAdd( first, last); result2 = recursiveAdd( first, last); //return address B 6
  35. Fibonacci Numbers  Add previous two numbers  0, 1,

    1, 2, 3, 5, 8, 13, 21, 34, 55, ...  Known in India at least 1000 years ago  Named after Leonardo Fibonacci who used the series to predict breeding rates of rabbits in 1202
  36. Fibonacci Numbers in Nature  Fibonacci numbers are often seen

    in nature  Idealized ancestry of a male bee  Scales of certain pinecones  Sunflower floret spirals  Pineapple leaves
  37. Fibonacci Numbers Definition  Fibonacci numbers in terms of n

     f 0 = 0, f 1 = 1, f 2 = 1, f 3 = 2, f 4 = 3, f 5 = 5, f 6 = 8, f 7 = 13, f 8 = 21, f 9 = 34, f 10 = 55, …  Recursive definition  if n = 0, fib(0) = 0  if n = 1, fib(1) = 1  if n > 0, fib(n) = fib(n-1) + fib(n-2)  if n < 0, fib(n) = undefined
  38. Fibonacci Numbers Code  Recursive and iterative C functions 

    See program fib.c  Compare the two functions by number of function calls which are displayed as program output  The recursive function has many more function calls than the iterative function!  Compare the Big-O of the two functions: O(n) vs. O(2n)
  39. Function Calls for fib(5) fib(5) fib(4) + fib(3) fib(3) +

    fib(2) fib(2) + fib(1) fib(2) + fib(1) fib(1) + fib(0) fib(1) + fib(0) fib(1) + fib(0)
  40. Memory Management  Iteration  Recursion  Adding code example

     Runtime Stack  Fibonacci numbers code example