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

cs3000 algorithms data jonathan ullman
SMART_READER_LITE
LIVE PREVIEW

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&


slide-1
SLIDE 1

CS3000:&Algorithms&&&Data Jonathan&Ullman

Lecture&7:&

  • Dynamic&Programming:&Knapsacks,&Edit&Distance

Jan&29,&2020

slide-2
SLIDE 2

TugGofGWar,&SubsetGSum,&Knapsack

slide-3
SLIDE 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

Sy

73

75

slide-4
SLIDE 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, … , !
  • Value&3

4 = ∑

./

/∈4

as&large&as&possible

  • Weight&7

4 = ∑

"/

/∈4

at&most&0

  • SubsetSum: ./ = "/

Tug of War

ri

wi

T I

wi

slide-5
SLIDE 5

Item 1

wi

It 1

Vi

3

Hem 2 wi

Ia

Vi 2

Hem 3

u

Iz

Vi

2

Interval scheduling Giver

n items

Wanted to find the

best

subset

Se El

n

Is the nth item in the optimal set

Segmented Least

Squares Given

n

points

in

  • rder

t

  • f
  • Xu

Find the best patton of the

n points

What is the last segment in the optimal partition

slide-6
SLIDE 6

Dynamic&Programming

  • Let&8 ⊆ 1, … , ! be&the&optimal subset&of&items

Should

item

n go in the optimal solution T

Case L

NEO

n not in 0

µ

Then

is the optimal solution for items

b

in 1 and capacity T

Case

2 NEO

no

in 0

Then

is n

t the optimal solution for

T un

items 1

in 1 and capacity T un

µ

slide-7
SLIDE 7

Dynamic&Programming

  • Let&9:;(=, >) be&the&value'of&the&optimal&subset&of&

items& 1, … , @ in&a&knapsack&of&size&1

  • Case'1:'@ ∉ 8/,4
  • Case'2: @ ∈ 8/,4

U

OE i En OES ET

0PT

i

D

OPT

i

t

5

OPT

i

D

crit 0PT

i l

S

w

OPT its

f

S

S

if

wi

S

OPT

i l S

slide-8
SLIDE 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 OPT @ − 1, 1 LLLLLL

Base'Cases:

OPT @, 0 = OPT 0, 1 = 0 if wies

if

wi

S

slide-9
SLIDE 9

Ask&the&Audience

  • Input:&0 = 8, ! = 3
  • "# = 1 , .# = 4
  • "O = 3 , .O = 5
  • "P = 5 , .P = 8

3 2 1 < 1 2 3 4 5 6 7 8

capacities items

ifwies

if u s

03,8 833 02,3 33 23 t Oyo

2,33

O

4

4

5

9 9

12

12g By

O

O

4

4 rn

5

9g 9g 9g 9g 9g

the 4g 4

riff

fifty

O

O

O

O

O

O

O

O

O

O

slide-10
SLIDE 10

Knapsack&(“BottomGUp”)

// All inputs are global vars FindOPT(n,T): M[0,S]L←L0, M[i,0]L←L0 for (S = 1,…,T): for (i = 1,…,n): if (wi > S): M[i,S] ←LM[i-1,S] else: M[i]L←Lmax{M[i-1,S],vi + M[i-1,S-wi]} return M[n,T]

slide-11
SLIDE 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 (wn > T): return FindSol(M,n-1,T) else: if (M[n-1,T] > vn + M[n-1,T-wn]): return FindSol(M,n-1,T) else: return {n} + FindSol(M,n-1,T-wn)

slide-12
SLIDE 12

Knapsack&Wrapup

  • Can&solve&knapsack in&time/space&8 !0
  • Brute&force&algorithms&runs&in&time&8 2&
  • Dynamic&Programming:
  • Decide&whether&the&nth item&goes&in&the&knapsack
  • Solve&subset<sum and&tug<of<war as&special&cases
slide-13
SLIDE 13

Edit&Distance Alignments

slide-14
SLIDE 14

Distance&Between&Strings

  • Autocorrect&works&by&finding&similar&strings
  • ocurrance and&occurrence seem&similar,&but&
  • nly&if&we&define&similarity&carefully
  • currance
  • ccurrence
  • c urrance
  • ccurrence
slide-15
SLIDE 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

  • c

u r r a n c e

  • c

c u r r e n c e

The edit d st btw

x

y

is the

cost of the

mm costaligned

slide-16
SLIDE 16

Ask&the&Audience

  • What&is&the&minimum&cost&alignment&of&the&strings&

smitten and&sitting

O

DEFEE

slide-17
SLIDE 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
slide-18
SLIDE 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 (&−,UV )
  • Case'III:'use&one&symbol&from&each&(&S&,UV )

Can I

Case 4

Case

X

Xn

l n

X Xn

X

Xn

n

n

Y Ym Yi

Ym l Im

Yi

Ym 1

Ym

  • ptimal alignment
  • ptimalalignment
  • ptimal alignment

for

X

Xn I

Y Ym

slide-19
SLIDE 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 (&−, UV )
  • insertion&+&optimal&alignment&of&S#:&,U#:VX#
  • Case'III:'use&one&symbol&from&each&(&S&, UV )
  • If&S& = UV:&optimal&alignment&of&S#:&X#,U#:VX#
  • If&S& ≠ UV:&mismatch&+&opt.&alignment&of&S#:&X#,U#:VX#
slide-20
SLIDE 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 (&−, UC )
  • Case'III:'use&one&symbol&from&each&(&S/, UC )

OPT ily

f

if Xity

It

mm

OPTfit

1

OPT

i j D

  • pt

i l

slide-21
SLIDE 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 (&−, UC )
  • Case'III:'use&one&symbol&from&each&(&S/, UC )

Recurrence:

OPT @,[ = G 1 + min OPT @ − 1, [ ,OPT @,[ − 1 ,OPT(@ − 1, [ − 1)L min{1 + 8_0 @ − 1,[ ,1 + OPT @, [ − 1 ,OPT(@ − 1,[ − 1)}LLLLLL

Base'Cases:

OPT @,0 = @,&OPT 0,[ = [

fr

if

Xi y

if

E Yj

slide-22
SLIDE 22

Example

x = pert y = beast

  • b

e a s t

  • p

e r t

OPT

111 Of111M

A T

OP

Thi

slide-23
SLIDE 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 (&−, UC )
  • Case'III:'use&one&symbol&from&each&(&S/, UC )
slide-24
SLIDE 24

Knapsack&(“BottomGUp”)

// All inputs are global vars FindOPT(n,m): M[0,j]L←Lj, M[i,0]L←Li 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]

slide-25
SLIDE 25

Ask&the&Audience

  • Suppose&inserting/deleting'costs'a > c and&

swapping'd ↔ f costs'gd,f > c

  • Write&a&recurrence&for&the&minGcost&alignment
slide-26
SLIDE 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!
slide-27
SLIDE 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.
slide-28
SLIDE 28

Saving&Space

  • Input: Two&strings&S ∈ Σ&, U ∈ ΣV
  • Output: The&minimum'cost'alignment S and&U
  • Can&we&still&use&8(! + h) space?
slide-29
SLIDE 29

Saving&Space

slide-30
SLIDE 30

Saving&Space

slide-31
SLIDE 31

Summary

  • Can&compute&the&edit&distance,&or&minimum&cost&

alignment between&two&strings&in&time 8 !h and& space 8 ! + h