Transcript Lecture7C
Design and Analysis of Algorithms
Maximum-subarray problem and matrix
multiplication
Haidong Xue
Summer 2012, at GSU
Maximum-subarray problem
back ground
β’ If you know the price of certain stock from day
i to day j;
β’ You can only buy and sell one share once
β’ How to maximize your profit?
120
110
Price
100
90
80
70
60
0
1
2
3
4
5
6
7
8
day #
9
10
11
12
13
14
15
16
Maximum-subarray problem
back ground
β’ What is the brute-force solution?
max = -infinity;
for each day pair p {
if(p.priceDifference>max)
max=p.priceDifference;
}
Time complexity?
π
2
pairs, so O(π2 )
Maximum-subarray problem
back ground
β’ If we know the price difference of each 2
contiguous days
β’ The solution can be found from the
maximum-subarray
β’ Maximum-subarray of array A is:
β A subarray of A
β Nonempty
β Contiguous
β Whose values have the the largest sum
Maximum-subarray problem
back ground
Day
Price
Difference
0
10
1
11
1
What is the solution?
2
7
-4
3
10
3
4
6
-4
Buy on day 2, sell on day 3
Can be solve it by the maximum-subarray of difference array?
Sub-array
Sum
1-1
1
1-2
-3
1-3
0
1-4
-4
2-2
-4
2-3
-1
2-4
-5
3-3
3
3-4
-1
4-4
-4
Maximum-subarray problem β divideand-conquer algorithm
β’ How to divide?
β Divide to 2 arrays
β’ What is the base case?
β’ How to combine the sub problem solutions to the
current solution?
β A fact:
β’ when divide array A[i, β¦, j] into A[i, β¦, mid] and A[mid+1, β¦, j]
β’ A sub array must be in one of them
β A[i, β¦, mid] // the left array
β A[mid+1, β¦, j] // the right array
β A[ β¦, mid, mid+1β¦.] // the array across the midpoint
β The maximum subarray is the largest sub-array among
maximum subarrays of those 3
Maximum-subarray problem β divideand-conquer algorithm
β’ Input: array A[i, β¦, j]
β’ Ouput: sum of maximum-subarray, start point of maximumsubarray, end point of maximum-subarray
β’ FindMaxSubarray:
1. if(j<=i) return (A[i], i, j);
2. mid = floor(i+j);
3. (sumCross, startCross, endCross) = FindMaxCrossingSubarray(A, i,
j, mid);
4. (sumLeft, startLeft, endLeft) = FindMaxSubarray(A, i, mid);
5. (sumRight, startRight, endRight) = FindMaxSubarray(A, mid+1, j);
6. Return the largest one from those 3
Maximum-subarray problem β divideand-conquer algorithm
FindMaxCrossingSubarray(A, i, j, mid)
1. Scan A[i, mid] once, find the largest A[left,
mid]
2. Scan A[mid+1, j] once, find the largest
A[mid+1, right]
3. Return (sum of A[left, mid] and A[mid+1,
right], left, right)
Letβs try it in Java
Target array :
1
All the sub arrays:
1
-4
3
2
1
-4
-4
3
3
2
1
-3
-4
-4
Max!
1
-1
3
3
1
2
2
5
0
-4
3
-4
3
2
1
-4
3
2
2
Target array :
1
All the sub arrays:
1
-4
3
1
-4
-4
The problem can be
then solved by:
2
1
1
-1
3
3
1
2
-3
-4
-4
3. Find the max in
crossing sub arrays
4. Choose the largest
one from those 3 as the
final result
3
3
1. Find the max in left
sub arrays
2. Find the max in right
sub arrays
Divide target array into 2
arrays
2
2
5
0
-4
3
-4
3
2
1
-4
3
2
2
We then have 3 types of
subarrays:
The ones belong to the left
array
The ones belong to the right
array
The ones crossing the mid
point
FindMaxSub (
1
-4
3
)
2
1. Find the max in left sub arrays
FindMaxSub (
1
-4
2. Find the max in right sub arrays FindMaxSub (
3
2
3. Find the max in crossing sub arrays
Scan
1
-4
once, and scan
3
2 once
4. Choose the largest one from those 3 as the final
result
)
)
3. Find the max in crossing sub arrays
1
1
The largest crossing subarray is :
-4
3
2
-4
Sum=-4
-4
Sum=-3
3
Sum=3
3
2
largest
Sum=5
largest
Maximum-subarray problem β divideand-conquer algorithm
β’ What is the time complexity?
β’ FindMaxSubarray:
1. if(j<=i) return (A[i], i, j);
2. mid = floor(i+j);
3. (sumCross, startCross, endCross) =
FindMaxCrossingSubarray(A, i, j, mid);
4. (sumLeft, startLeft, endLeft) = FindMaxSubarray(A, i,
mid);
5. (sumRight, startRight, endRight) =
FindMaxSubarray(A, mid+1, j);
6. Return the largest one from those 3
π π = 2π
π
2
+ Ξ(n)
Matrix multiplication
β’ How to multiply two matrices?
β9 β3
= β1 2
2
1
β’ Given matrix π΄ππ πππ π΅ππ , πΆππ = π΄π΅
Time Complexity?
β’ πππ = ππ=1 πππ πππ
β’ For each πππ , we need Ξ(π)
β’ There are π2 πππ , so π π = π2 Ξ(π)=Ξ(π3 )
Matrix multiplication
divide-and-conquer algorithm
β’ C=AB
β’
β’
β’
β’
β’
β’
πΆ11 πΆ12
π΄11 π΄12
π΅11
=
Γ
π΅21
πΆ21 πΆ22
π΄21 π΄22
πΆ11 = π΄11 Γ π΅11 + π΄12 Γ π΅21
πΆ12 = π΄11 Γ π΅12 + π΄12 Γ π΅22
πΆ21 = π΄21 Γ π΅11 + π΄22 Γ π΅21
πΆ22 = π΄21 Γ π΅12 + π΄22 Γ π΅22
Recurrence equation?
β’ π π = 8π
π
2
+ Ξ(π2 )
π΅12
π΅22
Matrix multiplication
divide-and-conquer algorithm
β’ π π = 8π
π
2
+ Ξ(π2 )
β’ What is the time complexity?
β’ From Master method we know it is Ξ(π3 )
Matrix multiplication
Strassenβs algorithm
β’ π π = 8π
π
2
+ Ξ(π2 )
β’ π π = 7π
π
2
+ Ξ(π2 )
Matrix multiplication
Strassenβs algorithm
β’ Strassenβs algorithm
β’ 1. Perform 10 times matrix addition or
subtraction to make π1 π‘ππ10 from π΄ππ πππ π΅ππ
β’ 2. Perform 7 times matrix multiplication to
make π1 to π7 from π΄ππ , π΅ππ πππ ππ
β’ 3. Perform matrix addition or matrix
subtraction to obtain πΆ11 , πΆ12 , πΆ21 and πΆ22
π π = 7π
π
2
+ Ξ(π2 )= Ξ(ππππ27 )