cs147 Lecture Slides
cs147 Lecture Slides
(CS147)
Lecture 2&3: Algorithm analysis: worst-case asymptotic runing time, big-O notation
Fanghui Liu
Example
Input - An array A[1, 2, . . . , n] of n numbers.
⇔ A[1], A[2], A[3], · · · , A[n]
For each i ∈ {1, 2, · · · , n}, A[i] is a real number.
Example
Input - An array A[1, 2, . . . , n] of n numbers.
Output
▶ Yes if there exist indices i, j ∈ {1, 2, · · · , n} with i , j such that A[i] + A[j] = 0.
▶ No otherwise.
Example
Input - An array A[1, 2, . . . , n] of n numbers.
Output
▶ Yes if there exist indices i, j ∈ {1, 2, · · · , n} with i , j such that A[i] + A[j] = 0.
▶ No otherwise.
Definition
An algorithm for a computational problem P is a step by step procedure such that given any
input I, outputs a valid solution for I.
Definition
An algorithm for a computational problem P is a step by step procedure such that given any
input I, outputs a valid solution for I.
Input Output
Algorithm
▶ Input: I
▶ Output: A valid solution for I
Statement
Runtime of a computer program = the actual time (say, in microseconds) if it takes to finish
execution.
▶ It depends on
◦ the input
◦ the programming language
◦ the machine (computer)
Claim
For very large values of n, there is no significant difference between 5n and 7n!
Claim
For very large values of n, there is no significant difference between 5n and 7n!
Target
Talk about the rate at which some function changes as its argument grows (or shrinks),
without worrying to much about the detailed form.
▶ g(n) ∈ O(f (n)) [Big O of f (n)] if there exist constants c > 0 and N such that
g(n) ≤ cf (n) for all n > N .
▶ g(n) ∈ O(f (n)) [Big O of f (n)] if there exist constants c > 0 and N such that
g(n) ≤ cf (n) for all n > N .
▶ g(n) ∈ Ω(f (n)) [Big Omega of f (n)] if there exist constants c > 0 and N such that
g(n) ≥ cf (n) for all n > N .
▶ g(n) ∈ O(f (n)) [Big O of f (n)] if there exist constants c > 0 and N such that
g(n) ≤ cf (n) for all n > N .
▶ g(n) ∈ Ω(f (n)) [Big Omega of f (n)] if there exist constants c > 0 and N such that
g(n) ≥ cf (n) for all n > N .
▶ g(n) ∈ Θ(f (n)) [Big Theta of f (n)] if g(n) ∈ O(f (n)) and g(n) ∈ Ω(f (n)).
⇔ there exist constants c1 , c2 > 0 and N such that c1 f (n) ≤ g(n) ≤ c2 f (n) for all
n > N.
Example
The function n is in O(n3 ).
Example
The function n is in O(n3 ).
Proof.
Set c = 1, N = 1, then n ≤ cn3 for all n ≥ N . □
Example
The function n is in O(n3 ).
Proof.
Set c = 1, N = 1, then n ≤ cn3 for all n ≥ N . □
Example
The function n3 is not in O(n).
Example
The function n is in O(n3 ).
Proof.
Set c = 1, N = 1, then n ≤ cn3 for all n ≥ N . □
Example
The function n3 is not in O(n).
Proof by contradiction.
3
√ constants c > 0 and N such that n ≤ cn for all n > N . But we have
Suppose that there exist
3
n > cn for all n > c. This leads to a contradiction, and concludes the proof. □
▶ g(n) ∈ o(f (n)) [little o of f (n)] if for every c > 0, there exists an N such that
g(n) ≤ cf (n) for all n > N .
g(n)
⇔ lim = 0.
n→∞ f (n)
▶ g(n) ∈ ω(f (n)) [little omega of f (n)] if for every c > 0, there exists an N such that
g(n) ≥ cf (n) for all n > N .
g(n)
⇔ lim = ∞.
n→∞ f (n)
Statement
for all f (n) ∈ O(n2 ) and g(n) ∈ O(n3 ), we have f (n) + g(n) + 1 ∈ O(n3 ).
Statement
for all f (n) ∈ O(n2 ) and g(n) ∈ O(n3 ), we have f (n) + g(n) + 1 ∈ O(n3 ).
Proof.
Prove by definition. □
Statement
for all f (n) ∈ O(n2 ) and g(n) ∈ O(n3 ), we have f (n) + g(n) + 1 ∈ O(n3 ).
Proof.
Prove by definition. □
n2 n3
◦ Example: 2 + 4 = O(n2 ) + O(n3 ) = O(n3 )
Statement
for all f (n) ∈ O(n2 ) and g(n) ∈ O(n3 ), we have f (n) + g(n) + 1 ∈ O(n3 ).
Proof.
Prove by definition. □
2 3
◦ Example: n2 + n4 = O(n2 ) + O(n3 ) = O(n3 )
◦ We can generalize it!
Example
√
ln n
Given f (n) = 2 and g(n) = n0.0001 , check f (n) = O(g(n)) and f (n) , Ω(g(n)).
Example
√
ln n
Given f (n) = 2 and g(n) = n0.0001 , check f (n) = O(g(n)) and f (n) , Ω(g(n)).
Proof.
We can prove it by the definition.
We prove f (n) = O(g(n)) as an example, by definition,
Example
√
ln n
Given f (n) = 2 and g(n) = n0.0001 , check f (n) = O(g(n)) and f (n) , Ω(g(n)).
Proof.
We can prove it by the definition.
We prove f (n) = O(g(n)) as an example, by definition, we need to find c > 0 and n > N such
that f (n) ≤ cg(n).
Example
√
ln n
Given f (n) = 2 and g(n) = n0.0001 , check f (n) = O(g(n)) and f (n) , Ω(g(n)).
Proof.
We can prove it by the definition.
We prove f (n) = O(g(n)) as an example, by definition, we need to find c > 0 and n > N such
that f (n) ≤ cg(n). Here we assume c ≥ 1 without loss of generality.
Example
√
ln n
Given f (n) = 2 and g(n) = n0.0001 , check f (n) = O(g(n)) and f (n) , Ω(g(n)).
Proof.
We can prove it by the definition.
We prove f (n) = O(g(n)) as an example, by definition, we need to find c > 0 and n > N such
that f (n) ≤ cg(n). Here we assume c ≥ 1 without loss of generality. why?
Example
√
ln n
Given f (n) = 2 and g(n) = n0.0001 , check f (n) = O(g(n)) and f (n) , Ω(g(n)).
Proof.
We can prove it by the definition.
We prove f (n) = O(g(n)) as an example, by definition, we need to find c > 0 and n > N such
that f (n) ≤ cg(n). Here we assume c ≥ 1 without loss of generality. why?
√
ln n
2 ≤ cn0.0001
Example
√
ln n
Given f (n) = 2 and g(n) = n0.0001 , check f (n) = O(g(n)) and f (n) , Ω(g(n)).
Proof.
We can prove it by the definition.
We prove f (n) = O(g(n)) as an example, by definition, we need to find c > 0 and n > N such
that f (n) ≤ cg(n). Here we assume c ≥ 1 without loss of generality. why?
√
ln n
√
2 ≤ cn0.0001 ⇔ ln n ln 2 ≤ ln c + 0.0001 ln n
Example
√
ln n
Given f (n) = 2 and g(n) = n0.0001 , check f (n) = O(g(n)) and f (n) , Ω(g(n)).
Proof.
We can prove it by the definition.
We prove f (n) = O(g(n)) as an example, by definition, we need to find c > 0 and n > N such
that f (n) ≤ cg(n). Here we assume c ≥ 1 without loss of generality. why?
√
ln n
√
2 ≤ cn0.0001 ⇔ ln n ln 2 ≤ ln c + 0.0001 ln n
√
⇐ ln n ln 2 ≤ 0.0001 ln n
Example
√
ln n
Given f (n) = 2 and g(n) = n0.0001 , check f (n) = O(g(n)) and f (n) , Ω(g(n)).
Proof.
We can prove it by the definition.
We prove f (n) = O(g(n)) as an example, by definition, we need to find c > 0 and n > N such
that f (n) ≤ cg(n). Here we assume c ≥ 1 without loss of generality. why?
√
ln n
√
2 ≤ cn0.0001 ⇔ ln n ln 2 ≤ ln c + 0.0001 ln n
√
⇐ ln n ln 2 ≤ 0.0001 ln n
√ 8
(ln 2)2
⇔ ln n ≥ 104 ln 2 ⇔ n ≥ e10 := N .
Example
√
ln n
Given f (n) = 2 and g(n) = n0.0001 , check f (n) = O(g(n)) and f (n) , Ω(g(n)).
Proof.
We can prove it by the definition.
We prove f (n) = O(g(n)) as an example, by definition, we need to find c > 0 and n > N such
that f (n) ≤ cg(n). Here we assume c ≥ 1 without loss of generality. why?
√
ln n
√
2 ≤ cn0.0001 ⇔ ln n ln 2 ≤ ln c + 0.0001 ln n
√
⇐ ln n ln 2 ≤ 0.0001 ln n
√ 8
(ln 2)2
⇔ ln n ≥ 104 ln 2 ⇔ n ≥ e10 := N .
8
(ln 2)2
We conclude the proof by taking c ≥ 1 and N := e10 . □
f (n)
▶ If limn→∞ g(n) exists (and is finite), then f (n) = O(g(n)).
▶ If limn→∞ fg(n)
(n)
exists (and is finite), then f (n) = O(g(n)).
▶ We can often apply L’Höpital’s rule to calculate this limit
f (n) f ′ (n)
lim = lim ′ .
n→∞ g(n) n→∞ g (n)
▶ If limn→∞ fg(n)
(n)
exists (and is finite), then f (n) = O(g(n)).
▶ We can often apply L’Höpital’s rule to calculate this limit
f (n) f ′ (n)
lim = lim ′ .
n→∞ g(n) n→∞ g (n)
Example
Consider two functions f (n) = 2n and g(n) = 3n , we have f (n) = O(g(n)) and
f (n) , Ω(g(n)).
Example
Pn
Bounding parts of the sum: consider f (n) = i=1 i3 .
Example
Pn
Bounding parts of the sum: consider f (n) = i=1 i3 . Prove that f (n) = Θ(n4 ).
Example
Pn
Bounding parts of the sum: consider f (n) = i=1 i3 . Prove that f (n) = Θ(n4 ).
Proof.
◦ upper bound
n
X
f (n) = i3 ≤
i=1
Example
Pn
Bounding parts of the sum: consider f (n) = i=1 i3 . Prove that f (n) = Θ(n4 ).
Proof.
◦ upper bound
n
X n
X
3
f (n) = i ≤ n3 = O(n4 ) .
i=1 i=1
Example
Pn
Bounding parts of the sum: consider f (n) = i=1 i3 . Prove that f (n) = Θ(n4 ).
Proof.
◦ upper bound
n
X n
X
3
f (n) = i ≤ n3 = O(n4 ) .
i=1 i=1
◦ lower bound
n
X
f (n) = i3 ≥
i=1
Example
Pn
Bounding parts of the sum: consider f (n) = i=1 i3 . Prove that f (n) = Θ(n4 ).
Proof.
◦ upper bound
n
X n
X
3
f (n) = i ≤ n3 = O(n4 ) .
i=1 i=1
◦ lower bound
n n
X X
3n
f (n) = i ≥ ( )3
i=1 n 2
i= 2
Example
Pn
Bounding parts of the sum: consider f (n) = i=1 i3 . Prove that f (n) = Θ(n4 ).
Proof.
◦ upper bound
n
X n
X
3
f (n) = i ≤ n3 = O(n4 ) .
i=1 i=1
◦ lower bound
n n
X X
3n
f (n) = i ≥ ( )3 = Ω(n4 ) .
i=1 n 2
i= 2
4
Accordingly, we have f (n) = Θ(n ). □
▶ For vectorMax(): ignore the original two variable initializations, the return statement,
the comparison, and the setting of currentMax in the loop.
▶ For vectorMax(): ignore the original two variable initializations, the return statement,
the comparison, and the setting of currentMax in the loop.
▶ Notice that the important part of the function is the fact that the loop conditions will
change with the size of the array: for each extra element, there will be one more
iteration. This is a linear relationship, and therefore O(n).
▶ The inner loop (variable j) has a complexity of O(n), as our analysis from the previous
slides would show. However, the entire inner loop happens n times, as well! This squares
the number of times result is incremented.
▶ The inner loop (variable j) has a complexity of O(n), as our analysis from the previous
slides would show. However, the entire inner loop happens n times, as well! This squares
the number of times result is incremented.
▶ Now we have a quadratic relationship between n and the time to complete the function:
O(n2 ).
Formally, let
Goal
Given a computational problem, our goal will be to find an algorithm for it with smallest
possible worst-case asymptotic runtime.
Statement
▶ constant time O(1): arithmetic/logic operation, access one element in an array
▶ linear time O(n): merge two sorted arrays (Lecture 5)
▶ quadratic time O(n2 ): bubble sort (Lecture 4)
▶ logarithmic time O(log n): binary search (Lecture 6)
▶ linearithmic time O(n log n): merge sort (Lecture 5)
▶ the best case: find one input that the algorithm can perform the best
▶ the best case: find one input that the algorithm can perform the best
▶ the average case: averaged over all possible inputs for randomized algorithm
▶ the best case: find one input that the algorithm can perform the best
▶ the average case: averaged over all possible inputs for randomized algorithm
▶ runtime analysis vs. memory analysis
▶ the best case: find one input that the algorithm can perform the best
▶ the average case: averaged over all possible inputs for randomized algorithm
▶ runtime analysis vs. memory analysis
▶ be careful of hidden loop: e.g., for an array A[n], insert a value
◦at index 1
◦ at index n+1
▶ the best case: find one input that the algorithm can perform the best
▶ the average case: averaged over all possible inputs for randomized algorithm
▶ runtime analysis vs. memory analysis
▶ be careful of hidden loop: e.g., for an array A[n], insert a value
◦at index 1
◦ at index n+1
Be careful!
What is the difference between “at index 1” and “index n+1”?
▶ the best case: find one input that the algorithm can perform the best
▶ the average case: averaged over all possible inputs for randomized algorithm
▶ runtime analysis vs. memory analysis
▶ be careful of hidden loop: e.g., for an array A[n], insert a value
◦at index 1
◦ at index n+1
Be careful!
What is the difference between “at index 1” and “index n+1”?
▶ the best case: find one input that the algorithm can perform the best
▶ the average case: averaged over all possible inputs for randomized algorithm
▶ runtime analysis vs. memory analysis
▶ be careful of hidden loop: e.g., for an array A[n], insert a value
◦at index 1
◦ at index n+1
Be careful!
What is the difference between “at index 1” and “index n+1”?
Figure: Recall the example in Lecture 1: sample complexity and time complexity [CKM22].
[0] Zeyuan Allen-Zhu, Yuanzhi Li, and Zhao Song, A convergence theory for deep learning via
over-parameterization, International Conference on Machine Learning, PMLR, 2019,
pp. 242–252.
(Cited on page 119.)
[0] Sitan Chen, Adam R Klivans, and Raghu Meka, Learning deep relu networks is
fixed-parameter tractable, 2021 IEEE 62nd Annual Symposium on Foundations of
Computer Science (FOCS), IEEE, 2022, pp. 696–707.
(Cited on page 119.)