Rod cutting
cutting
Programacin
Programacin Dinmica
Dinmica
RodDEL
22 DEL FEBRERO Cutting
2017 1/11
Rod Cutting
ndice
Explicacin del problema
Introduccin al problema
Enunciado
Algoritmo Recursivo
Anlisis
Formulacin Recursiva
Hallmark #1: Subestructuras
ptimas
Demostracin
Implementacin
Anlisis del tiempo de Ejecucin
Hallmark #2: Subproblemas
repetidos
Top-Down Memoization
Rod Cutting 2/11
Rod Cutting
Explicacin del Problema
Introduccin
Introduccin
Serling Enterprises compra varillas largas de acero y las
corta en varillas mas pequeas, las cuales
posteriormente las vende obteniendo ingresos. Se desea
saber la mejor forma de cortar las varillas.
Longitu 1 2 3 4 5 6 7
di
Precio 1 5 8 9 10 16 17
pi
Rod Cutting 3/11
Rod Cutting
Explicacin del Problema
Enunciado
rn =
Longitu 1 2 3 4 5 6 7 12
di
Precio 1 5 8 9 10 16 17
pi
Rod Cutting 4/11
Rod Cutting
Explicacin del Problema
Enunciado
rn =
Longitu 1 2 3 4 5 6 7 15
di
Precio 1 5 8 9 10 16 17
pi
Rod Cutting 4/11
Rod Cutting
Explicacin del Problema
Enunciado
rn =
Longitu 1 2 3 4 5 6 7 17
di
Precio 1 5 8 9 10 16 17
pi
Rod Cutting 4/11
Rod Cutting
Explicacin del Problema
Enunciado
rn =
Longitu 1 2 3 4 5 6 7 18
di
Precio 1 5 8 9 10 16 17
pi
Rod Cutting 4/11
Rod Cutting
Algoritmo Recursivo
Anlisis
Ej. n = 4
a) b) c) d)
e) f) g) h)
Ej. n = 4 0
cortes
a) b) c) d)
e) f) g) h)
Ej. n = 4 1 corte
a) b) c) d)
e) f) g) h)
Ej. n = 4
a) b) c) d)
e) f) g) h)
2
cortes
Hay posiciones de corte, y se debe escoger cortes
Ej. n = 4
a) b) c) d)
e) f) g) h)
3
cortes
Hay posiciones de corte, y se debe escoger cortes
Ej. n = 4
a) b) c) d)
e) f) g) h)
Formulacin Recursiva
Notacin Aditiva:
con a =
Implementacin
Cut-Rod(p, n)
1 if n == 0
2 return 0
3 q = -inf
4 for i = 1 to n
5 q = max(q, p[i] + Cut-Rod(p, n - i))
6 return q
Por qu es ineficiente?
Cut-Rod(p, n) Cut-Rod(p, n-i) , i = 1,2, ... n
n
n1 n-2 ... 1 0
Tiempo Exponencial !
Mtodos
- Top-down con
Memoization
Memorization
Memoized-Cut-Rod (p, n)
1 let r[0...n] be a new array -inf -inf -inf ... -inf -inf -inf
2 for i = 0 to n
3 r[i] = -inf
4 return Memoized-Cut-Rod-Aux(p, n,
r)
Memoized-Cut-Rod-Aux(p, n, r)
1 if r[n] 0
2 return r[n]
3 if n == 0 n
4 q = 0
-inf -inf -inf ... q -inf -inf
5 else q = -inf
6 for i = 1 to n
7 q = max(q, p[i] + Memoized-Cut-Rod-Aux(p, n - i,
8 r))
9 r[n] = q
return q
Rod Cutting 11/11