Tutorial 2 notes

COMPSCI 220 – Tutorial 2
Richard
Summary
Analysing/ comparing algorithms
Estimating running time
Single loop
(Number of elementary operations)
Nested loops
𝑓(𝑛)
Conditional/ switch statement
…
Big-O, Big-Ω, Big-Θ
Analysing
(𝑛
Time complexity :
:
𝑓(𝑛) asymptotically.
the input data size)
Big-Θ of running time
Big-O vs. Actual Running Time
Input data itself related: Average and Worse case
Recurrence (for algorithms by Divide-and-Conquer )
Time complexity:
When the Input Itself Matters

Average case




Specify the probability of different input (assumption)
Difficult to compute
Assumptions might not be realistic
Worst case


Upper bound
Might only be the case for a few very specific input
Algorithms based on Divide-and-Conquer

Recurrence relation



Implicit formula
Explicit formula, closed form
Two approaches


Guess and prove
Telescoping
Exercise 1
Prove that T(n) = 3n3 + 2n4 is not O(n3)
Suppose it is, then there must exist a c and n0 such that 3n3+2n4 <= cn3 for all
n>n0.
Rearrange the inequality we get c >= (3n3+2n4)/n3=3+2n.
Such constant c does not exist.
Exercise 2

Determine whether each statement is TRUE or FALSE
Statement
Rule of products
Transitivity
Limit rule
TRUE
𝑂 𝑓 + 𝑔 = 𝑂(max{𝑓, 𝑔})
𝑂 𝑓. 𝑔 = 𝑂 𝑓 . 𝑂(𝑔)
𝑂 𝑓 + 𝑔 = 𝑂 𝑓 + 𝑂(𝑔)
Rule of sums
Scaling
TRUE or FALSE
𝑖𝑓 π‘˜ > 0, 𝑓 𝑖𝑠 O(kf)
For all π‘˜ β‰  0, 𝑓 is 𝑂 π‘˜π‘“
If β„Ž = 𝑂 𝑔 , 𝑔 = 𝑂 𝑓 ,
π‘‘β„Žπ‘’π‘› β„Ž = 𝑂(𝑓)
If lim 𝑓(𝑛) = 10 , then
π‘›β†’βˆž 𝑔(𝑛)
𝑓=𝑂 𝑔
If
TRUE
TRUE
𝑓(𝑛)
, then
lim
=0
π‘›β†’βˆž 𝑔(𝑛)
𝑔=Ω 𝑓
If 𝑓 = 𝑂 𝑛 and 𝑔 = 𝑂 𝑛^2
then 𝑓 = 𝑂 𝑔
Counter example for the last one:
TRUE
𝑓 = 𝑂 𝑛 , and 𝑔 = 𝑂 𝑛^2
Then 𝑓 = 𝑂
𝑛^2
𝑓 = 𝑛, π‘Žπ‘›π‘‘ 𝑔 =
𝑛
Exercise 3
Assume the running time of an algorithm is described by
recurrence relation T(n) = 3T(n/3) + 1;T(1) = 1
Using telescoping find a closed form for T(n) and what is
the time complexity of the algorithm ?
n = 3m
T(3m) = 3T(3m-1) + 1
T(3m-1) = 3T(3m-2) + 1
T(3m) = 9T(3m-2) + 1 + 3
T(3m) = 1 + 31 + 32 + … + 3m
T(3m) = (3m+1 – 1)/2
T(3m) = (3n – 1)/2
Therefore the solution is Θ(n)
Exercise 4
What is the time complexity of the recurrence
T(n) = T(n/2) + n, T(1) = 0?
n = 2m
T(2m) = T(2m-1) + 2m
T(2m-1) = T(2m-2) + 2m-1
T(2m) = T(2m-2) + 2m-1 + 2m
T(2m) = T(20) + 21 + 22 + … + 2m
T(2m) = 2m+1 – 2
T(2m) = 2(n – 1)
Therefore the solution is Θ(n)
Exercise 5
What is the time complexity of the recurrence
T(n) = 2T(n/2) + n, T(1) = 1?
n = 2m
T(2m) = 2T(2m-1) + 2m
T(2m-1) = 2T(2m-2) + 2m-1
T(2m) = 4T(2m-2) + 2m + 2m
T(2m) = 2m + 2m + … + 2m
T(2m) = (m+1)(2m)
T(3m) = n(1+lgn)
Therefore the solution is Θ(nlgn)