Cynthia Bailey Lee CS2 in C Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons Attribution NonCommercial ShareAlike ID: 791269
Download The PPT/PDF document "CS106X – Programming Abstractions in ..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
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.
Slide2Today’s Topics:
Formal introduction to Big OPowers of two and exponential time
Traveling Salesman problem2
Slide3Big-OExtracting time cost from example code
Slide4A student has counted how many times we perform each line of code
Do you agree with the student’s count of 3n+5?
YesNoOther/ none/ more StatementsCost1
float
findAvg
( Vector<
int
>& grades ){
2
float sum = 0;
1
3 int count = 0;14 while ( count < grades.size() ) {n + 15 sum += grades[count];n6 count++;n7 }8 if ( grades.size() > 0 )19 return sum / grades.size();10 else111 return 0.0f;12}ALL3n+5
Slide5A student has counted how many times we perform each line of code
Do you agree with the student’s count of 3n+5?
YesNoOther/ none/ more StatementsCost1
float
findAvg
( Vector<
int
>& grades ){
2
float sum = 0;
1
3 int count = 0;14 while ( count < grades.size() ) {n + 15 sum += grades[count];n6 count++;n7 }8 if ( grades.size() > 0 )19 return sum / grades.size();10 else111 return 0.0f;12}ALL3n+5
Do we really care about the +5?
Or the 3 for that matter?
Slide6Big-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.
Slide7Big-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 thatWe ignore behavior for “small” n
Slide8Big-O
Interpreting graphs
Slide9f2 is O(f1)
TRUE
FALSEWhy or why not?f1f2
Slide10f1 is O(f2)
TRUE
FALSEWhy or why not?f1f2
Slide11f2 = O(f1
) because f1 is above f2—an “upper bound”But also true: f
1 = O(f2)We can move f2 above f1 by multiplying by cf1f2f(n) = O(g(n)), if there are positive constants c and n0 such that f(n) ≤ c * g(n) for all n ≥ n0.
Slide12f2 = O(f1
) because f1 is above f2—an “upper bound”But also true: f
1 = O(f2)We can move f2 above f1 by multiplying by cThese two functions are both big-O of each other*f1f2f(n) = O(g(n)), if there are positive constants c and n0 such that f(n) ≤ c * g(n) for all n ≥ n0. * Another way of saying that is that they are big-Θ of each other (Θ is beyond the scope of this class)
Slide13f3 is O(f1
)TRUE
FALSEWhy or why not?f1f2f3
Slide14f1 is O(f3
)TRUE
FALSEWhy or why not?f1f2f3
Slide15Shortcuts for calculating
Big-O analysis starting with a function characterizing the growth in cost of the algorithm
Slide16Let f(n) = 3 log2
n + 4 n log2 n +
nWhich of the following is true?f(n) = O(log2n)f(n) = O(nlog2n)f(n) = O(n2)f(n) = O(n)Other/none/more
Slide17Let 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
Slide18Let f(n) = 2n + 14
n2 + 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
Slide19Let f(n) = 100
Which of the following is true?f(n) = O(2
n)f(n) = O(n2)f(n) = O(n)f(n) = O(n100)Other/none/more
Slide20Fibonacci
F(0) = 0F(1) = 1F(n) = F(n-1) + F(n-2) for all n > 1
Work is duplicated throughout the call treeF(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