03 Divide and Conquer

download 03 Divide and Conquer

of 40

Transcript of 03 Divide and Conquer

  • 8/17/2019 03 Divide and Conquer

    1/40

     DIVIDE ANDCONQUER

    Design and Analysis Algorithm

    Mochammad Kautsar SophanGenap 2015

  • 8/17/2019 03 Divide and Conquer

    2/40

    Review Divide &Conquer  Divide the problem into a number of

    subproblems that are smaller instances of thesame problem

      !on"uer the subproblems by solving themrecursively#f the subproblem si$es are small enough% ho&ever% 'ust solve the subproblems in a straightfor&ard

    manner

      !ombine the solutions to the subproblems intothe solution for the original problem

    (autsar ) #nformati(a ) *+M 2

  • 8/17/2019 03 Divide and Conquer

    3/40

      ,hen the subproblems are large enough tosolve recursively% &e call that the recursivecase

      -nce the subproblems become small enoughthat &e no longer recurse% &e say that therecursion .bottoms out/ and that &e have

    gotten do&n to the base case

    (autsar ) #nformati(a ) *+M

  • 8/17/2019 03 Divide and Conquer

    4/40

    Objective

      see more algorithms based on divide)and)con"uer+he rst one solves the maimum)subarray

    problem it ta(es as input an array of numbers% andit determines the contiguous subarray &hosevalues have the greatest sum

    (autsar ) #nformati(a ) *+M 3

  • 8/17/2019 03 Divide and Conquer

    5/40

    Recurrences

      is an e"uation or ine"uality that describes afunction of recursive running time

      4or eample% &orst)case running time +n6 of

    the M78G7)S-8+ procedure by therecurrence &hose solution &e claimed to be+ n6 9 :n lg n6

    (autsar ) #nformati(a ) *+M 5

  • 8/17/2019 03 Divide and Conquer

    6/40

      8ecurrences can ta(e many forms  4or eample% a recursive algorithm might

    divide subproblems into une"ual si$es% such

    as a 2;)to)1; split  #f the divide and combine steps ta(e linear

    time% such an algorithm &ould give rise to the

    recurrence + n6 9 + 2n;6 < + n;6

  • 8/17/2019 03 Divide and Conquer

    7/40

      three methods for solving recurrences>that is% forobtaining asymptotic .:/or.-/ bounds on thesolution

    #n the substitution method% &e guess a bound and thenuse mathematical induction to prove our guess correct

    +he recursion)tree method converts the recurrence intoa tree &hose nodes represent the costs incurred at

    various levels of the recursion ,e use techni"ues forbounding summations to solve the recurrence

    +he master method provides bounds for recurrences ofthe form

    (autsar ) #nformati(a ) *+M ?

  • 8/17/2019 03 Divide and Conquer

    8/40

    Master et!od

       A recurrence of the form in master method characteri$es a divide)and)con"uer algorithmthat creates a subproblems% each of &hich is 1;b 

    the si$e of the original problem% and in &hich thedivide and combine steps together ta(e fn6 time

      +o use the master method% you &ill need tomemori$e three cases% but once you do that%you &ill easily be able to determine asymptoticbounds for many simple recurrences

    (autsar ) #nformati(a ) *+M @

  • 8/17/2019 03 Divide and Conquer

    9/40

      -ccasionally% &e shall see recurrences thatare not e"ualities but rather ine"ualities% suchas

      ecause such a recurrence states only anupper bound on +n6% &e &ill couch itssolution using -)notation rather than : 

    notation

    (autsar ) #nformati(a ) *+M B

  • 8/17/2019 03 Divide and Conquer

    10/40

      Similarly% if the ine"uality &ere reversed to

      then because the recurrence gives only a

    lo&er bound on +n6% &e &ould use C)notation in its solution

    (autsar ) #nformati(a ) *+M 10

  • 8/17/2019 03 Divide and Conquer

    11/40

     "!e a#iu$subarra% rob'e

      Suppose that you been offered the opportunity toinvest in the olatile !hemical !orporation

      the stoc( price of the olatile !hemical !orporation

    is rather volatile  Eou are allo&ed to buy one unit of stoc( only one

    time and then sell it at a later date% buying and sellingafter the close of trading for the day

      +o compensate for this restriction% you are allo&ed tolearn &hat the price of the stoc( &ill be in the future

      Eour goal is to maimi$e your prot

    (autsar ) #nformati(a ) *+M 11

  • 8/17/2019 03 Divide and Conquer

    12/40(autsar ) #nformati(a ) *+M 12

  • 8/17/2019 03 Divide and Conquer

    13/40

      Eou may buy the stoc( at any one time%starting after day 0% &hen the price is F100per share

      -f course% you &ould &ant to .buy lo&% sellhigh/>buy at the lo&est possible price andlater on sell at the highest possible price>to

    maimi$e your prot

    (autsar ) #nformati(a ) *+M 1

  • 8/17/2019 03 Divide and Conquer

    14/40

      &e &ould maimi$e prot by buying at thelo&est price% after day ?

      #f this strategy al&ays &or(ed% then it &ould

    be easy to determine ho& to maimi$e protnd the highest and lo&est prices% and then &or(

    left from the highest price to nd the lo&est priorprice%

    &or( right from the lo&est price to nd the highestlater price% and ta(e the pair &ith the greaterdifference

    (autsar ) #nformati(a ) *+M 13

  • 8/17/2019 03 Divide and Conquer

    15/40(autsar ) #nformati(a ) *+M 15

  • 8/17/2019 03 Divide and Conquer

    16/40

    aroac!

       A brute)force solution   A transformation

      divide)and)con"uer 

    (autsar ) #nformati(a ) *+M 1=

  • 8/17/2019 03 Divide and Conquer

    17/40

    A brute$(orce so'ution

       'ust try every possible pair of buy and selldates in &hich the buy date precedes the selldate

       A period of n days has n;2 such pairs ofdates

      Since n;2 is :n26 and the best &e can hope

    for is to evaluate each pair of dates inconstant time% this approach &ould ta(e Cn26time

    (autsar ) #nformati(a ) *+M 1?

  • 8/17/2019 03 Divide and Conquer

    18/40

    A trans(oration

      &e &ill loo( at the input in a slightly different&ay

      ,e &ant to nd a se"uence of days over &hich

    the net change from the rst day to the last ismaximum

      #nstead of loo(ing at the daily prices% let us

    instead consider the daily change in price%&here the change on day i is the differencebet&een the prices after day i ) 1 and after day i

    (autsar ) #nformati(a ) *+M 1@

  • 8/17/2019 03 Divide and Conquer

    19/40

      +he table sho&s daily changes in the bottomro&

      #f &e treat this ro& as an array A% &e no&

    &ant to nd the nonempty% contiguoussubarray of A &hose values have the largestsum

     

    ,e call this contiguous subarray themaximum subarray

    (autsar ) #nformati(a ) *+M 1B

  • 8/17/2019 03 Divide and Conquer

    20/40

      4or eample% in the array the maimum  subarray of AH1 1=I is AH@ 11I% &ith the

    sum 3+hus% you &ould &ant to buy the stoc( 'ust before

    day @ that is% after day ?6 and sell it after day 11%earning a prot of F3 per share

    (autsar ) #nformati(a ) *+M 20

  • 8/17/2019 03 Divide and Conquer

    21/40

    A so'ution usin) divide$and$conquer

      Suppose &e &ant to nd a maimumsubarray of the subarray AHlo& highI

      Divide)and)con"uer suggests that &e divide

    the subarray into t&o subarrays of as e"ualsi$e as possible

      +hat is% &e nd the midpoint% say mid% of the

    subarray% and consider the subarrays AHlo& midI and AHmid < 1 highI

    (autsar ) #nformati(a ) *+M 21

  • 8/17/2019 03 Divide and Conquer

    22/40

      any contiguous subarray AHi ' I of AHlo& highI must lie in eactly one of the follo&ingplaces

    entirely in the subarray AHlo& midI% so thatlo& J i J ' J mid%

    entirely in the subarray AHmid < 1 highI% so that mid i J ' J high

    crossing the midpoint% so thatlo& J i J mid ' J high

      a maimum subarray of AHlo& LighI must lie ineactly one of these places

    (autsar ) #nformati(a ) *+M 22

  • 8/17/2019 03 Divide and Conquer

    23/40

    (autsar ) #nformati(a ) *+M 2

  • 8/17/2019 03 Divide and Conquer

    24/40

    (autsar ) #nformati(a ) *+M 23

  • 8/17/2019 03 Divide and Conquer

    25/40

    (autsar ) #nformati(a ) *+M 25

  • 8/17/2019 03 Divide and Conquer

    26/40

    Ana'%*in) t!e divide$and$conquer a')orit!

      &e ma(e the simplifying assumption that theoriginal problem si$e is a po&er of 2% so thatall subproblem si$es are integers

      ,e denote by +n6 the running time of 4#D)MAN#M*M)S*A88AE on a subarray of n elements

     

    4or starters% line 1 ta(es constant time  +he base case% &hen n 9 1% is easyO line 2

    ta(es constant time% and so +169:16

    (autsar ) #nformati(a ) *+M 2=

  • 8/17/2019 03 Divide and Conquer

    27/40

      +he recursive case occurs &hen nP1

      Qines 1 and ta(e constant time

      7ach of the subproblems solved in lines 3 and 5 is on a

    subarray of n;2 elements our assumption that theoriginal problem si$e is a po&er of 2 ensures that n;2 isan integer6%

      and so &e spend +n;26 time solving each of them

     

    ecause &e have to solve t&o subproblems>for the leftsubarray and for the right subarray>the contribution tothe running time from lines 3 and 5 comes to 2+n;26

    (autsar ) #nformati(a ) *+M 2?

  • 8/17/2019 03 Divide and Conquer

    28/40

      the call to 4#D)MAN)!8-SS#G)S*A88AE in line = ta(es :n6 time

      Qines ?R11 ta(e only :16 time  4or the recursive case% therefore% &e have

    (autsar ) #nformati(a ) *+M 2@

  • 8/17/2019 03 Divide and Conquer

    29/40

      !ombining e"uations 356 and 3=6 gives usa recurrence for the running time +n6 of4#D)MAN#M*M)S*A88AEO

    (autsar ) #nformati(a ) *+M 2B

  • 8/17/2019 03 Divide and Conquer

    30/40

      +hus% &e see that the divide)and)con"uermethod yields an algorithm that isasymptotically faster than the brute)force

    method  ,ith merge sort and no& the maimum)

    subarray problem% &e begin to get an idea of

    ho& po&erful the divide)and)con"uer methodcan be

    (autsar ) #nformati(a ) *+M 0

  • 8/17/2019 03 Divide and Conquer

    31/40

    Assignment (1)

    1 #mplement 4ind)Maimum)Subarray

    2 ,hat does 4#D)MAN#M*M)S*A88AEreturn &hen all elements of A are negative

    ,rite pseudocode for the brute)force methodof solving the maimum)subarray problemState your looping count

    (autsar ) #nformati(a ) *+M 1

  • 8/17/2019 03 Divide and Conquer

    32/40

    atri# u'ti'ication

      #f A 9 ai'6 and 9 bi'6 are s"uare n n 

    matrices% then in the product! 9 A % &e dene the entry ci' %

    for i %' 9 1%2Tn% by

    (autsar ) #nformati(a ) *+M 2

  • 8/17/2019 03 Divide and Conquer

    33/40

    (autsar ) #nformati(a ) *+M

  • 8/17/2019 03 Divide and Conquer

    34/40

      ecause each of the triply)nested for loopsruns eactly n iterations% and each eecutionof line ? ta(es constant time% the SU*A87)

    MA+8#N)M*Q+#VQE procedure ta(es :n6 time

    (autsar ) #nformati(a ) *+M 3

  • 8/17/2019 03 Divide and Conquer

    35/40

    Assignment (2)

    1 #mplement Matri multiplication

    2 State your looping count for n n matri

    (autsar ) #nformati(a ) *+M 5

  • 8/17/2019 03 Divide and Conquer

    36/40

    The master method forsolving recurrences

      +he master method provides a .coo(boo(/method for solving recurrences of the form

      &here a W 1 and b P1 are constants and fn6 isan asymptotically positive function

    (autsar ) #nformati(a ) *+M =

  • 8/17/2019 03 Divide and Conquer

    37/40

      +he recurrence describes the running time ofan algorithm that divides a problem of si$e n into a subproblems% each of si$e n;b%&here a 

    and b are positive constants  +he a subproblems are solved recursively%

    each in time + n;b6

     

    +he function fn6 encompasses the cost ofdividing the problem and combining theresults of the subproblems

    (autsar ) #nformati(a ) *+M ?

  • 8/17/2019 03 Divide and Conquer

    38/40

    (autsar ) #nformati(a ) *+M @

  • 8/17/2019 03 Divide and Conquer

    39/40

    (autsar ) #nformati(a ) *+M B

  • 8/17/2019 03 Divide and Conquer

    40/40