CS3000:&Algorithms&&&Data Jonathan&Ullman
Lecture&7:&
- Dynamic&Programming:&Knapsacks,&Edit&Distance
Jan&29,&2020
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&
Lecture&7:&
Jan&29,&2020
need&to&split&as&evenly&as&possible&into&two&teams
21,523
42,333
Sy
73
75
in&the&knapsack
4 = ∑
./
/∈4
as&large&as&possible
4 = ∑
"/
/∈4
at&most&0
ri
wi
T I
wi
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
Find the best patton of the
n points
What is the last segment in the optimal partition
Should
item
n go in the optimal solution T
Case L
n not in 0
Then
is the optimal solution for items
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
items& 1, … , @ in&a&knapsack&of&size&1
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
items& 1, … , @ in&a&knapsack&of&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
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
O
4
4 rn
5
O
O
O
O
O
O
O
O
O
// 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]
// 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)
is&the&number&of&insertions,&deletions,&and&swaps required&to&turn&S into&U.
positions&where&the&two&strings&don’t&agree
u r r a n c e
c u r r e n c e
The edit d st btw
x
y
is the
cost of the
mm costaligned
smitten and&sitting
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
for
X
Xn I
Y Ym
OPT ily
f
if Xity
It
mm
OPTfit
1
OPT
i j D
i l
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,[ = [
if
Xi y
E Yj
x = pert y = beast
e a s t
e r t
OPT
111 Of111M
A T
OP
Thi
// 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]
swapping'd ↔ f costs'gd,f > c
between&two&strings&in&time/space&8 !h
alignment between&two&strings&in&time 8 !h and& space 8 ! + h