CSC373: A1: Ds - Mult
CSC373: A1: Ds - Mult
1 Question 1
1.1 a.
1 1 1 1
1 −1 1 −1
M2 =
1 1 −1 −1
1 −1 −1 1
1.2 b.
v⃗
Let v⃗k = 1
v⃗2
Where v⃗1 is the first half of the vector v⃗k (with shape ( k2 , 1)), and v⃗2 is the second half of v⃗k
Mk−1 v⃗1 + Mk−1 v⃗2
DS-Mult(v⃗k ) =
Mk−1 v⃗1 − Mk−1 v⃗2
Mk−1 v⃗1 result in a product that has shape ( k2 , 1), the end result is a vertical stack of the products.
We only need to compute the following matrix multiplications: Mk−1 v⃗1 and Mk−1 v⃗2 . The requires recursively calling the
DS-Mult on v⃗1 , v⃗2 . The combining step will require vector add of size k2 and stack the product on another. This operation takes
at most O(n).
Therefore the recurrence relation is T (n) = 2T ( n2 ) + cn, this means a = 2, b = 2, d = 1. According to master theorem, this
algorithm runs in O(nlogn).
The pseudocode is as follows:
def ds_mult(v):
if len(v) == 1:
# Then k = 0, M_0 = [1], product is v itself
return v
mid = len(v) // 2
v1 = v[:mid]
v2 = v[mid:]
ds1 = ds_mult(v1) # T(n / 2)
ds2 = ds_mult(v2) # T(n / 2)
first_half = vector_add(ds1, ds2) # O(n)
second_half = vector_add(ds1, -ds2) # O(n)
return vertical_concat(first_half, second_half) # O(n)
2 c.
In each step of the algorithm, we need to solve two sub-problems of size n2 , In the combining steps, we need to perform two
vector add of size n2 , and then append one of the vector onto another, this results in total of 3n
2 operation which is in linear time
O(n). The recurrence relationis therefore:
n
T (n) = 2T ( ) + cn
2
Where a = 2, b = 2, d = 1. a = bd , therefore according to Master theorem, the runtime is bounded by O(nd logn) = O(nlogn)