Class Prep - PowerPoint PPT Presentation

1 / 43
About This Presentation
Title:

Class Prep

Description:

Pushdown Stack for Function Calls. Stack is a data structure ... could define power so that each subproblem was based on computing kn/2 instead ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 44
Provided by: davidh67
Category:
Tags: calls | class | for | papers | prep

less

Transcript and Presenter's Notes

Title: Class Prep


1
Class Prep
  • Bring to Class
  • In-Class Exercises
  • Paper for Classroom Printer
  • Copy programs to MATLAB folder
  • Run MATLAB

2
Grab Files
  • While we are waiting for class to start, copy
    push.m, pop.m, top.m, numberOfDigits.m,
    cs70ex7a.m from the course folder
  • Set MATLAB Current Directory

3
Week 07-a(91.-9.5)
  • Stacks
  • Recursion

4
Reminder Test Coming Up Next Class
  • Test 2 will not cover the new material from
    today to make sure that you have had enough time
    between presentation and testing.
  • Sample Test is accessible via a link on course
    schedule on course web page
  • cs.union.edu/csc070/tests
  • Once you have had a chance to work on the
    questions, feel free to send me an email message
    requesting the answers

5
Algorithm Study for CSc-70 Test
  • Take Practice Test, then Request Answers
  • Don't understand something? Email or see HannayD
  • Redo In-Class Exercises
  • Don't understand something? Email or see HannayD
  • Redo HW on Without Help from Others
  • Don't understand something? Email or see HannayD
  • Go Over Lecture Notes
  • Don't understand something? Email or see HannayD
  • Study Textbook, add Notes to Text
  • Don't understand something? Email or see HannayD

6
Concept Basic Stack Operations
The Activation Stack is a stack used by the
operating system to manage the execution of
function calls
7
A "stack"
  • Dishes and trays
  • Last one in is first one out
  • (don't know how deep it is, and don't care)
  • Can get the "top" value
  • A stack is used for functions to get space for
    it's arguments, internal data, and it's return
    location
  • Each call to a function gets the room it needs on
    the stack ("pushed" onto the stack).
  • Allows calling function over and over

8
Hands-On DEMO Pushdown Stack
  • Make sure that push.m, pop.m, and top.m are in
    your current directory (format compact)
  • gtgt push(25)
  • gtgt push(34)
  • gtgt pop
  • gtgt top
  • gtgt pop
  • gtgt pop
  • gtgt top

9
Pushdown Stack for Function Calls
  • Stack is a data structure
  • First In, Last Out (like cafeteria trays)
  • Stack used to automatically track info for
    function calls
  • return addresses
  • call-by-value parameters
  • local variables
  • Every time a function is called, a set of data
    goes on the stack, it disappears when the
    function returns
  • Recursion the stack grows

10
Function Specification vs Function Instances
  • The file defining the function merely specifies
    how it would behave if you ever called it. This
    is the function specification.
  • When you call the function, a stack frame is
    created, parameter values are supplied and you
    have a fully defined instance of that function.
  • There is no reason in principle why a function
    cannot call itself, because each call is a
    different instance of the function.

11
Recursion
  • Recursion is a basic problem solving technique
    that divides a problem into smaller subproblems
  • These subproblems may also be divided into
    smaller subproblems
  • When the subproblems are small enough to solve
    directly the process stops
  • A recursive algorithm is a problem solution that
    has been expressed in terms of two or more easier
    to solve subproblems

12
Illegal Recursion
  • Recursive Directions--Infinite Recursion
  • "Back to the Future"--Infinite Recursion

13
Recursion is Mysterious
  • You must ass u me that a function already exists
    as you are defining it.
  • "Leap of Faith"

14
The Two Requirements
  • 1) Base (Stopping) Case
  • Recursion stops here
  • Missing with Ziggy and Michael J. Fox
  • 2) Inductive (Recursive) Case
  • Convert to simpler problem
  • Also missing with Ziggy and Michael J. Fox
  • They call for the same problem again
  • Basic structure of a recursive function
  • if (base case is reached)
  • give solution
  • else
  • reduce problem to simpler version using recursion

15
Counting Digits in n
  • Recursive definition
  • if n is between and not equal to 10 and 10
  • the number of digits is 1
  • else
  • the number of digits is 1 more than the number
    of digits in n/10
  • Example
  • digits(321) 1 digits(321/10) 1
    digits(32)
  • 1 1 digits(32/10) 1
    1 digits(3)
  • 1 1 (1)
  • 3

16
Hands-On DEMO Counting Digits Function
  • function digits numberOfDigits(n)
  • if (abs(n) lt 10)
  • digits 1
  • else
  • digits 1 numberOfDigits(n/10)
  • end
  • NOTE No Loop, but we still are "repeating"
    something...

17
Factorial
  • Write a function that, given n, computes n!
  • n! 1 2 ... (n-1) n
  • Example
  • 5! 1 2 3 4 5 120
  • Specification
  • Receive n, an integer.
  • Precondition n gt 0
  • Return n!.

18
Analysis
  • Consider n! 1 2 ... (n-1) n
  • but (n-1)! 1 2 ... (n-1)
  • substituting n! (n-1)! n
  • We have defined the ! function in terms of
    itself.
  • Historically, this is how the function was
    defined before computers (and for-loops) existed.

19
Base Case
  • Recursive functions are designed in a 3-step
    process
  • 1. Identify a base case -- an instance of the
    problem whose solution is trivial.
  • Example The factorial function has two base
    cases
  • if n 0 n! 1
  • if n 1 n! 1

20
Induction Step
  • 2. Identify an induction step -- a means of
    solving the non-trivial (or big) instances of
    the problem using one or more smaller instances
    of the problem.
  • Example In the factorial problem, we solve the
    big problem using a smaller version of the
    problem
  • n! (n-1)! n
  • 3. Form an algorithm from the base case and
    induction step.

21
Algorithm
  • // factorial(n)
  • Receive n.
  • if n lt 1
  • return 1.
  • else
  • return factorial(n-1) n.
  • Factorial Exercise
  • Each become an "instance" of the function

22
Hands-On DEMO Factorial Example
  • function result fact(n)
  • if (n lt 1)
  • result 1
  • else
  • result n fact(n-1)
  • end
  • Obviously, we could have said result prod(1n),
    but that wouldn't give us such a nice example of
    recursion!

23
Activation Stack for Factorial
  • Call the function answer fact(5)

main program Unfinished answer
fact (5)
24
Activation Stack for Factorial
fact. 1st N5, Unfinished
5fact(4)
main program Unfinished answer
fact (5)
25
Activation Stack for Factorial
fact. 2nd N4, Unfinished
4fact(3)
fact. 1st N5, Unfinished
5fact(4)
main program Unfinished answer
fact (5)
26
Activation Stack for Factorial
fact. 3rd N3, Unfinished
3fact(2)
fact. 2nd N4, Unfinished
4fact(3)
fact. 1st N5, Unfinished
5fact(4)
main program Unfinished answer
fact (5)
27
Activation Stack for Factorial
fact. 4th N2, Unfinished
2fact(1)
fact. 3rd N3, Unfinished
3fact(2)
fact. 2nd N4, Unfinished
4fact(3)
fact. 1st N5, Unfinished
5fact(4)
main program Unfinished answer
fact (5)
28
Activation Stack for Factorial
fact. 5th N1, Finished
return 1
fact. 4th N2, Unfinished
2fact(1)
fact. 3rd N3, Unfinished
3fact(2)
fact. 2nd N4, Unfinished
4fact(3)
fact. 1st N5, Unfinished
5fact(4)
main program Unfinished answer
fact (5)
29
Activation Stack for Factorial
fact. 4th N2, Finished returns 21
fact. 3rd N3, Unfinished
3fact(2)
fact. 2nd N4, Unfinished
4fact(3)
fact. 1st N5, Unfinished
5fact(4)
main program Unfinished answer
fact (5)
30
Activation Stack for Factorial
fact. 3rd N3, Finished
returns 32
fact. 2nd N4, Unfinished
4fact(3)
fact. 1st N5, Unfinished
5fact(4)
main program Unfinished answer
fact (5)
31
Activation Stack for Factorial
fact. 2nd N4, Finished
returns 46
fact. 1st N5, Unfinished
5fact(4)
main program Unfinished answer
fact (5)
32
Activation Stack for Factorial
Fact. 1st N5, Finished
returns 524
main program Unfinished answer
fact (5)
33
Activation Stack for Factorial
main program Finished answer
120
34
Summary Characteristics of Recursion
  • Answer requires calling same function again
  • Think of it as a clone and I never run out of
    clones - each with their own copies of data on
    the stack. "Instance"
  • Stopping condition
  • Call with different parameters moving closer to
    the stopping condition
  • Each call to the next clone has different
    parameters than the previous call "closer" to
    the stopping condition

35
Recursive Binary Search (c.f.Guess the Number)
  • Basic algorithm
  • If target is middle value, we are done
  • Else if target is less than middle value, search
    in lower half
  • Else target is greater than middle value search
    in upper half
  • What about case where target is not in array?
    What will happen?
  • Base case(s)
  • Inductive step(s)

36
Recursive Binary Search
  • function where binSearch(List, lookingFor,
    first, last)
  • if (first gt last)
  • where -1
  • else
  • next fix((firstlast)/2)
  • if lookingFor List(next)
  • where next
  • elseif lookingFor gt List(next)
  • where binSearch(List, lookingFor, next1,
    last)
  • else
  • where binSearch(List, lookingFor, first,
    next-1)
  • end
  • end

37
Hands-On DEMO Recursive "power" Function
  • Write a recursive algorithm to calculate the nth
    power of x
  • x x x x x (n times)
  • Base case?
  • what condition?
  • return what?
  • Inductive step?
  • what simpler call to the same function?
  • (see next slide for answer)

38
Evaluating Exponents Recursively
  • function pow power(x, n)
  • raise x to the power n
  • precondition n gt 0
  • if (n 0)
  • pow 1
  • else
  • pow x power(x, n 1)
  • end

39
Divide and Conquer
  • Using this method, each recursive subproblem is
    about one-half the size of the original problem
  • If we could define power so that each subproblem
    was based on computing kn/2 instead of kn 1 we
    could use the divide and conquer principle
  • Efficiency of Binary Search vs. Linear Search
  • Recursive divide and conquer algorithms are often
    more efficient than iterative algorithms

40
Evaluating Exponents Using Divide and Conquer
  • function pow myPower(x, n)
  • if (n 0)
  • pow 1
  • else
  • tmp myPower(x, fix(n/2))
  • if (rem(n, 2) 0)
  • pow tmp tmp
  • else
  • pow x tmp tmp
  • end
  • end

41
Review the 2 Requirements
  • 1) Base (Stopping) Case
  • Recursion stops here
  • 2) Inductive (Recursive) Case
  • Convert to simpler problem
  • Basic structure of a recursive function
  • if (base case is reached)
  • give solution
  • else
  • reduce problem to simpler version using recursion

What happens if we forget to include the base
case?
42
In-Class Exercise 7a Compound Interest(given
cs70ex7a.m)
  • Non-Recursive Formula
  • Value StartingValue(1interest)n
  • Recursively,
  • Value(0) StartingValue
  • Value(period) Value(period-1)(1interest)
  • Write a function, rCompound using recursion to
    compute the amount of money in your account at
    the end of "n" periods. Your function will be
    sent
  • Starting Value, Interest Rate, Number of Periods
  • Test with 1000, 0.05, 20 and 10000, 0.07, 25
  • Both calls in same main program cs70ex7a.m

43
END
Write a Comment
User Comments (0)
About PowerShow.com