Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.

Similar presentations


Presentation on theme: "CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons."— Presentation transcript:

1 CS106X – Programming Abstractions in C++ Cynthia Bailey Lee 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.Cynthia Bailey LeeCreative Commons Attribution-NonCommercial- ShareAlike 4.0 International Licensehttp://peerinstruction4cs.org

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

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? A. Yes B. No C. Other/ none/ more StatementsCost 1 float findAvg ( Vector & grades ){ 2 float sum = 0;1 3 int count = 0;1 4 while ( count < grades.size() ) {n + 1 5 sum += grades[count];n 6 count++;n 7 } 8 if ( grades.size() > 0 )1 9 return sum / grades.size(); 10 else1 11 return 0.0f; 12 } ALL 3n+5

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? A. Yes B. No C. Other/ none/ more StatementsCost 1 float findAvg ( Vector & grades ){ 2 float sum = 0;1 3 int count = 0;1 4 while ( count < grades.size() ) {n + 1 5 sum += grades[count];n 6 count++;n 7 } 8 if ( grades.size() > 0 )1 9 return sum / grades.size(); 10 else1 11 return 0.0f; 12 } ALL 3n+5 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 n 0 such that:  f(n) ≤ c g(n) for all n ≥ n 0. 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 n 0 such that:  f(n) ≤ c g(n) for all n ≥ n 0. 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 f 2 is O(f 1 ) A. TRUE B. FALSE Why or why not? f1f1 f2f2

10 f 1 is O(f 2 ) A. TRUE B. FALSE Why or why not? f1f1 f2f2

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

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

13 f 3 is O(f 1 ) A. TRUE B. FALSE Why or why not? f1f1 f2f2 f3f3

14 f 1 is O(f 3 ) A. TRUE B. FALSE Why or why not? f1f1 f2f2 f3f3

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

16 Let f(n) = 3 log 2 n + 4 n log 2 n + n  Which of the following is true? A. f(n) = O(log 2 n) B. f(n) = O(nlog 2 n) C. f(n) = O(n 2 ) D. f(n) = O(n) E. Other/none/more

17 Let f(n) = 546 + 34n + 2n 2  Which of the following is true? A. f(n) = O(2 n ) B. f(n) = O(n 2 ) C. f(n) = O(n) D. f(n) = O(n 3 ) E. Other/none/more

18 Let f(n) = 2 n + 14n 2 + 4n 3  Which of the following is true? A. f(n) = O(2 n ) B. f(n) = O(n 2 ) C. f(n) = O(n) D. f(n) = O(n 3 ) E. Other/none/more

19 Let f(n) = 100  Which of the following is true? A. f(n) = O(2 n ) B. f(n) = O(n 2 ) C. f(n) = O(n) D. f(n) = O(n 100 ) E. 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 http://commons.wikimedia.org/wiki/File:Fibonacci_call_tree_5.gif


Download ppt "CS106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons."

Similar presentations


Ads by Google