cs3000 algorithms data jonathan ullman
play

CS3000:&Algorithms&&&Data Jonathan&Ullman - PowerPoint PPT Presentation

CS3000:&Algorithms&&&Data Jonathan&Ullman Lecture&7:& Dynamic&Programming:&Knapsacks,&Edit&Distance Jan&29,&2020 TugGofGWar,&SubsetGSum,&Knapsack TugGofGWar We&have&


  1. CS3000:&Algorithms&&&Data Jonathan&Ullman Lecture&7:& Dynamic&Programming:&Knapsacks,&Edit&Distance • Jan&29,&2020

  2. TugGofGWar,&SubsetGSum,&Knapsack

  3. TugGofGWar • We&have& ! students&with&weights& " # , … , " & ∈ ℕ ,& need&to&split&as&evenly&as&possible&into&two&teams • e.g.& 21,42,33,52 21,523 42,333 75 73 Sy

  4. The&Knapsack&Problem • Input:' ! items&for&your&knapsack • value& . / and&a&weight& " / ∈ ℕ for& ! items • capacity&of&your&knapsack& 0 ∈ ℕ • Output: the&most&valuable&subset&of&items&that&fits& in&the&knapsack • Subset& 1 ⊆ 1, … , ! 4 = ∑ • Value& 3 . / as&large&as&possible /∈4 4 = ∑ • Weight& 7 " / at&most& 0 /∈4 • SubsetSum: . / = " / T I Tug of War wi ri wi

  5. It 1 3 Item 1 Vi wi Ia 2 Hem 2 wi Vi Hem 3 Iz 2 Vi u Interval scheduling Giver n items Se El subset n best Wanted to find the Is the nth item in the optimal set Segmented Least Squares order Given in points n of t o Xu Find the best patton of the n points What is the last segment in the optimal partition

  6. Dynamic&Programming • Let& 8 ⊆ 1, … , ! be&the& optimal subset&of&items n go in the optimal solution Should T item µ NEO in 0 Case L n not is the optimal solution for items 0 Then in 1 and capacity T b in 0 NEO 2 no Case t the optimal solution for n Then is T un items 1 in 1 and capacity T un µ

  7. U Dynamic&Programming OES ET OE i En • Let& 9:;(=, >) be&the& value' of&the&optimal&subset&of& items& 1, … , @ in&a&knapsack&of&size& 1 • Case'1:' @ ∉ 8 /,4 D 5 i 0PT OPT t i • Case'2: @ ∈ 8 /,4 D S crit 0PT i l w OPT i S S f OPT its l S OPT S i if wi

  8. Dynamic&Programming • Let& 9:;(=, >) be&the& value' of&the&optimal&subset&of& items& 1, … , @ in&a&knapsack&of&size& 1 • Case'1:' @ ∉ 8 /,4 • Use&opt.&solution&for&items& 1 to& @ − 1 and&size& 1 • Case'2: @ ∈ 8 /,4 • Use& @ +&opt.&solution&for&items& 1 to& @ − 1 and&size& 1 − " C Recurrence: OPT @, 1 = Gmax OPT @ − 1, 1 , . / + OPT @ − 1, 1 − " / L if wies OPT @ − 1, 1 LLLLLL if S wi Base'Cases: OPT @, 0 = OPT 0, 1 = 0

  9. Ask&the&Audience if wies if u s • Input:& 0 = 8, ! = 3 03,8 833 02,3 • " # = 1 , . # = 4 23 t Oyo 33 • " O = 3 , . O = 5 2,33 • " P = 5 , . P = 8 3 9 9 12 4 4 5 12g By O 2 5 9g 9g 9g 9g 9g O 4 4 rn O items 1 fifty 0 the 4g 4 riff 0 O O O O O O O O O < 0 1 2 3 4 5 6 7 8 O capacities

  10. Knapsack&(“BottomGUp”) // All inputs are global vars FindOPT(n,T): M[0,S] L←L 0, M[i,0] L←L 0 for (S = 1,…,T): for (i = 1,…,n): if (w i > S): M[i,S] ←L M[i-1,S] else: M[i] L←L max{M[i-1,S],v i + M[i-1,S-w i ]} return M[n,T]

  11. Filling&the&Knapsack // All inputs are global vars // M[0:n,0:T] contains solutions to subproblems FindSol(M,n,T): if (n = 0 or T = 0): return ∅ else: if (w n > T): return FindSol(M,n-1,T) else: if (M[n-1,T] > v n + M[n-1,T-w n ]): return FindSol(M,n-1,T) else: return {n} + FindSol(M,n-1,T-w n )

  12. Knapsack&Wrapup • Can&solve& knapsack in&time/space& 8 !0 • Brute&force&algorithms&runs&in&time& 8 2 & • Dynamic&Programming: • Decide&whether&the&n th item&goes&in&the&knapsack • Solve& subset<sum and& tug<of<war as&special&cases

  13. Edit&Distance Alignments

  14. Distance&Between&Strings • Autocorrect&works&by&finding&similar&strings • ocurrance and& occurrence seem&similar,&but& only&if&we&define&similarity&carefully ocurrance oc urrance occurrence occurrence

  15. Edit&Distance&/&Alignments • Given&two&strings& S ∈ Σ & , U ∈ Σ V ,&the& edit'distance is&the&number&of&insertions,&deletions,&and&swaps required&to&turn& S into& U . • Given&an&alignment,&the&cost&is&the&number&of& positions&where&the&two&strings&don’t&agree o c u r r a n c e o c c u r r e n c e is the cost of the mm cost aligned The edit d st btw y x

  16. Ask&the&Audience • What&is&the&minimum&cost&alignment&of&the&strings& smitten and& sitting O DEFEE

  17. Edit&Distance&/&Alignments • Input: Two&strings& S ∈ Σ & , U ∈ Σ V • Output: The&minimum&cost&alignment&of& S and& U • Edit'Distance' =&cost&of&the&minimum&cost&alignment

  18. Dynamic&Programming • Consider&the& optimal alignment&of& S, U • Three&choices&for&the&final&column • Case'I:' only&use& S (& S & ,− ) • Case'II:' only&use& U (& −,U V ) • Case'III:' use&one&symbol&from&each&(& S & ,U V ) Case Can I Case 4 Xn X X Xn X Xn n l Ym l Im n n Y Ym Yi Yi Ym 1 Ym optimal alignment optimal alignment optimal alignment for X Xn I Y Ym

  19. Dynamic&Programming • Consider&the& optimal alignment&of& S, U • Case'I:' only&use& S (& S & , − ) • deletion&+&optimal&alignment&of& S #:&X# ,U #:V • Case'II:' only&use& U (& −, U V ) • insertion&+&optimal&alignment&of& S #:& ,U #:VX# • Case'III:' use&one&symbol&from&each&(& S & , U V ) • If& S & = U V :&optimal&alignment&of& S #:&X# ,U #:VX# • If& S & ≠ U V :&mismatch&+&opt.&alignment&of& S #:&X# ,U #:VX#

  20. Dynamic&Programming • 9:; =, Z =&cost&of&opt.&alignment&of& S #:/ and& U #:C • Case'I:' only&use& S (& S / , − ) • Case'II:' only&use& U (& −, U C ) • Case'III:' use&one&symbol&from&each&(& S / , U C ) f OPT ily i j D i l It OPT fit OPT opt if Xity 1 mm

  21. Dynamic&Programming • 9:; =, Z =&cost&of&opt.&alignment&of& S #:/ and& U #:C • Case'I:' only&use& S (& S / , − ) • Case'II:' only&use& U (& −, U C ) • Case'III:' use&one&symbol&from&each&(& S / , U C ) Xi y if Recurrence: fr 1 + min OPT @ − 1, [ ,OPT @,[ − 1 ,OPT(@ − 1, [ − 1)L OPT @,[ = G min{1 + 8_0 @ − 1,[ ,1 + OPT @, [ − 1 ,OPT(@ − 1,[ − 1)}LLLLLL if E Yj Base'Cases: OPT @,0 = @ ,& OPT 0,[ = [

  22. Example x = pert y = beast - b e a s t - Of111M 11 1 p OPT T A e Thi OP r t

  23. Finding&the&Alignment • 9:; =, Z =&cost&of&opt.&alignment&of& S #:/ and& U #:C • Case'I:' only&use& S (& S / , − ) • Case'II:' only&use& U (& −, U C ) • Case'III:' use&one&symbol&from&each&(& S / , U C )

  24. Knapsack&(“BottomGUp”) // All inputs are global vars FindOPT(n,m): M[0,j] L←L j, M[i,0] L←L i for (i= 1,…,n): for (j = 1,…,m): if (xi = yj): M[i,j] = min{1+M[i-1,j],1+M[i,j-1],M[i-1,j-1] elseif (xi != yj): M[i,j] = 1+min{M[i-1,j],M[i,j-1],M[i-1,j-1]} return M[n,m]

  25. Ask&the&Audience • Suppose& inserting/deleting'costs' a > c and& swapping' d ↔ f costs' g d,f > c • Write&a&recurrence&for&the&minGcost&alignment

  26. Summary • Compute&the& edit'distance ,&or& min<cost'alignment between&two&strings&in&time/space& 8 !h • Dynamic&Programming: • Decide&the&final&pair&of&symbols&in&the&alignment • Space&can&be&prohibitive&in&practice • Compute&edit&distance&in&space& 8 min !, h • Can&also&find&alignment&in&small&space!

  27. Saving&Space • Input: Two&strings& S ∈ Σ & , U ∈ Σ V • Output: The& edit'distance'between S and& U • Can&compute& ijk0(S, U) with& 8 ! + h space.

  28. Saving&Space • Input: Two&strings& S ∈ Σ & , U ∈ Σ V • Output: The& minimum'cost'alignment S and& U • Can&we&still&use& 8(! + h) space?

  29. Saving&Space

  30. Saving&Space

  31. Summary • Can&compute&the&edit&distance,&or&minimum&cost& alignment between&two&strings&in& time 8 !h and& space 8 ! + h

Recommend


More recommend