maximum-subarray problem, matrix multiplication and strassen's algorithm

Upload: paksmiler

Post on 07-Aug-2018

245 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/21/2019 Maximum-Subarray Problem, Matrix Multiplication and Strassen's Algorithm

    1/18

    Design and Analysis of AlgorithmsMaximum-subarray problem and matrix

    multiplication

    Haidong Xue

    Summer 2012, at GSU

  • 8/21/2019 Maximum-Subarray Problem, Matrix Multiplication and Strassen's Algorithm

    2/18

    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?

    60

    70

    80

    90

    100

    110

    120

    0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

    Price

    day #

  • 8/21/2019 Maximum-Subarray Problem, Matrix Multiplication and Strassen's Algorithm

    3/18

    Maximum-subarray problem

    back ground

    What is the brute-forcesolution?

    max = -infinity;

    for each day pair p {if(p.priceDifference>max)

    max=p.priceDifference;

    }

    Time complexity?

    pairs, so O()

  • 8/21/2019 Maximum-Subarray Problem, Matrix Multiplication and Strassen's Algorithm

    4/18

    Maximum-subarray problem

    back ground

    If we know the price difference of each 2contiguous 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

  • 8/21/2019 Maximum-Subarray Problem, Matrix Multiplication and Strassen's Algorithm

    5/18

    Maximum-subarray problem

    back groundDay 0 1 2 3 4

    Price 10 11 7 10 6

    Difference 1 -4 3 -4

    What is the solution? Buy on day 2, sell on day 3

    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 0 -4 -4 -1 -5 3 -1 -4

    Can be solve it by the maximum-subarray of difference array?

  • 8/21/2019 Maximum-Subarray Problem, Matrix Multiplication and Strassen's Algorithm

    6/18

    Maximum-subarray problemdivide-

    and-conquer algorithm

    How to divide?

    Divide to 2 arrays

    What is the base case?

    How to combine the sub problem solutions to thecurrent 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 amongmaximum subarrays of those 3

  • 8/21/2019 Maximum-Subarray Problem, Matrix Multiplication and Strassen's Algorithm

    7/18

    Maximum-subarray problemdivide-

    and-conquer algorithm

    Input: array A[i, , j]

    Ouput: sum of maximum-subarray, start point of maximum-subarray, end point of maximum-subarray

    FindMaxSubarray:

    1. if(j

  • 8/21/2019 Maximum-Subarray Problem, Matrix Multiplication and Strassen's Algorithm

    8/18

    Maximum-subarray problemdivide-

    and-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

  • 8/21/2019 Maximum-Subarray Problem, Matrix Multiplication and Strassen's Algorithm

    9/18

    1 -4 3 2Target array :

    All the sub arrays:

    1

    -4

    3

    2

    1 -4

    -4 3

    3 2

    1 -4 3

    -4 3 2

    1 -4 3 2

    1

    -4

    3

    2

    -3

    -1

    5

    0

    1

    2

    Max!

  • 8/21/2019 Maximum-Subarray Problem, Matrix Multiplication and Strassen's Algorithm

    10/18

    1 -4 3 2Target array :

    All the sub arrays:

    1

    -4

    3

    2

    1 -4

    -4 3

    3 2

    1 -4 3

    -4 3 2

    1 -4 3 2

    1

    -4

    3

    2

    -3

    -1

    5

    0

    1

    2

    Divide target array into 2

    arrays

    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

    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

    4. Choose the largest

    one from those 3 as the

    final result

  • 8/21/2019 Maximum-Subarray Problem, Matrix Multiplication and Strassen's Algorithm

    11/18

    1 -4 3 2FindMaxSub( )

    1. Find the max in left sub arrays

    2. Find the max in right sub arrays

    3. Find the max in crossing sub arrays

    4. Choose the largest one from those 3 as the finalresult

    1 -4FindMaxSub( )

    3 2FindMaxSub( )

    Scan once, and scan once1 -4 3 2

  • 8/21/2019 Maximum-Subarray Problem, Matrix Multiplication and Strassen's Algorithm

    12/18

    3. Find the max in crossing sub arrays

    1 -4 3 2

    -4 Sum=-4

    1 -4 Sum=-3 largest

    3 Sum=3

    3 Sum=52 largest

    The largest crossing subarray is :

  • 8/21/2019 Maximum-Subarray Problem, Matrix Multiplication and Strassen's Algorithm

    13/18

    Maximum-subarray problemdivide-

    and-conquer algorithm

    What is the time complexity?

    FindMaxSubarray:

    1. if(j

  • 8/21/2019 Maximum-Subarray Problem, Matrix Multiplication and Strassen's Algorithm

    14/18

    Matrix multiplication

    How to multiply two matrices?

    Given matrix ,

    =

    For each , we need ()

    There are , so ()=()

    =

    9 3

    1 2

    2 1

    Time Complexity?

  • 8/21/2019 Maximum-Subarray Problem, Matrix Multiplication and Strassen's Algorithm

    15/18

    Matrix multiplication

    divide-and-conquer algorithm

    C=AB

    + +

    +

    + Recurrence equation?

    8

    + ()

  • 8/21/2019 Maximum-Subarray Problem, Matrix Multiplication and Strassen's Algorithm

    16/18

    Matrix multiplication

    divide-and-conquer algorithm

    8

    + ()

    What is the time complexity?

    From Master method we know it is ()

  • 8/21/2019 Maximum-Subarray Problem, Matrix Multiplication and Strassen's Algorithm

    17/18

    Matrix multiplication

    Strassensalgorithm

    8

    + ()

    7

    + (

    )

  • 8/21/2019 Maximum-Subarray Problem, Matrix Multiplication and Strassen's Algorithm

    18/18

    Matrix multiplication

    Strassensalgorithm

    Strassens algorithm

    1. Perform 10 times matrix addition or

    subtraction to make 0from

    2. Perform 7 times matrix multiplicationto

    make to from,

    3. Perform matrix addition or matrix

    subtraction to obtain , , and

    7

    + ()= ()