Algorithm Assignments Fall Semester
Algorithm Assignments Fall Semester
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).
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:
At the start of each iteration of the for loop, the subarray A[1..i−1] consists of elements that are
different than ν.
Initialization
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:
left = 0
right = 0
sum = -∞
for j = i to high
current-sum += A[j]
sum = current-sum
left = i
right = j
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
S1=6, S2=4, S3=12, S4=−2, S5=5, S6=8, S7=−2, S8=6, S9=−6, S10=14.
The result is
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