Algo (2024) Homework 02
Algo (2024) Homework 02
004 - Algorithms
January- April Term, 2024
Homework Set 2
Due by: Week 3 Wednesday (7 Feb 2024) 1pm.
Please submit your homework online via eDimension.
Note: In this homework set (and throughout the course), log denotes the natural logarithm,
i. e. the logarithm in base e.
Following the notation used in the textbook, we write A [l..n] to mean an array A indexed from
1 ton. This means the first entry of A is A[l].
Question 1. For each of the five recurrence relations below, use the master theorem to find
asymptotic upper and lower bounds for T(n). Make your bounds as tight as possible, and
justify your answers . If master theorem applies, please explicitly indicate which case of master
theorem applies . If master theorem does not apply, please indicate so, and explain why. You
may assume that T(n) is constant for n::; 3. [10 points]
Question 2. Consider the following operation SORTIT, which takes as its input an array A,
and which does not return any output , but modifies A directly.
function SORTIT(A)
Require: A[l..n] is an array with numerical entries.
1: n +- A.length
2: count+- 2
3: while count ::; n do
4: if count= 1 or A[count] 2:: A [count - 1] then
5: count +- count + 1
6: else
7: swap A [count] and A [count - 1]
8: count +- count - 1
(i) Given that A[l..n] is an array with numerical values, justify with as much details as
possible why running SORTIT(A) would correctly sort the input array A in ascending
order. Please explain in your own words. [4 points]
(ii) Design an algorit hm that takes as its inputs two integer arrays A and B (possibly of
different lengths, and possibly with repeated entries), that returns as its output a single
array sorted in descending order , where the entries of this output array are precisely
all the entries in the two input arrays, and such that the first two lines of the algorithm
are SORTIT(A) and SORTIT(B), respectively. Please present your algorithm in pseudocode.
For example, the start of your algorithm should look like the following pseudocode snippet
MYSORT. [6 points]
1
function MYSORT(A, B)
Require: A[l..n] is an integer array.
Require: B[l..m] is an integer array.
1: SORTIT(A)
2: SORTIT(B)
3:
HINT: Lines such as "Require: A [l..n] is an integer array." are technically comments to help
us understand the requirements/assumptions of the function; these lines do not actually get
executed by the machine. Hence, the machine does not "know" that n refers to the length of
the input array A. If you have a line of code that refers to n, before defining what n is, then
your code would not work. For example, in the pseudocode for SORTIT, if line 1 is omitted, then
line 3 does not make sense!
function EXPONENTIAL(a, n)
Require: a is a real number.
Require: n is a non-negative integer.
1: if n = 0 then
2: return 1
3: else
4: return a* EXPONENTIAL(a, n - 1)
(i) Given a real number a and any non-negative integer n, we can compute the value of
an by running EXPONENTIAL(a, n), which runs in 0(n) time. Using a divide and conquer
approach, design an algorithm 1 that takes as its inputs a real number a and a non-negative
integer n, and that returns as its output the value an. Please present your algorithm
in pseudocode. To get full credit, your algorithm should be recursive, and should run
in O(logn) time, no matter what the value of a is. Please also justify why the time
complexity of your algorithm is O(logn) . [6 points]
HINT: The floor operation is useful!
(ii) Given a real number a and any non-negative integer n, we can similarly compute the
value of ann by running EXPONENTIAL(a,EXPONENTIAL(n,n)). (Be careful! ann equals
a(nn), which is not the same as (anr.) Design a new algorithm that takes as its inputs
a real number a and a non-negative integer n, and that returns as its output the value
ann. Please present your new algorithm in pseudocode. For this new algorithm, you may
use your algorithm from part (i) as a subroutine, or you may design a completely new
algorithm "from scratch". Assuming that a is some given fixed constant, what is the time
complexity (in terms of n) for your new algorithm? Justify your answer with as much
details as possible. [4 points]
HINT: If your new algorithm (presented as a new operation) uses your operation from
part (i) as a subroutine, then be careful of the name you give to your new operation.
1 You may assume that you have access to the usual four arithmetic operations+ , - , *,/ (plus, minus, times,
divide) for real numbers, and you may assume that a single use of any of these four arithmetic operations (on
a pair of real numbers) runs in 0(1) time. You may also assume that you have access to the floor operation,
which also runs in 0(1) time. However , for this question, you do not have access to any in-built exponentiation
operations.
2
Question 4. Design an algorithm that takes as its three inputs A, B , k, where A and B are
integer arrays of the same length, and where k is an integer , such that the algorithm returns as
its output either the character string "true", if there exist an entry a from A, and an entry b
from B, satisfying a+b = k, or the character string "false" otherwise. Note that the entries of A
and B could have repeated values. Please present your algorithm in pseudocode. In particular,
your algorithm should return "false" if A and B are empty arrays. To get full credit, your
algorithm should run in O(nlogn) time. Please also justify why the time complexity of your
algorithm is O(nlogn). [10 points]