0% found this document useful (0 votes)
3 views32 pages

5-Max Subarray and Strassen's Matrix Multiplication

The document discusses algorithms for matrix multiplication and the maximum subarray problem using divide-and-conquer techniques. It details Strassen's method for efficient matrix multiplication, achieving a time complexity of Θ(n^2.81), and presents a recursive approach to find the maximum contiguous subarray sum with a time complexity of Θ(n log n). The document also includes pseudocode for the algorithms and analysis of their performance.

Uploaded by

farjadkhalil917
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views32 pages

5-Max Subarray and Strassen's Matrix Multiplication

The document discusses algorithms for matrix multiplication and the maximum subarray problem using divide-and-conquer techniques. It details Strassen's method for efficient matrix multiplication, achieving a time complexity of Θ(n^2.81), and presents a recursive approach to find the maximum contiguous subarray sum with a time complexity of Θ(n log n). The document also includes pseudocode for the algorithms and analysis of their performance.

Uploaded by

farjadkhalil917
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 32

DIVIDE-AND-

Strassen’s Method CONQUER


Maximum Subarray
Problem
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

An O(n3) time naive algorithm

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:

8 mults of (n/2)×(n/2) submatrices


4 adds of (n/2)×(n/2) submatrices
6
DIVIDE-AND-CONQUER ALGORITHM
C11  A11 B11  A12 B21

C12  A11 B12  A12 B22


C21  A21B11  A22 B21
C22  A21B12  A22 B22
A straightforward divide-and-conquer algorithm

T(n) = 8T(n/2) + (n2)


= (n3)
Computing A+B  O(n2)

7
ANALYSIS OF D&C
ALGORITHM
T(n) = 8 T(n/2) + Θ(n2)

# subproblems work dividing


subproblem size and combining

a=8, b=2, nlogb a= n3 ⇒ CASE 1 ⇒ T(n) = Θ(n3) .


No better than the ordinary algorithm.
8
• If we were to create 12 new n/2 * n/2 matrices, we would spend‚ (n2) time copying
entries. In fact, we can partition the matrices without copying entries.
• In lines 6–9, we recursively call SQUARE-MATRIX-MULTIPLY-RECURSIVE a total of
eight times.
• Because each recursive call multiplies two n/2 * n/2 matrices, thereby contributing T(n/2)
to the overall running time, the time taken by all eight recursive calls is 8T(n/2) 9
STRASSEN’S DIVIDE-AND-CONQUER
ALGORITHM
Step 1: Divide each of A, B, and C into four sub-matrices
Step 2: Create 10 matrices S1, S2, …, S10
Steep 3: Recursively, compute P1, P2, …, P7
C11 , C12 , C21 , C22
Step 4: Compute

10
STRASSEN’S METHOD

Recursively multiply n/2 * n/2 matrices seven times to compute the


following n/2 * n/2 matrices, each of which is the sum or difference of 11
products of A and B submatrices
STRASSEN’S ALGORITHM
1. Divide: Partition A and B into
(n/2)×(n/2) submatrices. Form terms
to be multiplied using + and – .
2. Conquer: Perform 7 multiplications of
(n/2)×(n/2) submatrices recursively.
3. Combine: Form C using + and – on
(n/2)×(n/2) submatrices.
T(n) = 7 T(n/2) + Θ(n2)
12
ANALYSIS OF STRASSEN
T(n) = 7 T(n/2) + Θ(n2)
a=7, b=2, nlogb a= nlg7 ≈ n2.81 ⇒ CASE 1 ⇒ T(n) = Θ(n lg 7).

The number 2.81 may not seem much smaller than


3, but because the difference is in the exponent, the
impact on running time is significant. In fact,
Strassen’s algorithm beats the ordinary algorithm
on today’s machines for n ≥ 30 or so.

Best to date (of theoretical interest only): Θ(n2.376…).


13
MAXIMUM SUBARRAY SUM

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

low mid high

mid +1

entirely in A[low..mid] entirely in A[mid+1..high]

Possible locations of subarrays of 16


A[mid+1..j]

low
i mid high

mid +1 j
A[i..mid]

A[i..j] comprises two subarrays A[i..mid] and


A[mid+1..j]
MAXIMUM CONTIGUOUS
SUBARRAY SUM

18
MAXIMUM CONTIGUOUS
SUBARRAY SUM
1. Divide instance into subparts.
2. Solve the parts recursively.
3. Conquer by combining the answers

1. Split the array in half


2. Solve the parts recursively.
3. Just take the max?

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)

Initial call: FIND-MAXIMUM-SUBARRAY (A, 1, n)


FIND-MAX-CROSSING-SUBARRAY (A, LOW, MID, HIGH)
left-sum = - // Find a maximum
sum =0
subarray of the form A[i..mid]
for j = mid +1 to high
sum = 0
sum = sum + A[j]
for i = mid downto low
if sum > right-sum
sum = sum + A[i ]
right-sum = sum
if sum > left-sum max-right = j
left-sum = sum // Return the indices and the sum of the two
max-left = i subarrays
right-sum = - // Find a maximum Return (max-left, max-right, left-sum + right-
subarray of the form A[mid + 1 .. j ] sum)
Example:
mid =5
1 2 3 4 5 6 7 8 9 10
A 13 -3 -25 20 -3 -16 -23 18 20 -7

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy