Presentation is loading. Please wait.

Presentation is loading. Please wait.

Design and Analysis of Algorithms Maximum-subarray problem and matrix multiplication Haidong Xue Summer 2012, at GSU.

Similar presentations


Presentation on theme: "Design and Analysis of Algorithms Maximum-subarray problem and matrix multiplication Haidong Xue Summer 2012, at GSU."— Presentation transcript:

1 Design and Analysis of Algorithms Maximum-subarray problem and matrix multiplication
Haidong Xue Summer 2012, at GSU

2 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?

3 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 )

4 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

5 Maximum-subarray problem back ground
Day 1 2 3 4 Price 10 11 7 6 Difference -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 1-1 1-2 1-3 1-4 2-2 2-3 2-4 3-3 3-4 4-4 Sum 1 -3 -4 -1 -5 3

6 Maximum-subarray problem – divide-and-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

7 Maximum-subarray problem – divide-and-conquer algorithm
Input: array A[i, …, j] Ouput: sum of maximum-subarray, start point of maximum-subarray, end point of maximum-subarray FindMaxSubarray: if(j<=i) return (A[i], i, j); mid = floor(i+j); (sumCross, startCross, endCross) = FindMaxCrossingSubarray(A, i, j, mid); (sumLeft, startLeft, endLeft) = FindMaxSubarray(A, i, mid); (sumRight, startRight, endRight) = FindMaxSubarray(A, mid+1, j); Return the largest one from those 3

8 Maximum-subarray problem – divide-and-conquer algorithm
FindMaxCrossingSubarray(A, i, j, mid) Scan A[i, mid] once, find the largest A[left, mid] Scan A[mid+1, j] once, find the largest A[mid+1, right] Return (sum of A[left, mid] and A[mid+1, right], left, right) Let’s try it in Java

9 1 -4 3 2 Target array : 1 1 All the sub arrays: -4 -4 3 3 2 2 1 -4 -3 -4 3 -1 3 2 Max! 5 1 -4 3 -4 3 2 1 1 -4 3 2 2

10 1 -4 3 2 Divide target array into 2 arrays Target array : 1 We then have 3 types of subarrays: 1 All the sub arrays: -4 -4 The problem can be then solved by: The ones belong to the left array 3 3 1. Find the max in left sub arrays The ones belong to the right array 2 2 1 -4 2. Find the max in right sub arrays -3 The ones crossing the mid point -4 3 -1 3. Find the max in crossing sub arrays 3 2 5 1 -4 3 -4 3 2 4. Choose the largest one from those 3 as the final result 1 1 -4 3 2 2

11 1 -4 3 2 FindMaxSub ( ) 1. Find the max in left sub arrays 1 -4 FindMaxSub ( ) 3 2 2. Find the max in right sub arrays FindMaxSub ( ) 3. Find the max in crossing sub arrays 1 -4 3 2 Scan once, and scan once 4. Choose the largest one from those 3 as the final result

12 3. Find the max in crossing sub arrays
1 -4 3 2 -4 Sum=-4 1 -4 Sum=-3 largest 3 Sum=3 3 2 Sum=5 largest The largest crossing subarray is :

13 Maximum-subarray problem – divide-and-conquer algorithm
What is the time complexity? FindMaxSubarray: if(j<=i) return (A[i], i, j); mid = floor(i+j); (sumCross, startCross, endCross) = FindMaxCrossingSubarray(A, i, j, mid); (sumLeft, startLeft, endLeft) = FindMaxSubarray(A, i, mid); (sumRight, startRight, endRight) = FindMaxSubarray(A, mid+1, j); Return the largest one from those 3 𝑇 𝑛 =2𝑇 𝑛 2 +Θ(n)

14 Matrix multiplication
How to multiply two matrices? Given matrix 𝐴 𝑛𝑛 𝑎𝑛𝑑 𝐵 𝑛𝑛 , 𝐶 𝑛𝑛 =𝐴𝐵 𝑐 𝑖𝑗 = 𝑘=1 𝑛 𝑎 𝑖𝑘 𝑏 𝑘𝑗 For each 𝑐 𝑖𝑗 , we need Θ(𝑛) There are 𝑛 2 𝑐 𝑖𝑗 , so 𝑇 𝑛 = 𝑛 2 Θ(𝑛)=Θ( 𝑛 3 ) = −9 −3 − Time Complexity?

15 Matrix multiplication divide-and-conquer algorithm
C=AB 𝐶 11 𝐶 12 𝐶 21 𝐶 22 = 𝐴 11 𝐴 12 𝐴 21 𝐴 22 × 𝐵 11 𝐵 12 𝐵 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 )

16 Matrix multiplication divide-and-conquer algorithm
𝑇 𝑛 =8𝑇 𝑛 2 +Θ( 𝑛 2 ) What is the time complexity? From Master method we know it is Θ( 𝑛 3 )

17 Matrix multiplication Strassen’s algorithm
𝑇 𝑛 =8𝑇 𝑛 2 +Θ( 𝑛 2 ) 𝑇 𝑛 =7𝑇 𝑛 2 +Θ( 𝑛 2 )

18 Matrix multiplication 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 )= Θ( 𝑛 𝑙𝑜𝑔 2 7 )


Download ppt "Design and Analysis of Algorithms Maximum-subarray problem and matrix multiplication Haidong Xue Summer 2012, at GSU."

Similar presentations


Ads by Google