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 100 Price 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 2 7 -4 3 10 3 4 6 -4
What is the solution?
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)
Lets try it in Java
Target array :
1 1
-4
All the sub arrays:
1
-4 3 2 -4 3 2 -3 3 3 2 -1 5 0 2 2 1 2
-4 -4
Max! 1 -4 -4 1 -4
3 3 3
Target array :
1 1
-4
Divide target array into 2 arrays
All the sub arrays: The problem can be then solved by: 1. Find the max in left sub arrays 2. Find the max in right sub arrays 3. Find the max in crossing sub arrays
1
-4 3 2 -4 3 2 -3 3 3 2 -1 5 0 2 2 1 2
We then have 3 types of subarrays: The ones belong to the left array The ones belong to the right array
-4 -4
The ones crossing the mid point
-4 -4
3 3 3
4. Choose the largest one from those 3 as the final result
-4
FindMaxSub (
-4
) FindMaxSub ( 1 -4 )
1. Find the max in left sub arrays
2. Find the max in right sub arrays FindMaxSub (
3. Find the max in crossing sub arrays Scan
-4
once, and scan
2 once
4. Choose the largest one from those 3 as the final result
3. Find the max in crossing sub arrays
1 -4 3 2
-4
1 -4
Sum=-4 Sum=-3 largest
3 3
Sum=3 2 Sum=5 largest
The largest crossing subarray is :
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 = 1 2 3 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?
2
12 22
= 8
+ (2 )
Matrix multiplication divide-and-conquer algorithm
= 8
2
+ (2 )
What is the time complexity? From Master method we know it is (3 )
Matrix multiplication Strassens algorithm
= 8
2
+ (2 )
= 7
+ (2 )
Matrix multiplication Strassens algorithm
Strassens 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 )