You are on page 1of 2

Simple Divide and Conquer Algorithms

Counting Inversions

Inversion: a pair indices i, j such that i < j and A[i] > A[j]. Possible
to solve problem in O(n log n) time by using the Mergesort algorithm:
1

count_and_split ( A ) :
if n == 1: return A , 0
else :
(B , x ) = count_and_split ( left )
(C , y ) = count_and_split ( right )
(D , z ) = merge_and_count (B , C )

return D , x + y + z
9

11

13

15

17

19

merge_and_count (B , C ) :
i = j = inversions = 0
D = []
while :
if B [ i ] < C [ j ]:
D += B [ i ++]
else :
D += C [ j ++]
inversions += ( len ( B ) - i )
return D , inversions

Strassens Subcubic Matrix Multiplication

Traditional matrix multiplication is done as such:








a b
e f
ae + bg af + bh
X=
,Y =
;X Y = Z =
c d
g h
ce + dg cf + dh
The nave implementation of this algorithm requires 8 recursive calls
and runs in O(n3 ) time. Strassens algorithm computes the product of two
square matrices with 7 recursive calls and accomplishes a subcubic running
time that is still polynomial.

Simple Divide and Conquer Algorithms


The 7 products p1 . . . p7 are:
p1 = a(f h)
p2 = (a + b)h
p3 = (c + d)e
p4 = d(g e)
p5 = (a + d)(e + h)
p6 = (b d)(g + h)
p7 = (a c)(e + f )
And the final product is:


p5 + p4 p2 + p6 p1 + p2
Z=
p3 + p4
p3 p7

You might also like