1 / 20

CS106X – Programming Abstractions in C++

CS2 in C++ Peer Instruction Materials by  Cynthia Bailey Lee  is licensed under a  Creative Commons Attribution- NonCommercial - ShareAlike 4.0 International License . Permissions beyond the scope of this license may be available at  http://peerinstruction4cs.org.

walter
Download Presentation

CS106X – Programming Abstractions in C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.Permissions beyond the scope of this license may be available at http://peerinstruction4cs.org. CS106X – Programming Abstractions in C++ Cynthia Bailey Lee

  2. Today’s Topics: • Formal introduction to Big O • Powers of two and exponential time • Traveling Salesman problem

  3. Big-O Extracting time cost from example code

  4. A student has counted how many times we perform each line of code Do you agree with the student’s count of 3n+5? • Yes • No • Other/ none/ more

  5. A student has counted how many times we perform each line of code Do you agree with the student’s count of 3n+5? • Yes • No • Other/ none/ more Do we really care about the +5? Or the 3 for that matter?

  6. Big-O We say a function f(n) is “big-O” of another function g(n), and write f(n) = O(g(n)) (or f(n) ∈ O(g(n))), if there are positive constants c and n0 such that: • f(n) ≤c g(n) for all n ≥ n0.  This is really more detail than you need to know for this course, but for some students it may help to know what underlies our approach.

  7. Big-O We say a function f(n) is “big-O” of another function g(n), and write f(n) = O(g(n)) (or f(n) ∈ O(g(n))), if there are positive constants c and n0 such that: • f(n) ≤c g(n) for all n ≥ n0.  What you need to know: • O(g(n)) describes an “upper bound”—the algorithm will perform no worse than g(n) • We ignore constant factors in saying that • We ignore behavior for “small” n

  8. Big-O Interpreting graphs

  9. f2 is O(f1) • TRUE • FALSE Why or why not? f1 f2

  10. f1 is O(f2) • TRUE • FALSE Why or why not? f1 f2

  11. f(n) = O(g(n)), if there are positive constants c and n0 such that f(n) ≤ c* g(n) for all n ≥ n0.  • f2 = O(f1) because f1 is above f2—an “upper bound” • But also true: f1=O(f2) • We can move f2 above f1 by multiplying by c f1 f2

  12. f(n) = O(g(n)), if there are positive constants c and n0 such that f(n) ≤ c* g(n) for all n ≥ n0.  • f2 = O(f1) because f1 is above f2—an “upper bound” • But also true: f1=O(f2) • We can move f2 above f1 by multiplying by c • These two functions are both big-O of each other* f1 f2 • * Another way of saying that is that they are big-Θ of each other (Θ is beyond the scope of this class)

  13. f3is O(f1) • TRUE • FALSE Why or why not? f3 f1 f2

  14. f1is O(f3) • TRUE • FALSE Why or why not? f3 f1 f2

  15. Shortcuts for calculating Big-O analysis starting with a function characterizing the growth in cost of the algorithm

  16. Let f(n) = 3 log2n + 4 n log2n + n • Which of the following is true? • f(n) = O(log2n) • f(n) = O(nlog2n) • f(n) = O(n2) • f(n) = O(n) • Other/none/more

  17. Let f(n) = 546 + 34n + 2n2 • Which of the following is true? • f(n) = O(2n) • f(n) = O(n2) • f(n) = O(n) • f(n) = O(n3) • Other/none/more

  18. Let f(n) = 2n + 14n2 + 4n3 • Which of the following is true? • f(n) = O(2n) • f(n) = O(n2) • f(n) = O(n) • f(n) = O(n3) • Other/none/more

  19. Let f(n) = 100 • Which of the following is true? • f(n) = O(2n) • f(n) = O(n2) • f(n) = O(n) • f(n) = O(n100) • Other/none/more

  20. Fibonacci • F(0) = 0 • F(1) = 1 • F(n) = F(n-1) + F(n-2) for all n > 1 • Work is duplicated throughout the call tree • F(2) is calculated 3 separate times when calculating F(5)! • 15 function calls in total for F(5)! Image is in the public domain. http://commons.wikimedia.org/wiki/File:Fibonacci_call_tree_5.gif

More Related