
































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

































Sanjay Ranka
September 13, 2002
Divide and Conquer Method
At each level of recursion:
or, for small subproblems, solve in a straightforward
manner.
solution of the original problem.
Merge Sort
Maximum Subsequence Problem: Algorithm
Step 1. Set M AXSU M = 0.
Step 2. Calculate s(i, j) =
∑j
k=i
a[k], and update the
M AXSU M by s(i, j) if s(i, j) > M AXSU M ; for
1 ≤ i ≤ j ≤ n.
Step 3. M AXSU M is the answer.
Analysis: In s(i, j) we sum (j − i + 1) terms.
Therefore the complexity of the algorithm is:
n ∑
i=
n ∑
j=i
j ∑
k=i
n ∑
i=
n ∑
j=i
(j − i + 1)
n ∑
i=
(n + i + 1)(n − i + 2)
n ∑
i=
{(n
2
2
}
n
3
2
= O(n
3
Note that s(i, j) =
∑j
k=i
a[k] = s(i, j − 1) + a[j].
Step 4. Find M AXSU M =
max(M AXSU M (L), M AXSU M (R)).
Flaw - What happens if the maximum sum is due to
terms in the middle of of the sequence as in the
following example?
Here M AXSU M (L) is 6 due to sum of first three
terms, M AXSU M (R) is 8 due to sum of 6th and 7th
terms, and M AXSU M is 11 due to sum of terms 1
through 7.
calculate
BORDER(i, L) = BORDER(i + 1, L) + a[i].
Step 4b. If BORDER(i, L) > M AXSU M B
(L) then
B
(L) = BORDER(i, L).
Step 5. As step 4 except for the right half of the sequence.
Step 6. Finally,
max(M AXSU M (L), M AXSU M (R),
B
B
Analysis : If T (n) denotes the total number of basic
operations of addition, then T (n) = 2T (n/2) + n. Here
we have ignored the costs of managing of the stacks,
assignments etc. The term n represents the cost of
adding sums from the border to the edges. Solution of
this equation, with T (1) = 1 gives T (n) = n lg n + n.
Alternatively, let
n
U 1
0
and V = 2
n
V 1
0
where U 1
= (u 2 n− 1
u 2 n− 2
... u n
2
and
0
= (u n− 1
u n− 2
... u 0
2
Likewise, V 1
= (v 2 n− 1
v 2 n− 2
... v n
2
and
0
= (v n− 1
u n− 2
... v 0
2
. Then,
2 n
U 1
1
n
(U 1
0
0
1
0
0
Apparently there is no saving, this multiplication also
requires execution time Θ(n
2
).
Multiplication: Divide and Conquer
2 n
n
)U 1
1
n
(U 1
0
0
1
n
+1)U 0
0
which requires only three multiplications and some
extra addition. If T (n) denotes time to multiply two
binary integers of size n each, then
T (2n) = 3T (n) + cn, and T (1) = 1,
where cn denotes the cost associated with additions and
shifting of binary integers etc.
Matrix multiplication
Let C = A × B = (c ij
) be the product of two n × n
matrices A and B, where
a 11
a 12
... a 1 n
a 21
a 22
... a 2 n
a n 1
a n 2
... a nn
and B =
b 11
b 12
... b 1 n
b 21
b 22
... b 2 n
b n 1
b n 2
... b nn
2
terms, c ij
for
i = 1, 2 ,... , n, j = 1, 2 ,... , n.
requires n scalar multiplications.
multiplication is of the order n
3
.
Suppose n = 2
k
for some integer value of k. Let
r
. s
t
. u
a
. b
c
. d
e
. g
f
. h
where r, s, t, u are all
n
2
n
2
matrices.
B in submatrices, we calculate 4 matrices
r, s, t, and u.
Matrix Multiplication: Strassen’s Algorithm
1
= a(g − h) = ag − ah
2
= (a + b)h = ah + bh
3
= (c + d)e = ce + de
4
= d(f − e) = df − de
5
= (a + d)(e + h) = ae + ah + de + dh
6
= (b − d)(f + h) = bf + bh − df − dh
and
7
= (a − c)(e + g) = ae + ag − ce − cg
Then, we can obtain r, s, t, and u as follows:
r = P 5
4
2
6
s = P 1
2
t = P 3
4
u = P 5
1
3
7
For example,
5
4
2
6
= (ae + ah + de + dh) + (df − de)
= ae + bf
= r