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

Class 20: Cost of Computation

Class 20: Cost of Computation

cs1120: Introduction to Computing
Explorations in Language, Logic, and Machine
University of Virginia, Spring 2016

http://xplorecs.org/class20

Class 20:
Cost of Computation
Constant, Linear, Quadratic, Exponential

David Evans

March 16, 2016
Tweet

More Decks by David Evans

Other Decks in Programming

Transcript

  1. Class 20: Cost of Computing Introduction to Computing: Explorations in

    Language, Logic, and Machines cs1120 Spring 2016 David Evans University of Virginia
  2. Plan Today Cost of Computing (notes from Monday, no new

    notes today) Now Project 4 Due Friday Last chance to become eligible for Blue Belt test Next Monday Blue Belt test (in class)
  3. Project 4 Addendum Opportunity If you feel like you didn’t

    understand how to describe computing cost to answer Problem 5 and Problem 10 well, but do after class today, you can submit an “addendum” to your Project 4 by 6:59pm tomorrow (Thursday), and still be eligible for Blue Best Test on Monday.
  4. Measuring Cost Cost scales with size of input Absolute cost

    depends on lots of “details” For a given program, we want to describe the cost function: costf (input size) ∼ number of steps to execute Since we don’t know the absolute cost of a “step”, and it is a huge pain to count steps exactly, we use the asymptotic operators to hide these details.
  5. Modeling Python Assume these operations take constant time: Assignment Arithmetic

    and Comparison Operators on Bounded Values Conditional and Looping Constructs (not their bodies!) Function calls and returns (not the cost of executing function) List indexing and slicing These are not necessarily true – depend on Python implementation!
  6. Expressing Formally Big-O: functions that grow no faster than f

    Omega (Ω): functions that grow no slower than f 9
  7. The Sets O(f ) and Ω(f ) f O(f) Functions

    that grow no faster than f Ω(f) Functions that grow no slower than f 10
  8. Theta (“Order of”) Intuition: set of functions that grow as

    fast as f Definition: Slang: When people say, “f is order g” that means 11
  9. O(n3) O(n2) f(n) = n2.5 f(n) = 12n2 + n

    f(n) = n3.1 – n2 Ω(n2) Tight Bound Theta (Θ) Θ(n2) 12
  10. Θ Examples • Is 10n in Θ(n)? – Yes, since

    10n is Ω(n) and 10n is in O(n) • Doesn’t matter that you choose different c values for each part; they are independent • Is n2 in Θ(n)? – No, since n2 is not in O(n) • Is n in Θ(n2)? – No, since n2 is not in Ω(n) 13
  11. Common Costs Constant Time: O(1) Execution time does not grow

    with size of input Either size of input is fixed, or the code does not even need to look at all of the input
  12. Common Costs Linear Time: ϴ(N) Execution time grows linearly with

    size of input Increasing the size of the input by one unit, increases the work by a constant amount.
  13. Common Costs Quadratic Time: ϴ(N2) Execution time grows as square

    of size of input Increasing the size of the input by one unit, adds work that is the size of the input.
  14. Common Costs Exponential Time: ϴ(2N) Grows exponentially in size of

    input Increasing the size of the input by one unit, doubles the amount of work.
  15. Common Costs Recap Impact of increasing input size by 1

    Asymptotic Cost Notation (N is size of input) No increase in cost Constant O(1) Increases by fixed amount Linear ϴ(N) Increases by size of input Quadratic ϴ(N2) Doubles cost Exponential ϴ(2N)
  16. Charge Project 4 Addendum Opportunity: if you feel like you

    didn’t understand how to describe computing cost to answer Problem 5 and Problem 10 well, but do now, you can submit an “addendum” to your Project 4 by 6:59pm tomorrow (Thursday), and still be eligible for Blue Best Test