Lecture 10:
Analysis & Big-O
CS 136: Spring 2024
Katie Keith
Record on Zoom
📣 Announcements
In support of policy goals:
�Cuts to funding affect YOU: Williams Summer Science student research positions are funded (largely) through federal grants
📣 Announcements
CS 136: Learning Goals
Analysis
Tradeoffs of Time and Space
Advanced Programming
OOP, Java, JUnit, Git
Data Structures*
Use & Implement
*Later, we’ll clarify abstract data types versus data structures
🎯 Today’s Learning Objectives
📚Readings
Analysis is almost as old as computer science itself…
Fig credit: Science Museum (London)
Questions you might ask about a program you wrote
Today’s focus
To measure the runtime of my program, what’s wrong with looking at the clock to figure out when my program starts and ends (what we call “wall time”)?
đź’ˇThink-pair-share
Answer
đź’ˇThink-pair-share
Example analysis
1. Input:
An array with n elements
2. Approximate
Approximate the program runtime as a function of n. Call this f(n).
3. Describe the order of growth
Program runtime
Also could be order of growth of memory use
Intuition for calculating Big-O
So we would say "f(n) is "
Example
Calculate Big-O for the two functions below.
Answer O(n^3)
Answer O(n)
đź’ˇThink-pair-share
Big-O: Formal definition
Let f be a function* and let g be a comparison function*.
We say “f(n) is big-O of g(n)” if there exists constants M>0 and n0 such that
*Both f and g are functions defined on some unbounded subset of the positive real numbers.
absolute value
“for all”
Big-O History
Q: Why do we call it Big-O?
Paul Bachmann, 1837-1920
A: The letter O was chosen by Bachmann to stand for Ordnung, aGerman word meaning (roughly) the order of approximation
How to turn code into a “function”?
We assume the following operations have a constant, unit cost:
Operation | Example(s) |
Accessing an element of an array | arr[5] |
Assigning a value to a variable | int x = 10 |
Accessing an instance variable | foo.some_data |
Elementary mathematical operations | x + 1 y*z |
Returning | return x |
Incrementing or decrementing | i++ j-=1 |
Be careful with String methods!
public static String mystery(String s){
int strLen = s.length();
if(strLen <=1){
return s;
}
String a = s.substring(0, strLen/2);
String b = s.substring(strLen/2, strLen);
return mystery(b) + mystery(a);
}
Using .substring() in Java 8 is O(n)
It copies each character from the original string to the new substring's character array.
Concatenating two strings in Java 8 is O(n)
The + operator creates a new String object and copies the contents of both strings into the new object.
If n is the current number of characters in the input string…
Example: Calculating Big-O from code
c + n*2c
n*2c
Total & Analysis:
f(n) = 4*nc + 4c
g(n) = n
O(n)
c
Here, n is the number of elements in array
Worse case, we never hit this line
int[] theArray = {1, 3, 5, 9, 11};
int index = linearSearch(theArray,10);
Example method call
2c
Examples of order of growth, Part 1
Sedgewick & Wayne. CS:IA. Section 4.1
Examples of order of growth, Part 2
Sedgewick & Wayne. CS:IA. Section 4.1
Running time of algorithms of different orders of growth
Very long = exceeds 10^25 years
Our running example: n is the number of elements in an array
Calculate the big-O time for the program below
// Like the quiz, We assume only n >= 1 given
public static String perfectNumber(int n){
int sum = 1;
for(int i=2; i <= Math.sqrt(n); i++){
if(n%i == 0){
sum += i;
if(i != n/i){
sum += n/i;
}
}
}
if(sum == n){return n + " is a perfect number";}
else{return n + " is not a perfect number";}
}
đź’ˇThink-pair-share
More order of growth
n
Binary search algorithm
Binary search locates a target value within a sorted array by
Search.binarySearch
đź’»
Two search algorithms, different big-O runtimes
Worst case: Target at the end of the array
Proof sketch, Big-O of Binary Search
Board work
Proof sketch, Big-O of Binary Search
âś…
âś…
🎯 Today’s Learning Objectives