1 / 13

Design and Analysis of Algorithms: Maximum Subarray Problem and Matrix Multiplication

Learn about the divide-and-conquer algorithm for the maximum subarray problem and matrix multiplication with detailed examples. Explore the time complexity and Strassen's algorithm.

kharold
Download Presentation

Design and Analysis of Algorithms: Maximum Subarray Problem and Matrix Multiplication

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Design and Analysis of AlgorithmsMaximum-subarray problem and matrix multiplication (with more examples) HaidongXue Summer 2012, at GSU

  2. 1 -4 3 2 Target array : What is a maximum subarray? 1 1 All the sub arrays: -4 -4 3 3 The subarray with the largest sum 2 2 1 -4 -3 -4 3 -1 3 2 What is the brute-force T(n)? Max! 5 1 -4 3 0 -4 3 2 1 O() 1 -4 3 2 2

  3. Maximum-subarray • Can we do it in a divide-and-conquer manner? • Yes, we can

  4. Divide target array into 2 arrays 1 -4 3 2 Target array : We then have 3 types of subarrays: 1 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 -3 2. Find the max in right sub arrays The ones crossing the mid point -4 3 -1 3. Find the max in crossing sub arrays 3 2 5 1 -4 3 0 -4 3 2 1 4. Choose the largest one from those 3 as the final result 1 -4 3 2 2

  5. How to find the max subarray crossing the midpoint? 1 -4 3 2 Find the left part -4 Sum=-4 1 -4 Sum=-3 largest Find the right part 3 Sum=3 3 2 Sum=5 largest The largest crossing subarray is :

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

  7. Maximum-subarrayproblem – 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 (n) Time complexity?

  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. Matrix multiplication • How to multiply two matrices? • If each matrix is nxn, for each , we need • There are so = A B C 1 0 0 -1 0 -1 2 3 3 4 9 10

  10. Matrix multiplication • How to multiply two matrices? • If each matrix is nxn, for each , we need • There are so = A B C 1 0 0 -1 0 -1 2 3 3 4 9 10

  11. Matrix multiplicationdivide-and-conquer algorithm • C=AB • Recurrence equation? • () 1 0 1 5 0 0 1 5 2 3 2 3 2 3 2 3 1 6 1 0 1 0 0 0 7 3 2 3 0 3 2 2

  12. Matrix multiplicationdivide-and-conquer algorithm • () • What is the time complexity? • From Master method we know it is ()

  13. Matrix multiplicationStrassen’s algorithm • () • () • =O()O()

More Related