dynprog knapsack

Upload: andry

Post on 07-Aug-2018

239 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/20/2019 DynProg Knapsack

    1/40

    Dynamic Programming …Dynamic Programming …

    ContinuedContinued0-1 Knapsack Problem

  • 8/20/2019 DynProg Knapsack

    2/40

    Last ClassLast Class

    Longest Common Subsequence (LCS◦ !ssigned problem " #ind t$e LCS bet%een

    &')!*+ and &C',)P!*..+ #ill out t$e dynamic programming table  /race back %$at t$e LCS is

    .dit Distance◦ !ssigned problem " #ind t$e edit distance

    required to transorm &K,L/+ into &K,//.*+ #ill out t$e dynamic programming table

     /race back t$e edit pat$

    ◦ !ssigned problem " utline t$e recursi2e calls%$en using t$e recursi2e met$od or 3ndingt$e edit distance bet%een &*+ and &##+4

  • 8/20/2019 DynProg Knapsack

    3/40

    Last ClassLast ClassCan I have volunteers to do the following on

    the board?1 #ill out t$e dynamic programming table or t$e LCS

    bet%een &')!*+ and &C',)P!*..+

    5  /race back %$at t$e LCS string is bet%een &')!*+

    and &C',)P!*..+

    6 #ill out t$e dynamic programming table to 3nd t$e editdistance required to transorm &K,L/+ into &K,//.*+

    7  /race back t$e edit pat$ t$at transormed &K,L/+ into&K,//.*+

    8 utline t$e recursi2e calls %$en using t$e recursi2emet$od or 3nding t$e edit distance bet%een &*+ and&##+4

  • 8/20/2019 DynProg Knapsack

    4/40

    !nnouncements!nnouncements

    !ssignment 97 D*! DistanceProblem

  • 8/20/2019 DynProg Knapsack

    5/40

    Knapsack 0-1 ProblemKnapsack 0-1 Problem

     /$e goal is tomaximize the valueof a knapsack t$atcan $old at most :units (i4e4 lbs or kg

    %ort$ o goods rom alist o items I0, I1, … In-1.

    ◦ .ac$ item $as 5attributes;

    1) Value – let this be vi foritem Ii

    2) Weight – let this be wi foritem Ii

  • 8/20/2019 DynProg Knapsack

    6/40

    Knapsack 0-1 ProblemKnapsack 0-1 Problem

     /$e di

  • 8/20/2019 DynProg Knapsack

    7/40

    Knapsack 0-1 ProblemKnapsack 0-1 Problem

    >rute #orce◦ /$e na?2e %ay to sol2e t$is problem is

    to cycle t$roug$ all 5n subsets o t$e n

    items and pick t$e subset %it$ a legal%eig$t t$at ma@imiAes t$e 2alue o t$eknapsack4

    ◦  :e can come up %it$ a dynamicprogramming algorit$m t$at %illS!LL= do better t$an t$is brute orcetec$nique4

  • 8/20/2019 DynProg Knapsack

    8/40

    Knapsack 0-1 ProblemKnapsack 0-1 Problem

    !s %e did beore %e are going to sol2e t$eproblem in terms o sub-problems4◦ So letBs try to do t$at…

    ur 3rst attempt mig$t be to c$aracteriAea sub-problem as ollo%s;◦ Let !  be the o"timal subset of elements from #I0, I1, …, I! $.

    :$at %e 3nd is t$at t$e optimal subset rom t$e

    elements #I0, I1, …, I!%1$ may not correspond to t$eoptimal subset o elements rom #I0, I1, …, I! $ in anyregular pattern4

    ◦ >asically t$e solution to t$e optimiAation

    problem or !%1 mig$t */ contain t$e optimalsolution rom roblem 4

  • 8/20/2019 DynProg Knapsack

    9/40

    Knapsack 0-1 ProblemKnapsack 0-1 ProblemLetBs illustrate t$at point %it$ an e@ample;

    Item Weight Value

    I0  3 0

    I  ! "

    I#  $ $

    I3  !

    %he maximum weight the knapsack can hold is #0&

     /$e best set o items rom ,0 ,1 ,5E is ,0 ,1 ,5E

    >/ t$e best set o items rom ,0 ,1 ,5 ,6E is ,0

    ,5 ,6E4◦ ,n t$is e@ample note t$at t$is optimal solution ,0 ,5 ,6E

    does */ build upon t$e pre2ious optimal solution ,0 ,1,5E4

    (,nstead it buildFs upon t$e solution ,0 ,5E %$ic$ is really t$e

    optimal subset o ,0 ,1 ,5E %it$ %eig$t 15 or less4

  • 8/20/2019 DynProg Knapsack

    10/40

    Knapsack 0-1 problemKnapsack 0-1 problemSo no% %e must re-%ork t$e %ay %e build upon

    pre2ious sub-problems…

    ◦ Let '(k) w* represent t$e ma@imum total 2alue o a subset Sk 

    %it$ %eig$t %4

    ◦ ur goal is to 3nd '(n) W*) %$ere n is t$e total number oitems and : is t$e ma@imal %eig$t t$e knapsack can carry4

    So our recursi2e ormula or subproblems;

    B[k, w] = B[k - 1,w], if wk  > w

      = max { B[k - 1,w], B[k - 1,w - wk ] + vk }, otherwise

    ,n .nglis$ t$is means t$at t$e best subset o Sk t$at

    $as total %eig$t % is;

    1 /$e best subset o Sk-1 t$at $as total %eig$t % or

    5 /$e best subset o Sk-1 t$at $as total %eig$t %-%k plus t$e item

    k

  • 8/20/2019 DynProg Knapsack

    11/40

    Knapsack 0-1 Problem "Knapsack 0-1 Problem "

    Gecursi2e #ormulaGecursi2e #ormula

     /$e best subset o Sk t$at $as t$e total%eig$t % eit$er contains item k or not4

    +irst case, wk  > w◦ ,tem k  canBt be part o t$e solutionH , it %as

    t$e total %eig$t %ould be I % %$ic$ isunacceptable4

    -econd case, wk  ≤ w◦

     /$en t$e itemk  can be in t$e solution and %ec$oose t$e case %it$ greater 2alue4

  • 8/20/2019 DynProg Knapsack

    12/40

    Knapsack 0-1 !lgorit$mKnapsack 0-1 !lgorit$mfor w = 0 to W { // Initialize 1st row to 0’s

    B[0,w] = 0

    }

    for i = 1 to n { // Initialize 1st column to 0’s

    B[i,0] = 0

    }

    for i = 1 to n {

    for w = 0 to W {

    if wi 

  • 8/20/2019 DynProg Knapsack

    13/40

    Knapsack 0-1 ProblemKnapsack 0-1 Problem

    LetBs run our algorit$m on t$e ollo%ingdata;

    ◦ n J 7 (9 o elements

    ◦ : J 8 (ma@ %eig$t

    ◦ .lements (%eig$t 2alue;

    (56 (67 (78 (8

  • 8/20/2019 DynProg Knapsack

    14/40

    Knapsack 0-1 .@ampleKnapsack 0-1 .@ample

    i / w 0 1 2 3 4 0 0 0 0 0 0 0

    1 0

    2 0

    3 0

    4 0

    && Initiali'e the base (ases

    for w 0 to W

    *+0,w 0

    for i 1 to n

    *+i,0 0

  • 8/20/2019 DynProg Knapsack

    15/40

    Knapsack 0-1 .@ampleKnapsack 0-1 .@ample,tems;

    1;(56

    5;

    (67

    6;(78

    7;

    (8

    i / w 0 1 2 3 4

    0 0 0 0 0 0 0

    1 0

    2 0

    3 0

    4 0

    if wi  w &&item i (an be in the solution

      if vi % *+i-1,w-wi *+i-1,w

      *+i,w vi % *+i-1,w- wi

      else

      *+i,w *+i-1,w

    else B[i,w] = B[i-1,w] && wi  w

    i 1

    vi  /

    wi  2

    w 1w-wi -1

    i / w 0 1 2 3 4

    0 0 0 0 0 0 0

    1 0 0

    2 0

    3 0

    4 0

  • 8/20/2019 DynProg Knapsack

    16/40

    Knapsack 0-1 .@ampleKnapsack 0-1 .@ample,tems;

    1;(56

    5;

    (67

    6;(78

    7;

    (8

    i / w 0 1 2 3 4

    0 0 0 0 0 0 0

    1 0 0

    2 0

    3 0

    4 0

    if wi  w &&item i (an be in the solution

      if vi % *+i-1,w-wi *+i-1,w

      *+i,w vi % *+i-1,w- wi

      else

      *+i,w *+i-1,w

    else *+i,w *+i-1,w && wi  w

    i 1

    vi  /

    wi  2

    w = 2w-wi 0

    i / w 0 1 2 3 4

    0 0 0 0 0 0 0

    1 0 0 /

    2 0

    3 0

    4 0

    if wi  w &&item i (an be in the solution

      if vi % *+i-1,w-wi *+i-1,w

      B[i,w] = vi + B[i-1,w- wi]

      else

      *+i,w *+i-1,w

    else *+i,w *+i-1,w && wi  w

    ,

  • 8/20/2019 DynProg Knapsack

    17/40

    Knapsack 0-1 .@ampleKnapsack 0-1 .@ample,tems;

    1;(56

    5;

    (67

    6;(78

    7;

    (8

    i / w 0 1 2 3 4

    0 0 0 0 0 0 0

    1 0 0 /

    2 0

    3 0

    4 0

    if wi  w &&item i (an be in the solution

      if vi % *+i-1,w-wi *+i-1,w

      B[i,w] = vi + B[i-1,w- wi]

      else

      *+i,w *+i-1,w

    else *+i,w *+i-1,w && wi  w

    i 1

    vi  /

    wi  2

    w = 3w-wi 1

    i / w 0 1 2 3 4

    0 0 0 0 0 0 0

    1 0 0 / /

    2 0

    3 0

    4 0

    ,t

  • 8/20/2019 DynProg Knapsack

    18/40

    Knapsack 0-1 .@ampleKnapsack 0-1 .@ample,tems;

    1;(56

    5;

    (67

    6;(78

    7;

    (8

    i / w 0 1 2 3 4

    0 0 0 0 0 0 0

    1 0 0 / /

    2 0

    3 0

    4 0

    if wi  w &&item i (an be in the solution

      if vi % *+i-1,w-wi *+i-1,w

      B[i,w] = vi + B[i-1,w- wi]

      else

      *+i,w *+i-1,w

    else *+i,w *+i-1,w && wi  w

    i 1

    vi  /

    wi  2

    w = 4w-wi 2

    i / w 0 1 2 3 4

    0 0 0 0 0 0 0

    1 0 0 / / /

    2 0

    3 0

    4 0

    ,t

  • 8/20/2019 DynProg Knapsack

    19/40

    Knapsack 0-1 .@ampleKnapsack 0-1 .@ample,tems;

    1;(56

    5;

    (67

    6;(78

    7;

    (8

    i / w 0 1 2 3 4

    0 0 0 0 0 0 0

    1 0 0 / / /

    2 0

    3 0

    4 0

    if wi  w &&item i (an be in the solution

      if vi % *+i-1,w-wi *+i-1,w

      B[i,w] = vi + B[i-1,w- wi]

      else

      *+i,w *+i-1,w

    else *+i,w *+i-1,w && wi  w

    i 1

    vi  /

    wi  2

    w = w-wi /

    i / w 0 1 2 3 4

    0 0 0 0 0 0 0

    1 0 0 / / / /

    2 0

    3 0

    4 0

    ,t

  • 8/20/2019 DynProg Knapsack

    20/40

    Knapsack 0-1 .@ampleKnapsack 0-1 .@ample,tems;

    1;(56

    5;

    (67

    6;(78

    7;

    (8

    i / w 0 1 2 3 4

    0 0 0 0 0 0 0

    1 0 0 / / / /

    2 0

    3 0

    4 0

    if wi  w &&item i (an be in the solution

      if vi % *+i-1,w-wi *+i-1,w

      *+i,w vi % *+i-1,w- wi

      else

      *+i,w *+i-1,w

    else *+i,w *+i-1,w && wi  w

    i 2

    vi 

    wi  /

    w = 1w-wi -2

    i / w 0 1 2 3 4

    0 0 0 0 0 0 0

    1 0 0 / / / /

    2 0 0

    3 0

    4 0

    if wi  w &&item i (an be in the solution

      if vi % *+i-1,w-wi *+i-1,w

      *+i,w vi % *+i-1,w- wi

      else

      *+i,w *+i-1,w

    else B[i,w] = B[i-1,w] && wi  w

    ,t

  • 8/20/2019 DynProg Knapsack

    21/40

    Knapsack 0-1 .@ampleKnapsack 0-1 .@ample,tems;

    1;(56

    5;

    (67

    6;(78

    7;

    (8

    i / w 0 1 2 3 4

    0 0 0 0 0 0 0

    1 0 0 / / / /

    2 0 0

    3 0

    4 0

    if wi  w &&item i (an be in the solution

      if vi % *+i-1,w-wi *+i-1,w

      *+i,w vi % *+i-1,w- wi

      else

      *+i,w *+i-1,w

    else B[i,w] = B[i-1,w] && wi  w

    i 2

    vi 

    wi  /

    w = 2w-wi -1

    i / w 0 1 2 3 4

    0 0 0 0 0 0 0

    1 0 0 / / / /

    2 0 0 /

    3 0

    4 0

    ,tems

  • 8/20/2019 DynProg Knapsack

    22/40

    Knapsack 0-1 .@ampleKnapsack 0-1 .@ample,tems;

    1;(56

    5;

    (67

    6;(78

    7;

    (8

    i / w 0 1 2 3 4

    0 0 0 0 0 0 0

    1 0 0 / / / /

    2 0 0 /

    3 0

    4 0

    if wi  w &&item i (an be in the solution

      if vi % *+i-1,w-wi *+i-1,w

      *+i,w vi % *+i-1,w- wi

      else

      *+i,w *+i-1,w

    else *+i,w *+i-1,w && wi  w

    i 2

    vi 

    wi  /

    w = 3w-wi 0

    i / w 0 1 2 3 4

    0 0 0 0 0 0 0

    1 0 0 / / / /

    2 0 0 /

    3 0

    4 0

    if wi  w &&item i (an be in the solution

      if vi % *+i-1,w-wi *+i-1,w

      B[i,w] = vi + B[i-1,w- wi]

      else

      *+i,w *+i-1,w

    else *+i,w *+i-1,w && wi  w

    ,tems;

  • 8/20/2019 DynProg Knapsack

    23/40

    Knapsack 0-1 .@ampleKnapsack 0-1 .@ample,tems;

    1;(56

    5;

    (67

    6;(78

    7;

    (8

    i / w 0 1 2 3 4

    0 0 0 0 0 0 0

    1 0 0 / / / /

    2 0 0 /

    3 0

    4 0

    if wi  w &&item i (an be in the solution

      if vi % *+i-1,w-wi *+i-1,w

      B[i,w] = vi + B[i-1,w- wi]

      else

      *+i,w *+i-1,w

    else *+i,w *+i-1,w && wi  w

    i 2

    vi 

    wi  /

    w = 4w-wi 1

    i / w 0 1 2 3 4

    0 0 0 0 0 0 0

    1 0 0 / / / /

    2 0 0 /

    3 0

    4 0

    ,tems;

  • 8/20/2019 DynProg Knapsack

    24/40

    Knapsack 0-1 .@ampleKnapsack 0-1 .@ample,tems;

    1;(56

    5;

    (67

    6;(78

    7;

    (8

    i / w 0 1 2 3 4

    0 0 0 0 0 0 0

    1 0 0 / / / /

    2 0 0 /

    3 0

    4 0

    if wi  w &&item i (an be in the solution

      if vi % *+i-1,w-wi *+i-1,w

      B[i,w] = vi + B[i-1,w- wi]

      else

      *+i,w *+i-1,w

    else *+i,w *+i-1,w && wi  w

    i 2

    vi 

    wi  /

    w = w-wi 2

    i / w 0 1 2 3 4

    0 0 0 0 0 0 0

    1 0 0 / / / /

    2 0 0 /

    3 0

    4 0

    ,tems;

  • 8/20/2019 DynProg Knapsack

    25/40

    Knapsack 0-1 .@ampleKnapsack 0-1 .@ample,tems;

    1;(56

    5;

    (67

    6;(78

    7;

    (8

    i / w 0 1 2 3 4

    0 0 0 0 0 0 0

    1 0 0 / / / /

    2 0 0 /

    3 0

    4 0

    if wi  w &&item i (an be in the solution

      if vi % *+i-1,w-wi *+i-1,w

      *+i,w vi % *+i-1,w- wi

      else

      *+i,w *+i-1,w

    else *+i,w *+i-1,w && wi  w

    i /

    vi 

    wi 

    w = 1!!3w-wi -/..-1

    i / w 0 1 2 3 4

    0 0 0 0 0 0 0

    1 0 0 / / / /

    2 0 0 /

    3 0 0 /

    4 0

    if wi  w &&item i (an be in the solution

      if vi % *+i-1,w-wi *+i-1,w

      *+i,w vi % *+i-1,w- wi

      else

      *+i,w *+i-1,w

    else B[i,w] = B[i-1,w] && wi  w

    ,tems;

  • 8/20/2019 DynProg Knapsack

    26/40

    Knapsack 0-1 .@ampleKnapsack 0-1 .@ample,tems;

    1;(56

    5;

    (67

    6;(78

    7;

    (8

    i / w 0 1 2 3 4

    0 0 0 0 0 0 0

    1 0 0 / / / /

    2 0 0 /

    3 0 0 /

    4 0

    if wi  w &&item i (an be in the solution

      if vi % *+i-1,w-wi *+i-1,w

      *+i,w vi % *+i-1,w- wi

      else

      *+i,w *+i-1,w

    else *+i,w *+i-1,w && wi  w

    i /

    vi 

    wi 

    w = 4

    w-wi 0

    i / w 0 1 2 3 4

    0 0 0 0 0 0 0

    1 0 0 / / / /

    2 0 0 /

    3 0 0 /

    4 0

    if wi  w &&item i (an be in the solution

      if vi % *+i-1,w-wi *+i-1,w

      B[i,w] = vi + B[i-1,w- wi]

      else

      *+i,w *+i-1,w

    else *+i,w *+i-1,w && wi  w

    ,tems;

  • 8/20/2019 DynProg Knapsack

    27/40

    Knapsack 0-1 .@ampleKnapsack 0-1 .@ample,tems;

    1;(56

    5;

    (67

    6;(78

    7;

    (8

    i / w 0 1 2 3 4

    0 0 0 0 0 0 0

    1 0 0 / / / /

    2 0 0 /

    3 0 0 /

    4 0

    if wi  w &&item i (an be in the solution

      if vi % *+i-1,w-wi *+i-1,w

      *+i,w vi % *+i-1,w- wi  else

      *+i,w *+i-1,w

    else *+i,w *+i-1,w && wi  w

    i /

    vi 

    wi 

    w =

    w-wi 1

    i / w 0 1 2 3 4

    0 0 0 0 0 0 0

    1 0 0 / / / /

    2 0 0 /

    3 0 0 /

    4 0

    if wi  w &&item i (an be in the solution

      if vi % *+i-1,w-wi *+i-1,w

      *+i,w vi % *+i-1,w- wi

      else

      B[i,w] = B[i-1,w]

    else *+i,w *+i-1,w && wi  w

    ,tems;

  • 8/20/2019 DynProg Knapsack

    28/40

    Knapsack 0-1 .@ampleKnapsack 0-1 .@ample,tems;

    1;(56

    5;

    (67

    6;(78

    7;

    (8

    i / w 0 1 2 3 4

    0 0 0 0 0 0 0

    1 0 0 / / / /

    2 0 0 /

    3 0 0 /

    4 0

    if wi  w &&item i (an be in the solution

      if vi % *+i-1,w-wi *+i-1,w

      *+i,w vi % *+i-1,w- wi

      else

      *+i,w *+i-1,w

    else *+i,w *+i-1,w && wi  w

    i

    vi  3

    wi 

    w = 1!!4

    w-wi -..-1

    i / w 0 1 2 3 4

    0 0 0 0 0 0 0

    1 0 0 / / / /

    2 0 0 /

    3 0 0 /

    4 0 0 /

    if wi  w &&item i (an be in the solution

      if vi % *+i-1,w-wi *+i-1,w

      *+i,w vi % *+i-1,w- wi

      else

      *+i,w *+i-1,w

    else B[i,w] = B[i-1,w] && wi  w

    ,tems;

  • 8/20/2019 DynProg Knapsack

    29/40

    Knapsack 0-1 .@ampleKnapsack 0-1 .@ample,tems;

    1;(56

    5;

    (67

    6;(78

    7;

    (8

    i / w 0 1 2 3 4

    0 0 0 0 0 0 0

    1 0 0 / / / /

    2 0 0 /

    3 0 0 /

    4 0 0 /

    if wi  w &&item i (an be in the solution

      if vi % *+i-1,w-wi *+i-1,w

      *+i,w vi % *+i-1,w- wi

      else

      *+i,w *+i-1,w

    else *+i,w *+i-1,w && wi  w

    i

    vi  3

    wi 

    w =

    w-wi 0

    i / w 0 1 2 3 4

    0 0 0 0 0 0 0

    1 0 0 / / / /

    2 0 0 /

    3 0 0 /

    4 0 0 /

    if wi  w &&item i (an be in the solution

      if vi % *+i-1,w-wi *+i-1,w

      *+i,w vi % *+i-1,w- wi

      else

      B[i,w] = B[i-1,w]

    else *+i,w *+i-1,w && wi  w

    ,tems;

  • 8/20/2019 DynProg Knapsack

    30/40

    Knapsack 0-1 .@ampleKnapsack 0-1 .@ample,tems;

    1;(56

    5;

    (67

    6;(78

    7;

    (8

    i / w 0 1 2 3 4

    0 0 0 0 0 0 0

    1 0 0 / / / /

    2 0 0 /

    3 0 0 /

    4 0 0 / "

    We4re 567899

    :he ma; "ossible value that (an be (arrie< in this !na"sa(! is $7 

  • 8/20/2019 DynProg Knapsack

    31/40

    Knapsack 0-1 !lgorit$mKnapsack 0-1 !lgorit$m

     /$is algorit$m only 3nds t$e ma@possible 2alue t$at can be carriedin t$e knapsack

    ◦ /$e 2alue in >n:M

     /o kno% t$e items t$at make

    t$is ma@imum 2alue %e need totrace back t$roug$ t$e table4

    K k 0 1 !l it$K k 0 1 !l it$

  • 8/20/2019 DynProg Knapsack

    32/40

    Knapsack 0-1 !lgorit$mKnapsack 0-1 !lgorit$m

    #inding t$e ,tems#inding t$e ,tems

    Let i n an< ! W

    if *+i, ! = *+i-1, ! then

    mar! the ith item as in the !na"sa(! 

    i i-1, ! !-wi

    else

    i i-1 && >ssume the ith item is not in the !na"sa(! 

    && ?oul< it be in the o"timall@ "a(!e< !na"sa(!A

    K k 0 1K k 0 1,tems; Knapsac

  • 8/20/2019 DynProg Knapsack

    33/40

    Knapsack 0-1Knapsack 0-1

    !lgorit$m!lgorit$m

    #inding t$e ,tems#inding t$e ,tems

    ,tems;

    1;(56

    5;

    (67

    6;(78

    7;

    (8

    i

    !

    vi  3

    wi 

    B[i,k] = "

    *+i-1,!

    i / w 0 1 2 3 4

    0 0 0 0 0 0 0

    1 0 0 / / / /

    2 0 0 /

    3 0 0 / 4 0 0 /

    i n , ! W

    while i, ! 0

    if B[i, k] ≠ B[i-1, k] thenmark the ith item as in the knapsack 

    i = i-1, k = k-wielse

    i = i-1

    Knapsack;

    K k 0 1Knapsack 0 1,tems; Knapsac

  • 8/20/2019 DynProg Knapsack

    34/40

    Knapsack 0-1Knapsack 0-1

    !lgorit$m!lgorit$m

    #inding t$e ,tems#inding t$e ,tems

    ,tems;

    1;(56

    5;

    (67

    6;(78

    7;

    (8

    i /

    !

    vi 

    wi 

    B[i,k] = "

    *+i-1,!

    i / w 0 1 2 3 4

    0 0 0 0 0 0 0

    1 0 0 / / / /

    2 0 0 /

    3 0 0 / 4 0 0 /

    i n , ! W

    while i, ! 0

    if B[i, k] ≠ B[i-1, k] thenmark the ith item as in the knapsack 

    i = i-1, k = k-wielse

    i = i-1

    Knapsack;

    K k 0 1Knapsack 0 1,tems; Knapsac

  • 8/20/2019 DynProg Knapsack

    35/40

    Knapsack 0-1Knapsack 0-1

    !lgorit$m!lgorit$m

    #inding t$e ,tems#inding t$e ,tems

    ,tems;

    1;(56

    5;

    (67

    6;(78

    7;

    (8

    i 2

    !

    vi 

    wi  /

    B[i,k] = "

    *+i-1,! /

    ! – wi  2

    i / w 0 1 2 3 4

    0 0 0 0 0 0 0

    1 0 0 / / / /

    2 0 0 /

    3 0 0 / 4 0 0 /

    i n , ! W

    while i, ! 0

    if B[i, k] ≠ B[i-1, k] thenmark the ith item as in the knapsack 

    i = i-1, k = k-wielse

    i = i-1

    Knapsack;Item 2

    K k 0 1Knapsack 0 1,tems; Knapsac

  • 8/20/2019 DynProg Knapsack

    36/40

    Knapsack 0-1Knapsack 0-1

    !lgorit$m!lgorit$m

    #inding t$e ,tems#inding t$e ,tems

    ,tems;

    1;(56

    5;

    (67

    6;(78

    7;

    (8

    i 1

    ! 2

    vi  /

    wi  2

    B[i,k] = 3

    *+i-1,! 0

    ! – wi  0

    i / w 0 1 2 3 4

    0 0 0 0 0 0 0

    1 0 0 / / / /

    2 0 0 /

    30 0 / 4 0 0 /

    i n , ! W

    while i, ! 0

    if B[i, k] ≠ B[i-1, k] thenmark the ith item as in the knapsack 

    i = i-1, k = k-wielse

    i = i-1

    Knapsack;

    Item 1

    Item 2

    Knapsack 0 1Knapsack 0 1,tems; Knapsac

  • 8/20/2019 DynProg Knapsack

    37/40

    Knapsack 0-1Knapsack 0-1

    !lgorit$m!lgorit$m

    #inding t$e ,tems#inding t$e ,tems

    ,tems;

    1;(56

    5;

    (67

    6;(78

    7;

    (8

    i 1

    ! 2

    vi  /

    wi  2

    B[i,k] = 3

    *+i-1,! 0

    ! – wi  0

    i / w 0 1 2 3 4

    0 0 0 0 0 0 0

    1 0 0 / / / /

    2 0 0 /

    3 0 0 /

    4 0 0 /

    k = 0, so we#re $%&'(

    )he o*tima ka*sak sho. otai

     Item 1 and Item 2

    Knapsack;

    Item 1

    Item 2

  • 8/20/2019 DynProg Knapsack

    38/40

    Knapsack 0-1 Problem " GunKnapsack 0-1 Problem " Gun

     /ime /imefor w 0 to W

    *+0,w 0

    for i 1 to n

    *+i,0 0

    for i 1 to n

    for w 0 to W

    the rest of the (o

  • 8/20/2019 DynProg Knapsack

    39/40

    Knapsack ProblemKnapsack Problem

    1 #ill out t$e

    dynamicprogrammingtable or t$e

    knapsackproblem tot$e rig$t4

    5  /race backt$roug$ t$etable to 3ndt$e items in

    t$e knapsack4

  • 8/20/2019 DynProg Knapsack

    40/40

    GeerencesGeerences

    Slides adapted rom !rup Nu$aBsComputer Science ,, Lecture notes;$ttp;OO%%%4cs4uc4eduOdmarinoOucOcop6806OlecturesO

    !dditional material rom t$e te@tbook;

    Data Structures and !lgorit$m !nalysis in Qa2a (Second .dition by )ark !llen :eiss

    !dditional images;%%%4%ikipedia4com

    @kcd4com

    http://www.cs.ucf.edu/~dmarino/ucf/cop3503/lectures/http://www.cs.ucf.edu/~dmarino/ucf/cop3503/lectures/http://www.wikipedia.com/http://xkcd.com/http://xkcd.com/http://www.wikipedia.com/http://www.cs.ucf.edu/~dmarino/ucf/cop3503/lectures/http://www.cs.ucf.edu/~dmarino/ucf/cop3503/lectures/