Algo (2024) Homework 01
Algo (2024) Homework 01
004 - Algorithms
Jan-Apr Term, 2024
Homework Set 1
Due by: Week 2 Wednesday (31 Jan 2024) 1pm.
Please submit your homework online via eDimension.
Note: In this homework set, log denotes the natural logarithm\ i.e. the logarithm in base e.
Question 1. Let a and t be constants where t > 0 and a is a positive integer, and whose exact
values are unknown to us. Assume you have an algorithm that processes an array of size a in
exactly t seconds, and assume further that the running time of your algorithm is proportional
to some function f (n).
Given some constant T 2: t and an explicit expression for f(n), determine the largest integer
A such that your algorithm will be able to process an array of size A in at most T seconds.
Below, we list three possible candidate functions for f(n). For each f(n), please express your
final answer A in terms of a, t, and T. Please show all details, including all intermediate steps.
To get full credit, your answers should be simplified. For example, writing l l will not get you
full credit; you should instead simplify to a2 .
(i) J(n) = logn [3 points]
(ii) f(n) = nm, where mis a positive constant. 2 [3 points]
(iii) f(n) = mn, where mis a positive constant. 3 [4 points]
Hint: You may find the floor operation l x J useful. Recall that l x J denotes the largest integer
less than or equal to x.
Question 2. Recall the definition of the big-0 notation, where for two functions f(n) and g(n),
we write "f(n) = 0(g(n))" to mean there exist positive constants c1, c2, and no such that
for all n 2: no. In words, we say that f(n) has the same asymptotic growth rate as g(n). For
example, given f (n) = 3n2 - 2n, we can write "f(n) = 0(n2 )", meaning that we have identified
g(n) = n 2 to be a function with the same asymptotic growth rate as our f(n). To show why
this is true, we need to explicitly specify the corresponding constants c1, c2, no. In our example,
we claim that we can select positive constants c1 = 2, c2 = 3, and no= 2. To justify our claim,
we need to show why it is true that O ~ 2n2 ~ 3n2 - 2n ~ 3n2 for all n 2: 2.
(i) Explain in detail why it is true that O ~ 2n2 ~ 3n2 - 2n ~ 3n2 for all n 2: 2. [2 points]
Now it's your turn to try more examples. For each function f(n) given below, identify a
suitable g(n), together with the corresponding positive constants ci, c2, no, and explain in as
much details as possible why it is true that f(n) = 8(g(n)) for your identified g(n) and
constants c1, c2, no. To get full credit, your g(n) must be "as simplified as possible".
1 The logarithm notation is not consistently used throughout various academic disciplines, so what "log" (with
no subscript) means would depend on the context. For example, in mathematics (especially in functional analysis
and analysis-related areas), log by default is natural logarithm. In information theory, log is commonly used to
denote base 2 logarithm. In some engineering areas (e.g. communication theory), log is sometimes in base 10,
but sometimes in base 2. More theoretical engineering papers would use log to mean natural logarithm. The
common good practice in research papers is to specify which base is being used in logarithms, since there is
actually no "universally accepted convention".
2 You may express your final answer in terms of a, t, T, m.
1
(ii) f(n) = 5nlog(3n) + 3nlog(5n) + 2n. [4 points]
(iii) f(n) = 22n + 3n + 4n(2n). [4 points]
NOTE: For the correct g(n), there are multiple possible valid triples of values for (c1, c2, no).
You need to justify f(n) = 0(g(n)) for only one specific valid triple of values for (c1, c2, no).
Question 3.
(i) For any asymptotically non-negative 4 functions f and g, show that f(n) = 0(g(n)) if and
only if g(n) = 8(f(n)). [4 points]
(ii) For any real number a, any d > 0, any positive integer b, and any function f satisfying
both f(n) > 1 for all n, and f(n) = O(n), show that (dx f(n) +a)b = 0(f(nt). [6 points]
Question 4. Determine the time complexity of each Python code snippet below (in terms of
n) using the big-0 notation. You do not have to justify your answer, i.e. no partial points are
given for this question. Your answers should be simplified, and all logarithms in your answers
should be natural logarithms (i.e. in base e). [10 points]
(i) def f ( x) :
s = 0
for i in range(2**x):
s = s + i
ifx>=l:
return s + f (x~l)
else:
return s
print(f(n))
(ii) i = 1
while i < n:
print(i)
= 2**i
(iii) i 2
while < n:
l * i
for j in range ( i ) :
print(" Hello!")
(iv) 1
a []
b []
while i < n:
print(" g")
i = i+i
b. append ( i)
a. extend (b)
(v) j = 1
a []
while < n:
j *= 2
i += j
a.append(j)
4 Hint: What does "asymptotically non-negative" mean? Have you read the course textbook?