0% found this document useful (0 votes)
13 views2 pages

CSC373: A1: Ds - Mult

This document contains Sherry Yuan's assignment on question 1. Question 1 involves analyzing the runtime of a divide-and-conquer algorithm for matrix-vector multiplication. The algorithm recursively divides the problem into subproblems of half the size, combines the solutions with vector additions, and is analyzed using the master method. The runtime is determined to be O(n log n). Pseudocode is provided to demonstrate how the algorithm works at a high level.

Uploaded by

sherry yuan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views2 pages

CSC373: A1: Ds - Mult

This document contains Sherry Yuan's assignment on question 1. Question 1 involves analyzing the runtime of a divide-and-conquer algorithm for matrix-vector multiplication. The algorithm recursively divides the problem into subproblems of half the size, combines the solutions with vector additions, and is analyzed using the master method. The runtime is determined to be O(n log n). Pseudocode is provided to demonstrate how the algorithm works at a high level.

Uploaded by

sherry yuan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

CSC373: A1

Sherry Yuan: September 18, 2022

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)

def vector_add(v1, v2): # O(n)


return [v1[i] + v2[i] for i in range(len(v1))]

def vertical_concat(v1, v2): # O(n)


for item in v2:
v1.append(v2)
return v1

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)

You might also like