14 Dynamic 1

download 14 Dynamic 1

of 43

Transcript of 14 Dynamic 1

  • 7/31/2019 14 Dynamic 1

    1/43

    CS161:DesignandAnalysisof

    Algorithms

  • 7/31/2019 14 Dynamic 1

    2/43

    DynamicProgrammingI:

    WeightedIntervalScheduling Example:FibonacciNumbers RecurrenceTrees DynamicProgrammingDags WeightedIntervalScheduling

  • 7/31/2019 14 Dynamic 1

    3/43

    Example:FibonacciNumbers

    F(n)={0ifn=01ifn=1F(n-1)+F(n-2)ifn>1}

  • 7/31/2019 14 Dynamic 1

    4/43

    RecursiveAlgorithm

    Fib1(n)={Ifn

  • 7/31/2019 14 Dynamic 1

    5/43

    RecursiveAlgorithm

    RunningTime?Claim:Forn>0,numberofaddionsisF(n)-1Trueforn=1,2

    Inducvelyassumetruefork

  • 7/31/2019 14 Dynamic 1

    6/43

    RecursiveAlgorithm

    RunningTime?(F(n))HowfastdoesF(n)grow?

  • 7/31/2019 14 Dynamic 1

    7/43

    FibonacciGrowthRate

    Letandbesoluonstox2=x+11.62,thegoldenrao-0.62

    Claim:F(n)=(n) StrongerClaim:F(n)=(nn)/()Proof:Trueforn=0,1Assumetruefork

  • 7/31/2019 14 Dynamic 1

    8/43

    FibonacciGrowthRate

    F(n)=(nn)/() Proof:Trueforn=0,1

    Assumetruefork

  • 7/31/2019 14 Dynamic 1

    9/43

    RecursiveAlgorithm

    RunningTime?(F(n))(1.62n)Growsextremelyrapidly

    Example:Mycomputer Runningme1.7x(1.62)nnanoseconds n=30:4milliseconds n=40:0.42seconds n=50:51seconds n=60:1.8hours(projecon) n=70:9.1days(projecon) n=130:93billionyears(projecon)

  • 7/31/2019 14 Dynamic 1

    10/43

    Problem

    TocomputeF(n):ComputeF(n-1)andF(n-2)CompungF(n-1)requirescompungF(n-2)andF(n-3)

    CompungF(n-2)requirescompungF(n-3)andF(n-4)

  • 7/31/2019 14 Dynamic 1

    11/43

    Problem

    TocomputeF(n):CallF(n-k)atotalofF(k-1)mesWaytoomuchrepeatedwork

  • 7/31/2019 14 Dynamic 1

    12/43

    Soluon:Memoizaon

    RememberanswerstoF(k)forfuturecalls Keeptrackof(k,F(k))mappings

    HashtableArray

    Fib2(n)={If(n

  • 7/31/2019 14 Dynamic 1

    13/43

    AlternaveApproach

    Wearegoingfromtopdown Howaboutgoingboomup:

    KeeparrayA,whereA[k]=F(k)Iteravelybuildarray

    First,setA[0]=0,A[1]=1 Then,fork=2,,n,setA[k]=A[k-1]+A[k-2]

  • 7/31/2019 14 Dynamic 1

    14/43

    IteraveAlgorithm

    Fib3(n)={ConstructarrayAoflengthn+1SetA[0]=0,A[1]=1

    Fork=2,,n A[k]=A[k-1]+A[k-2]

    ReturnA[n]

  • 7/31/2019 14 Dynamic 1

    15/43

    IteraveAlgorithm

    RunningTime:n-1iteraonsEachstephasa1addion(pretendalladdionsareconstantme)

    O(n)meMuchmoretractablenow

  • 7/31/2019 14 Dynamic 1

    16/43

    RecursionTree

    F(n)

    F(n-1) F(n-2)

    F(n-3) F(n-3) F(n-4)F(n-2)

    F(n-3) F(n-4) F(n-4) F(n-5) F(n-4) F(n-5) F(n-5) F(n-6)

  • 7/31/2019 14 Dynamic 1

    17/43

    MergeIdencalNodes

    F(n)

    F(n-1)

    F(n-2)

    F(n-3)

    F(n-4)

  • 7/31/2019 14 Dynamic 1

    18/43

    ProcessBoomUp

    F(n)

    F(n-1)

    F(n-2)

    F(n-3)

    F(n-4)

  • 7/31/2019 14 Dynamic 1

    19/43

    DynamicProgramming

    SubproblemshavedagstructureEdgerepresentsprerequisite

    SolvesubproblemsintopologicalorderWheneverwesolveasubproblem,wehavealreadysolvedalloftheothersubproblemsweneed

    Verygeneral,flexibletool

  • 7/31/2019 14 Dynamic 1

    20/43

    WeightedIntervalScheduling

    Givensetofnintervals(s(i),f(i)),eachwithaweightw(i)

    Goal:picksetofoverlappingintervalswithlargestpossibleweight Ifw(i)=1,wehavetheunweightedschedulingproblem,whichcanbesolvedbygreedy

    Doesgreedyworkhere?

  • 7/31/2019 14 Dynamic 1

    21/43

    WeightedIntervalScheduling

    Recallgreedy:pickintervalthatendsearliest

    Thisgreedyapproachdoesnotwork2

    1

  • 7/31/2019 14 Dynamic 1

    22/43

    WeightedIntervalScheduling

    SaywehaveopmalscheduleS Twopossibilies:

    Intervaln(thelastone)isinSIntervalnisnotinS

  • 7/31/2019 14 Dynamic 1

    23/43

    WeightedIntervalScheduling

    SupposeintervalnisnotinSThenSisactuallyopmalforfirstn-1intervalsOtherwise,anyopmalsoluonforfirstn-1intervalsissoluonfornintervalswithhigher

    weight

  • 7/31/2019 14 Dynamic 1

    24/43

    WeightedIntervalScheduling

    SupposeintervalnisinSThennointervalthatoverlapsncanbeinSS{n}mustbeopmaloverintervalsthatdontoverlapintervaln

  • 7/31/2019 14 Dynamic 1

    25/43

    WeightedIntervalScheduling

    Suggestthefollowingapproach:SubproblemswillconsistofsubsetsofintervalsIfsubsethassingleinterval,theopmalsoluonforthatsubproblemisjustthatinterval

    Otherwise,letTbesomesubset,andlettbethelastinterval

    TheopmalforasubsetTiseither: TheopmalforT{t} (TheopmalforT{sintersecngt})+{t}

  • 7/31/2019 14 Dynamic 1

    26/43

    Problem

    Thereare2nsubsets,sosolvingforallsubsetswilltakeexponenalme

    Instead,wewillbeclever:Orderintervalsbyfinishme(i.e.ifi

  • 7/31/2019 14 Dynamic 1

    27/43

    WeightedIntervalScheduling

    Opmalon{1,2,...,k}:Ifintervalkisnotinopmal,thenopmalisjustopmalon{1,2,,k-1}

    Ifkisinopmal,thenopmalistheopmalon{1,2,,p(k)},plustheintervalk

    Onlyneedtochecktwocases

  • 7/31/2019 14 Dynamic 1

    28/43

    WeightedIntervalScheduling

    WeightedIntervalSchedule:CreatesoluonarraySoflengthn+1CreateweightarrayWoflengthn+1Sortintervalsbyf(i)S[0]={},W[0]=0,S[1]={1},W[1]=w(1)Fork=2,,n:

    IfW[k-1]

  • 7/31/2019 14 Dynamic 1

    29/43

    RunningTime

    Saywehavep(i)values,andlistalreadysorted.

    Then,eachiteraontakesonlyO(1),soO(n)overall

    Compungp(j)?Obviousalgorithm:O(n2)

  • 7/31/2019 14 Dynamic 1

    30/43

    UnderlyingDag

    Nodesrepresentsets{1,,k} Pointerfromset{1,,k-1}to{1,,k}forallk Pointerformset{1,,p(k)}to{1,,k}forallk

  • 7/31/2019 14 Dynamic 1

    31/43

    DynamicProgrammingOutline

    Findgoodsubproblems ExpresssoluontosuproblemkintermsofsoluonstoothersubproblemsSoluontosubproblemkneedstobeefficientlycomputablegivensoluonstoothersubproblems

    Solvesubproblemsintopologicalorder

  • 7/31/2019 14 Dynamic 1

    32/43

    MoreonFibonacci

    Dynamicsoluonisntnecessarilybest OncewevecomputedF(k-1)andF(k-2),wenolongerneedF(k-3),F(k-4),,F(0)

    Savespace:onlykeeparoundlasttwocomputedvalues

  • 7/31/2019 14 Dynamic 1

    33/43

    MoreonFibonacci

    KeeparoundF(k)andF(k-1) Toupdate:

    F(k) = F(k1)+F(k 2)

    F(k1) = F(k1)

  • 7/31/2019 14 Dynamic 1

    34/43

    MoreonFibonacci

    KeeparoundF(k)andF(k-1) Toupdate:

    F(k)

    F(k1)

    "

    #

    $$

    %

    &

    ''=

    1 1

    1 0

    "

    #$

    %

    &'

    F(k1)

    F(k 2)

    "

    #

    $$

    %

    &

    ''

  • 7/31/2019 14 Dynamic 1

    35/43

    MoreonFibonacci

    CanwedobeerthanO(n)addions?F

    (n

    )F(n1)

    "

    #$$

    %

    &'' = 1 11 0

    "

    #$

    %

    &'

    n1F

    (1)F(0)

    "

    #$$

    %

    &''= 1 11 0

    "

    #$

    %

    &'

    n1

    10

    "

    #$

    %

    &'

  • 7/31/2019 14 Dynamic 1

    36/43

    HowtoComputePowers

    SaywehaveasetXwherewecanmulplyelementstogether

    Howdowecomputex

    n

    =x*x*x**x?Obvioussoluon:computexn-1recursively,andmulplybyx

    Requiresn-1mulplicaonsintheset

  • 7/31/2019 14 Dynamic 1

    37/43

    HowtoComputePowers

    Whatifwearecompungx4?Firstcomputey=x2,thencomputey2Only2mulplicaons

    Whataboutx8?Computey=x4asabove,thencomputey2Only3mulplicaons

  • 7/31/2019 14 Dynamic 1

    38/43

    HowtoComputePowers

    Ingeneral,cancomputex2nusingnmulplicaons

    Whataboutexponentsthatarenotpowersof2?

  • 7/31/2019 14 Dynamic 1

    39/43

    HowtoComputePowers

    Pow(x,n)={Ifn=1,returnxIfniseven,returnPow(x*x,n/2)Ifnisodd,returnx*Pow(x*x,(n-1)/2)}

  • 7/31/2019 14 Dynamic 1

    40/43

    HowtoComputePowers

    Numberofmulplicaons?Atmost2percalltoPowExponentisatleastdividedby2O(logn)mulplicaons

  • 7/31/2019 14 Dynamic 1

    41/43

    MoreonFibonacci

    CanwedobeerthanO(n)addions?

    CancomputeusingO(logn)2x2matrixmulplicaons O(logn)addionsandmulplicaons

    F(n

    )F(n1)

    "

    #$$

    %

    &'' = 1 11 0

    "

    #$

    %

    &'

    n1F

    (1)F(0)

    "

    #$$

    %

    &''= 1 11 0

    "

    #$

    %

    &'

    n1

    10

    "

    #$

    %

    &'

  • 7/31/2019 14 Dynamic 1

    42/43

    TheCatch

    Theintegersweareaddingandmulplyingarelarge(exponenal,infact)

    Numberofdigits:O(n)

    EventhoughO(logn)addionsandmulplicaons,eachaddionand

    mulplicaontakesmeuptoO(M(n)),

    whereM(n)isthemetomulply2n-digitintegers

  • 7/31/2019 14 Dynamic 1

    43/43

    ActualRunningTime

    T(n)=T(n/2)+O(M(n)) M(n)isatleastn,sorunningmedominatedbyO(M(n))term

    Therefore,T(n)=O(M(n))