3 - Algorithm Time Analysis
3 - Algorithm Time Analysis
مبحث سوم:
زما ا لگور ا
ﺳﺠﺎد ﺷﯿﺮﻋیل ﴲﺮﺿﺎ
ﲠﺎر 1402
ﯾﮑﺸﻨﺒﻪ 23 ،ﲠﻤﻦ 1401 1
Based on Stanford CS161 Slides from Summer 2020 by Karey Shi
﮳ﺤ﮲ ﮶ﺴﻬﺎی ﻣﺮ ﮴ ﮳ﻂ در ﮐ ﮴ﺎب
( 2.3 :و 4.4 مو ب ) بو ●
زما (3 : ) ● ا
(: ) وس ای 3و 4اب د ا ار و ار ● واژه ام ی ا
http://sharif.edu/~ghodsi/books/ds-algf-dics-both.pdf
2
FROM LAST WEEK
THE POINT OF ASYMPTOTIC NOTATION
suppress constant factors and lower-order terms
too system dependent irrelevant for large inputs
Worst-case analysis:
What is the runtime of the algorithm on the worst possible input?
Best-case analysis:
What is the runtime of the algorithm on the best possible input?
Average-case analysis:
What is the runtime of the algorithm on the average input?
4
A NOTE ON RUNTIME ANALYSIS
There are a few different ways to analyze the runtime of an algorithm:
5
BIG-O NOTATION
Let T(n) & f(n) be functions defined on the positive integers.
(In this class, we’ll typically write T(n) to denote the worst case runtime of an algorithm)
6
BIG-O NOTATION
Let T(n) & f(n) be functions defined on the positive integers.
(In this class, we’ll typically write T(n) to denote the worst case runtime of an algorithm)
In English
7
BIG-O NOTATION
Let T(n) & f(n) be functions defined on the positive integers.
(In this class, we’ll typically write T(n) to denote the worst case runtime of an algorithm)
In English In Pictures
n0 c·f(n)
n (input size)
8
BIG-O NOTATION
Let T(n) & f(n) be functions defined on the positive integers.
(In this class, we’ll typically write T(n) to denote the worst case runtime of an algorithm)
n (input size)
9
BIG-O NOTATION
Let T(n) & f(n) be functions defined on the positive integers.
(In this class, we’ll typically write T(n) to denote the worst case runtime of an algorithm)
12
PROVING BIG-O BOUNDS
If you’re ever asked to formally prove that T(n) is O(f(n)), use the MATH definition:
T(n) = O(f(n))
⇔
∃ c , n0 > 0 s.t. ∀ n ≥ n0 ,
must be constants!
T(n) ≤ c · f(n) i.e. c & n0 cannot
depend on n!
“Let c = __ and n0 = __. We will show that T(n) ≤ c・f(n) for all n ≥ n0.”
13
PROVING BIG-O BOUNDS: EXAMPLE
T(n) = O(f(n))
⇔
∃ c , n0 > 0 s.t. ∀ n ≥ n0 ,
T(n) ≤ c · f(n)
17
DISPROVING BIG-O BOUNDS
If you’re ever asked to formally disprove that T(n) is O(f(n)), use proof by contradiction!
18
DISPROVING BIG-O BOUNDS
If you’re ever asked to formally disprove that T(n) is O(f(n)), use proof by contradiction!
19
DISPROVING BIG-O BOUNDS
If you’re ever asked to formally disprove that T(n) is O(f(n)), use proof by contradiction!
Conclude that the original assumption must be false, so T(n) is not O(f(n)).
you have triumphantly proven your silly (or lying) friend wrong. 20
DISPROVING BIG-O: EXAMPLE
T(n) = O(f(n))
⇔
Prove that 3n2 + 5n is not O(n). ∃ c , n0 > 0 s.t. ∀ n ≥ n0 ,
T(n) ≤ c · f(n)
For sake of contradiction, assume that 3n2 + 5n is O(n). This means that there exists
positive constants c & n0 such that 3n2 + 5n ≤ c・n for all n ≥ n0. Then, we would have
the following:
3n2 + 5n ≤ c・n
3n + 5 ≤ c
n ≤ (c - 5)/3
21
DISPROVING BIG-O: EXAMPLE
T(n) = O(f(n))
⇔
Prove that 3n2 + 5n is not O(n). ∃ c , n0 > 0 s.t. ∀ n ≥ n0 ,
T(n) ≤ c · f(n)
For sake of contradiction, assume that 3n2 + 5n is O(n). This means that there exists
positive constants c & n0 such that 3n2 + 5n ≤ c・n for all n ≥ n0.
Then, we would have the following:
3n2 + 5n ≤ c・n
3n + 5 ≤ c
n ≤ (c - 5)/3
22
DISPROVING BIG-O: EXAMPLE
T(n) = O(f(n))
⇔
Prove that 3n2 + 5n is not O(n). ∃ c , n0 > 0 s.t. ∀ n ≥ n0 ,
T(n) ≤ c · f(n)
For sake of contradiction, assume that 3n2 + 5n is O(n). This means that there exists
positive constants c & n0 such that 3n2 + 5n ≤ c・n for all n ≥ n0.
Then, we would have the following:
3n2 + 5n ≤ c・n
3n + 5 ≤ c
n ≤ (c - 5)/3
23
DISPROVING BIG-O: EXAMPLE
T(n) = O(f(n))
⇔
Prove that 3n2 + 5n is not O(n). ∃ c , n0 > 0 s.t. ∀ n ≥ n0 ,
T(n) ≤ c · f(n)
For sake of contradiction, assume that 3n2 + 5n is O(n). This means that there exists
positive constants c & n0 such that 3n2 + 5n ≤ c・n for all n ≥ n0.
Then, we would have the following:
3n2 + 5n ≤ c・n
3n + 5 ≤ c
n ≤ (c - 5)/3
24
BIG-O EXAMPLES
Polynomials
k k-1
Say p(n) = akn + ak-1n + ··· + a1n + a0 is a 6n3 + n log2n= O(n3)
polynomial of degree k ≥ 1.
Then:
i. p(n) = O(nk) 25 = O(1)
ii. p(n) is not O(nk-1)
[any constant] = O(1)
25
BIG-O EXAMPLES
lower order terms remember, big-O
don’t matter! is upper bound!
k k-1
Say p(n) = akn + ak-1n + ··· + a1n + a0 is a 6n3 + n log2n= O(n3)
polynomial of degree k ≥ 1.
Then:
i. p(n) = O(nk) 25 = O(1)
ii. p(n) is not O(nk-1)
[any constant] = O(1)
26
ﺳﻮال؟
27
BIG-Ω NOTATION
Let T(n) & f(n) be functions defined on the positive integers.
(In this class, we’ll typically write T(n) to denote the worst case runtime of an algorithm)
28
BIG-Ω NOTATION
Let T(n) & f(n) be functions defined on the positive integers.
(In this class, we’ll typically write T(n) to denote the worst case runtime of an algorithm)
In English
29
BIG-Ω NOTATION
Let T(n) & f(n) be functions defined on the positive integers.
(In this class, we’ll typically write T(n) to denote the worst case runtime of an algorithm)
In English In Pictures
n0 f(n)
T(n) = Ω(f(n)) if and only if
T(n) is eventually lower
Runtime (ms) T(n) Mathematical
bounded by a constant
c·f(n)
Definition
multiple of f(n)
n (input size)
30
BIG-Ω NOTATION
Let T(n) & f(n) be functions defined on the positive integers.
(In this class, we’ll typically write T(n) to denote the worst case runtime of an algorithm)
T(n) = O(f(n))
∃ c > 0, ∃ n0 > 0 s.t. ∀ n ≥ n0, T(n) ≤ c ᐧ upper bound
f(n)
T(n) = Ω(f(n))
∃ c > 0, ∃ n0 > 0 s.t. ∀ n ≥ n0, T(n) ≥ c ᐧ lower bound
f(n)
33
ﺳﻮال؟
34