Share
Embed
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
49 slides
-
Memory Upload • Phone books • Iteration • Recursion •
Code examples • Box trace -
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 -
Algorithm #1 1. Look for the number on page 1
2. If can’t find the number, look for the number on page 2 3. If can’t find the number, look for the number on page 3 4. And keep repeating this until the number is found -
Algorithm #2 1. Open the phone book at the halfway
point 2. If can’t find number, open the 1st or 2nd half of the phone book at its halfway point 3. Keep repeating this until the number is found -
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 while loop -
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 method calls and if statements -
Recursive Algorithm • A more detailed algorithm for looking up
a number in the phone book recursively 1. Open the phone book at the halfway point 2. Check the top of the page for the person’s name -
Recursive Algorithm 3.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 -
Recursive Algorithm 4.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 -
General Characteristics • Two basic steps to writing recursive methods
1.Base case ends the method May have more than 1 base case -
General Characteristics 2.Recursive case calls itself Same method call
May have more than 1 recursive case Recursive case creates smaller problem Recursive case eventually calls base case -
Big-O Break • Iterative algorithm of looking for a number
in the phone book, one page at a time • O(n), because we must flip (loop) through each page • Recursive algorithm of dividing the phone book in half, and then half again, etc. to search for the number • O(log n), because we keep dividing the problem in half -
String Problem • How can we write a word backwards?
• Iterative algorithm Loop backwards through each character of the original word While looping, display each letter of the original word bat tab -
String Problem • How can we write a word backwards?
• Recursive algorithm 1.Base case: if length of string equals one (1), display string 2.Recursive case: display the last character in the string, then call method with shorter string with the last letter cut off -
Example Code • See code at Backwards.java under links column
for both the iterative and recursive methods • Note that you need to use commandline input -
Big-O Break • Iterative algorithm of looping backwards through a
string to display it backwards • O(n), because we must loop though each letter • Recursive algorithm of displaying the last character, deleting the last character, and keep calling the method • O(n), because have a method call for each letter -
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 stack heap -
3 Parts of Program in Memory 1. Data segment •
Contains global data (static variables) 2. Code segment • Contains the program code (in Java bytecode or machine language) -
3 Parts of Program in Memory 3. Stack segment A.
Top: Contains runtime stack • Stack of methods’ activation record instance (return address, arguments, local variables, return value) B. Bottom: Contains the heap (stores Java Objects) -
How To Trace Method Calls 1. Label all method calls
in program code with letters A, B, C, D, … • Each letter represents the return address of each method • The return address is where program control returns to after a method ends execution -
How To Trace Method Calls • Program control keeps track
of the next instruction that will be executed -
How To Trace Method Calls 2. Draw stack of boxes
– one (1) box for each time a method is called by the executing program • This diagram shows 3 recursive method calls (ARIs) method1’s ARI (1st method call) method1’s ARI (2nd method call) method1’s ARI (3rd method call) -
How To Trace Method Calls • Method’s activation record instances(ARIs)
1. return address (step #1 labels) 2. arguments (parameters) 3. local variables 4. return value 1. return address 2. arguments 3. local variables 4. return value 1. return address 2. arguments 3. local variables 4. return value 1. return address 2. arguments 3. local variables 4. return value -
Example Box Trace • See method recursiveBack() in program Backwards.java
• 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 -
Return Values • Usually when we write methods, we have
a return value, so that the method can return the result of a calculation For example, in the program Backwards.java, the strings are written backwards by all 3 methods, and the string is returned to the main() method, which prints the strings -
Box Trace • Let’s do a box trace for the
recursive method recursiveBack() 1. We already have return address labels B and D for the method calls 2. As the program runs, we will draw a box (ARI) for each method call • The commandline input to the program is the string "bat" -
Box Trace • Program code • Program control is on
line #20 with return address B • Once this method ends, the return value is assigned to variable backwards • Runtime stack No ARIs (activation record instances), boxes that store the data for the local environment of a method, on the runtime stack yet -
Box Trace • Program code • The method call pushes
return address B and argument "bat" to the runtime stack • Program control jumps to line #47 • Runtime stack return address=B string="bat" -
Box Trace • Program code • The instruction at line
#48 assigns length of string parameter to local variable length • Since if statement is false, program jumps to line #56 • Runtime stack return address=B string="bat" length=3 -
Box Trace • Program code • At line #57, the
return statement adds the letter "t" to another method call with argument "ba" • Runtime stack return address=B string="bat" length=3 return value="t"+m("ba") -
Box Trace • Program code • The method call on
line #58, pushes return address D and argument "ba" to the runtime stack • Program control jumps to line #47 • Runtime stack return address=B string="bat" length=3 return value="t"+m("ba") return address=D string="ba" -
Box Trace • Program code • Program control goes to
line #48 • A different string parameter’s length value is assigned to a different length local variable • Runtime stack return address=B string="bat" length=3 return value="t"+m("ba") return address=D string="ba" length=2 -
Box Trace • Program code • Because the if statement
at line #50 is still false, program control continues to the else statement on line #56 • Runtime stack return address=B string="bat" length=3 return value="t"+m("ba") return address=D string="ba" length=2 -
Box Trace • Program code • Return statement adds "a"
to a method call with argument "b" • Runtime stack return address=B string="bat" length=3 return value="t"+m("ba") return address=D string="ba" length=2 return value="a"+m("b") -
Box Trace • Program code • On line #57, return
address D and argument "b" pushed on stack, jump to line #47 • Runtime stack return address=B string="bat" length=3 return value="t"+m("ba") return address=D string="ba" length=2 return value="a"+m("b") -
Box Trace • Program code • Local variable length is
assigned 1, so the if statement is true • On line #51, the parameter string is returned with value "b" return address=B string="bat" length=3 return value="t"+m("ba") return address=D string="ba" length=2 return value="a"+m("b") return address=D string="b" length=1 return value="b" -
Box Trace • Program code • The return statement on
line #51 is a base case, so no more recursive method calls return address=B string="bat" length=3 return value="t"+m("ba") return address=D string="ba" length=2 return value="a"+m("b") return address=D string="b" length=1 return value="b" -
Box Trace • Program code • Return statements return the
return value to the calling method located at the return address, and pop the bottommost ARI off the runtime stack return address=B string="bat" length=3 return value="t"+m("ba") return address=D string="ba" length=2 return value="a"+m("b") return address=D string="b" length=1 return value="b" -
Box Trace • Program code • The return statement on
line #51, returns return value "b" to return address D at line #57, and pops the bottommost ARI off runtime stack • Runtime stack return address=B string="bat" length=3 return value="t"+m("ba") return address=D string="ba" length=2 return value="a"+"b" -
Box Trace • Program code • The return value on
the runtime stack has changed from "a"+m("b") to "a"+"b", which is the string "ab" • Runtime stack return address=B string="bat" length=3 return value="t"+m("ba") return address=D string="ba" length=2 return value="ab" -
Box Trace • Program code • Program control contines from
line #57 to line #58 • The recursive method call returns the value "ab" to the return address D at line #57 • Runtime stack return address=B string="bat" length=3 return value="t"+m("ba") return address=D string="ba" length=2 return value="ab" -
Box Trace • Program code • The recursive method call
also pops off the bottommost ARI • The return value is now "tab" • Runtime stack return address=B string="bat" length=3 return value="t"+"ab" -
Box Trace • Program code • The method is finally
finished • At the return statement at line #57, return value "tab" is returned to the address B at line #20 in main() • Runtime stack return address=B string="bat" length=3 return value="tab" -
Box Trace • Program code • Once program control returns
to line #20, the return value "tab" is assigned to local variable backwards • Runtime stack Since all of the ARIs have been popped off the runtime stack, we have no boxes of data for the method’s local environment anymore… -
jGRASP Debugger Box Trace • Make sure you mark the
code with a red dot breakpoint, press ladybug button, and press the “Step in for selected thread” button (top left blue arrow curving down to the right) • Also check out the call stack window to see the ARI for each method -
Infinite Recursion • See example code Counting.java • Two methods
which add integers from 1 to 5 using loops and recursion • Also has example of infinite recursion • If the method never reaches the base case, then the runtime stack will fill up with method calls (ARIs), and run out of memory (StackOverflowError) -
Memory Defragmenter • Phone book algorithm • Iteration • Recursion
• Writing a string backwards example • Box trace example showing how recursion works -
Task Manager • Before the next class, you need to:
1.Do the assignment corresponding to this lecture 2.Email me any questions you may have about the material 3.Turn in the assignment before the next lecture 4.Go outside and get some exercise!