0% found this document useful (0 votes)
112 views

Algorithm Assignments Fall Semester

The matrices are partitioned and intermediate matrices S1-S10 and P1-P7 are computed. Strassen's algorithm recursively computes the products P1-P7. The final matrix product is computed from combinations of P1-P7 in O(n^2.807) time, an improvement over the O(n^3) time of standard matrix multiplication.

Uploaded by

asdasa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
112 views

Algorithm Assignments Fall Semester

The matrices are partitioned and intermediate matrices S1-S10 and P1-P7 are computed. Strassen's algorithm recursively computes the products P1-P7. The final matrix product is computed from combinations of P1-P7 in O(n^2.807) time, an improvement over the O(n^3) time of standard matrix multiplication.

Uploaded by

asdasa
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Q1: What is the smallest value of n such that an algorithm whose running time is 100n 2 runs faster

than an algorithm whose running time is 2n on the same machine?

Ans:

For inputs of size n, running time of algorithm A is 100n2 and of B is 2n. For A to run faster than B, 100n2
must be smaller than 2n.

Calculate: A (quadratic time complexity) will run much faster than B (exponential time complexity) for
very large values of n. Let’s start checking from n=1 and go up for values of n which are power of 2.

Somewhere between 8 and 16, A starts to run faster than B. Let’s do what we were doing
but now we are going to try middle value of the range, repeatedly (binary search).

So, at n=15n=15, A starts to run faster than B.

Q:2 Consider the searching problem:

Input: A sequence of n numbers A= (a1, a2, …, an) and a value v.

Output: An index I such that v = A[i] or the special value NIL if v does not appear in A.
Write pseudocode for linear search, which scans through the sequence, looking for v. Using a
loop invariant, prove that your algorithm is correct. Make sure that your loop invarieant fulfills the
three necessary properties.

Ans2:

We will state the loop invariant as:

At the start of each iteration of the for loop, the subarray A[1..i−1] consists of elements that are
different than ν.

Here are the three properties:

Initialization

Initially the subarray is the empty array, so proving it is trivial.

Maintenance
On each step, we know that A[1..i−1] does not contain ν. We compare it with A[i]. If they are the same,
we return i, which is a correct result. Otherwise, we continue to the next step. We have already insured
that A[A..i−1] does not contain ν and that A[i] is different from ν, so this step preserves the invariant.

Termination

The loop terminates when i>A.length. Since i increases by 1 and i>A.length, we know that all the
elements in A have been checked and it has been found that ν is not among them. Thus, we return NIL.

pseudocode:

SEARCH(A, v):

for i = 1 to A.length

if A[i] == v

return i

return NIL

Q3: Explain why the statement, “The running time of algorithm A is at least O (n2),” is meaningless.

Ans: Let T(n) be the running time for algorithm A and let a function f(n) = O(n2).

The statement says that T(n) is at least O(n2). That is, T(n) is an upper bound of f(n). Since f(n) could be
any function “smaller” than n2 (including constant function), we can rephase the statement as “The
running time of algorithm A is at least constant.” This is meaningless because the running time for every
algorithm is at least constant.

Q4: Write pseudo code for the brute-force method of solving the maximum sub-array problem. Your
procedure should run in Ɵ (n2) time.

Ans:

pseudo code for maximum sub-array

FIND-MAX-SUBARRAY(A, low, high)

left = 0

right = 0

sum = -∞

for i = low to high


current-sum = 0

for j = i to high

current-sum += A[j]

if sum < current-sum

sum = current-sum

left = i

right = j

return (left, right, sum)

Running time: line 1, took O(1) time, for giving a value, the loop in line 2 take O(n) as it go through the
elements from 2 to n and line 3 and 4 only took O(1) time, line 5 took O(1) time, so the running time is
T(n) = O(1) + O(n)∗(O(1) + O(1)) + O(1) = O(n).

QNo5 Use Strassen’s algorithm to compute the matrix product of the following matrices. Show your
work.

 1 3  6 8 
  
 7 5  4 2 

Ans

The first matrices are

S1=6, S2=4, S3=12, S4=−2, S5=5, S6=8, S7=−2, S8=6, S9=−6, S10=14.

The products are


The four matrices are

The result is

pseudocode for Strassen's algorithm.

STRASSEN(A, B)
n = A.rows
let C be a new n × n matrix
if n == 1
c[1, 1] = a[1, 1] * b[1, 1]
else
partition A and B in equations (4.9)
let C[1, 1], C[1, 2], C[2, 1], and C[2, 2] be n / 2 × n / 2 matrices
create n / 2 × n / 2 matrices S[1], S[2], ..., S[10] and P[1], P[2],
..., P[7]
S[1] = B[1, 2] - B[2, 2]
S[2] = A[1, 1] + A[1, 2]
S[3] = A[1, 2] + A[2, 2]
S[4] = B[2, 1] - B[1, 1]
S[5] = A[1, 1] + A[2, 2]
S[6] = B[1, 1] + B[2, 2]
S[7] = A[1, 2] - A[2, 2]
S[8] = B[2, 1] + B[2, 2]
S[9] = A[1, 1] - A[2, 1]
S[10] = B[1, 1] + B[1, 2]
P[1] = STRASSEN(A[1, 1], S[1])
P[2] = STRASSEN(S[2], B[2, 2])
P[3] = STRASSEN(S[3], B[1, 1])
P[4] = STRASSEN(A[2, 2], S[4])
P[5] = STRASSEN(S[5], S[6])
P[6] = STRASSEN(S[7], S[8])
P[7] = STRASSEN(S[9], S[10])
C[1, 1] = P[5] + P[4] - P[2] + P[6]
C[1, 2] = P[1] + P[2]
C[2, 1] = P[3] + P[4]
C[2, 2] = P[5] + P[1] - P[3] - P[7]
combine C[1, 1], C[1, 2], C[2, 1], and C[2, 2] into C
return C

You might also like