Ch1 Handout
Ch1 Handout
Chapter 1
Elementary analysis
Contents
Advanced algorithms on March 3, 2021
Definition and
notations
Algorithm
Complexity
Formulas
Formulas Complexity
Formulas
Ch.1, p.2/32
Elementary analysis
Algorithm
Huynh Tuong Nguyen
What is an algorithm?
An algorithm is a finite set of precise instructions for performing a
computation or for solving a problem. Contents
Definition and
notations
Properties of algorithms Algorithm
Complexity
Ch.1, p.3/32
Elementary analysis
Complexity
Huynh Tuong Nguyen
Definition and
notations
Input size Algorithm A Algorithm B
Algorithm
n 5000 n 1.2n Complexity
10 50,000 6 Formulas
Ch.1, p.4/32
Elementary analysis
Types of formulas for basic operation count
Huynh Tuong Nguyen
Ch.1, p.5/32
Elementary analysis
Complexity classes - a small vocabulary
Huynh Tuong Nguyen
• Quadratic: O(n )
2
Basic methods for
asymptotic behaviour
• Cubic: O(n3 ) analysis
Counting number of
• Polynomial: O(np ) (O(n2 ), O(n3 ), etc) elementary operations
Solving recurrences
• Quasi-polynomial: O(nlog(n) )
• Exponential: O(2n )
• Factorial: O(n!)
Ch.1, p.6/32
Elementary analysis
Asymptotic upper bound - worst case
Huynh Tuong Nguyen
Ch.1, p.7/32
Elementary analysis
Asymtotic lower bound - best case
Huynh Tuong Nguyen
+ Definition and
T (n) = Ω(f (n)) iif ∃c ∈ R , c > 0 and ∃n0 ∈ N , n0 > 0 notations
Ch.1, p.8/32
Elementary analysis
Asymtotic approximating bound - average case
Huynh Tuong Nguyen
“big Theta”
T (n) = Θ(f (n)) iif ∃c1 , c2 ∈ R+ , c1 > 0, c2 > 0 and ∃n0 ∈ N ,
Contents
n0 > 0 Definition and
such that ∀n > n0 : c1 × f (n) ≤ T (n) ≤ c2 × f (n) notations
Algorithm
Complexity
Formulas
Property
Basic methods for
asymptotic behaviour
T (n) = O(f (n)) and T (n) = Ω(f (n)) =⇒ T (n) = Θ(f (n)) analysis
Counting number of
elementary operations
Ch.1, p.9/32
Elementary analysis
Other properties
Huynh Tuong Nguyen
Not transitive
• f (n) = n2 ; g(n) = n
• ⇒ f (n) = O(n2 ) = g(n) but f (n) 6= g(n)
Contents
Definition and
Transitivity notations
Algorithm
• f (n) = Ω(g(n)) & g(n) = Ω(h(n)) ⇒ f (n) = Ω(h(n)) Basic methods for
asymptotic behaviour
analysis
• f (n) = Θ(g(n)) & g(n) = Θ(h(n)) ⇒ f (n) = Θ(h(n)) Counting number of
elementary operations
Solving recurrences
Additivity
Ch.1, p.10/32
Elementary analysis
Exercise
Huynh Tuong Nguyen
Contents
Definition and
notations
Algorithm
a T (n) = 3 + 5n2 ⇒ T (n) = Θ(n2 ) ? Complexity
Formulas
2n + 5 when n is even
b if T (n) = , then T (n) = O(?)
n2 − n + 1 when n is odd Basic methods for
asymptotic behaviour
and T (n) = Ω(?). analysis
Counting number of
elementary operations
Solving recurrences
Ch.1, p.11/32
Elementary analysis
Exercise
Huynh Tuong Nguyen
Ch.1, p.12/32
Elementary analysis
Common asymptotic behaviours
Huynh Tuong Nguyen
Contents
Definition and
Size Approximate computational time notations
n Θ(log n) Θ(n) Θ(n log n) Θ(n2 ) Θ(2n ) Θ(n!) Algorithm
Ch.1, p.13/32
Elementary analysis
Example
Huynh Tuong Nguyen
Contents
1. (1)Var int: d = 0 Definition and
notations
2. (n)For i from 1 to n do Algorithm
1. (1)d = d + 1 Complexity
Formulas
2. (1)a[i] = a[i] × a[i] + d × d
Basic methods for
asymptotic behaviour
3. Endfor analysis
Counting number of
elementary operations
Solving recurrences
Ch.1, p.14/32
Elementary analysis
Linear loop example
Huynh Tuong Nguyen
Contents
Ch.1, p.15/32
Elementary analysis
Logarithmic loop example
Huynh Tuong Nguyen
Contents
Ch.1, p.16/32
Elementary analysis
Nested loop example
Huynh Tuong Nguyen
Definition and
notations
Algorithm
2. j = j × 3 1 + 2n + 2n × log3 (n).
3. EndWhile
4. i=i+1
3. EndWhile
Ch.1, p.17/32
Elementary analysis
Exercise
Huynh Tuong Nguyen
5. (1)a[j + 1] = t
3. EndFor
Ch.1, p.18/32
Elementary analysis
Exercise
Huynh Tuong Nguyen
√ Solving recurrences
• T8 (n) = n
√
• T9 (n) = 3 n2
• T10 (n) = 2n
• T11 (n) = n!
Ch.1, p.19/32
Elementary analysis
Solving recurrences
Huynh Tuong Nguyen
Logarithmic recurrence
T (n) = 1 + T (n/2) = 1 + (1 + T (n/4)) Contents
Ch.1, p.23/32
Elementary analysis
Exercise
Huynh Tuong Nguyen
1. If n ≤ 1 then
Contents
1. Return 1
Definition and
notations
2. Else Algorithm
1. If a[n] ≤ 1 then
1. Return 1
2. Else
1. Return a[n] + SumUp(n-1,a[])
3. EndIf
Ch.1, p.24/32
Elementary analysis
Exercise
Huynh Tuong Nguyen
Ch.1, p.25/32
Elementary analysis
Exercise
Huynh Tuong Nguyen
• Quasi-polynomial: O(nlog(n) )
• Exponential: O(2n )
• Factorial: O(n!)
Ch.1, p.26/32
Elementary analysis
Exercise
Huynh Tuong Nguyen
Contents
Definition and
notations
√ Algorithm
Solve recurrence f (n) = 5f ( n) + log3 n.
3
Complexity
Ch.1, p.32/32