5-Max Subarray and Strassen's Matrix Multiplication
5-Max Subarray and Strassen's Matrix Multiplication
2
MATRIX MULTIPLICATION
Input:
Output:
3
c=ab, where all a,b,c are square matrices. we will divide these
larger matrices into smaller sub matrices n/2
4
MATRIX MULTIPLICATION
Input: two n n matrices A and B
Output: C = AB,
n
cij aik bkj .
k 1
SQUARE-MATRIX-MULTIPLY(A, B)
n A.rows
let C be an n n matrix
for i 1 to n
for j 1 to n
cij 0
for k 1 to n
cij cij + aikbkj
return C 5
DIVIDE-AND-CONQUER ALGORITHM
IDEA:
n×n matrix = 2×2 matrix of (n/2)×(n/2) submatrices:
7
ANALYSIS OF D&C
ALGORITHM
T(n) = 8 T(n/2) + Θ(n2)
10
STRASSEN’S METHOD
14
MAXIMUM SUBARRAY SUM
Given: an array of integers (positive and negative), find the indices that give
the maximum contiguous subarray sum.
0 1 2 3 4 5 6 7
-3 2 4 -1 3 -10 6 -4
15
A DIVIDE-AND-CONQUER
SOLUTION
Possible locations of a maximum subarray
A[i..j] of A[low..high], where mid =
(low+high)/2
entirely in A[low..mid] (low i j mid)
entirely in A[mid+1..high] (mid < i j high)
crossing the midpoint (low i mid < j high)
crossing the midpoint
mid +1
low
i mid high
mid +1 j
A[i..mid]
18
MAXIMUM CONTIGUOUS
SUBARRAY SUM
1. Divide instance into subparts.
2. Solve the parts recursively.
3. Conquer by combining the answers
19
CONQUER
0 1 2 3 4 5 6 7
-3 2 4 -1 3 -10 6 -4
20
CONQUER
0 1 2 3 4 5 6 7
-3 2 4 -1 3 -10 6 -4
21
CROSSING SUBARRAYS
0 1 2 3 4 5 6 7
-3 2 4 -1 3 -10 6 -4
22
CROSSING SUBARRAYS
0 1 2 3 4 5 6 7
-3 2 4 -1 3 -10 6 -4
23
CROSSING SUBARRAYS
0 1 2 3 4 5 6 7
-3 2 4 -1 3 -10 6 -4
24
FINISHING THE RECURSIVE
CALL
0 1 2 3 4 5 6 7
-3 2 4 -1 3 -10 6 -4
25
IDEA OF DIVIDE-AND-CONQUER
Example: 10 15 -3 -4 -2 -1 8 5
max on L (recursion) 10 15
max on R (recursion) 8 5
mid extend to L 10 15 -3 -4
mid extend to R -2 -1 8 5
Possible candidates:
25, 13, 28 (=18+10)
overall maximum 28, i.e., take all numbers
26
IDEA OF DIVIDE-AND-CONQUER
Example: -2 5 -1 -5 2 -1 2
max on L (recursion) 5
max on R (recursion) 2 -1 2
mid extend to L 5 -1
mid extend to R not take any
Possible candidates:
5, 3, 4 (=4+0)
overall maximum 5
27
FIND-MAXIMUM-SUBARRAY (A, low, high)
if high == low
Return (low, high, A[low]) // base case: only one element
else mid = low high / 2
(left-low, left-high, left-sum) =
FIND-MAXIMUM-SUBARRAY(A, low, mid)
(right-low, right-high, right-sum) =
FIND-MAXIMUM-SUBARRAY(A, mid + 1, high)
(cross-low, cross-high, cross-sum) =
FIND-MAX-CROSSING-SUBARRAY(A, low, mid, high)
if left-sum ≧ right-sum and left-sum ≧ cross-sum
return (left-low, left-high, left-sum)
elseif right-sum ≧ left-sum and right-sum ≧ cross-sum
return (right-low, right-high, right-sum)
else return (cross-low, cross-high, cross-sum)
S[5 .. 5] = -3
S[4 .. 5] = 17 (max-
left = 4)
S[3 .. 5] = -8
S[2 .. 5] = -11
mid =5
S[1 .. 5] = 2
1 2 3 4 5 6 7 8 9 10
A 13 -3 -25 20 -3 -16 -23 18 20 -7
S[6 .. 6] = -16
S[6 .. 7] = -39
S[6 .. 8] = -21
S[6 .. 9] = (max-right = 9) -
1
S[6..10] = -
maximum subarray
8 crossing mid is S[4..9] = 16
ANALYZING TIME COMPLEXITY
FIND-MAX-CROSSING-SUBARRAY : (n),
where n = high low + 1
FIND-MAXIMUM-SUBARRAY
T(n) = 2T(n/2) + (n) (with T(1) = (1))
= (nlg n) (similar to merge-sort)
31
RUNNING TIME
32