You are on page 1of 16

Revisit Dynamic

Programming
Consider a directed graph G  (V , E ) with positive arc
weight w : E  R  . Show that for any vertex s V ,
there exists an arc (u s , vs ) such that every shortest path
tree rooted at s contains (us , vs ).

w1
w3 w2

w1  w2  w3  
Technique 1
• Find a related optimization problem with
self-reducibility.
• Solve the related problem by dynamic
programming.
• Solve original problem.
A problem on strip: outside weighted disks cover
Inside points with minimum total weight.

p2
p1 pi

Ti(D,D’) : minimum weight set


with D, D’, dominating p1, …, pi such that
(1) D (lowest intersection point on L)
among disks above the strip
(2) D’(highest intersection point on L)
among disks below the strip
D1 (lowest intersection point on L)
among disks above the strip, in Ti(D,D’)

p2
p1 pi-1

D2 (highest intersection point on L)


among disks below the strip, in Ti(D,D’)
Dynamic Programming

Ti ( D,D' ) = min D1 , D2 {Ti 1 ( D1,D2 )


+ [ D1  D]c( D)
+ [ D2  D' ]c( D' )}
where
D1 is over all disk each having intersecti on point not lower than
that of D with line passing pi , D2 is over all disks each having
intersecti on point not higher tha n that of D' with line passing pi ,
1 if D1  D,
[ D1  D]  
0 otherwise.
Dynamic Programming

w(Ti ( D,D' )) = min D1 , D2 {w(Ti 1 ( D1,D2 ) )


+ [ D1  D]c( D)
+ [ D2  D' ]c( D' )}
where
D1 is over all disk each having intersecti on point not lower than
that of D with line passing pi , D2 is over all disks each having
intersecti on point not higher tha n that of D' with line passing pi ,
1 if D1  D,
[ D1  D]  
0 otherwise.
If D covers p j ( j  i  1), so does D1.

D1

pi-1
pi
pj
D
Knapsack
max c1 x1  c 2 x 2    cnxn
s. t. s1 x1  s 2 x 2    snxn  S
xi {0, 1}.
Assume si  S for any i. Hence ci  opt for any i.

Pseudo Polynomial-time
Algorithm for Knapsack
DP-type Algorithm

c(i, j ) is a subset of indeces in {1, ..., i} such that


 ic ( i , j )
ci  j, 
ic ( i , j )
si  S and

 ic ( i , j )
si  min {iI si | iI ci  j ,
I  {1,..., i}}.
c(i, j )  nil if no such subset exists.
• Initially,

for j  1, ... csum  c1    cn,


  if j  0,

c(1, j )  {1} if j  c1,
nil otherwise

for i  2 to n do
for j  0 to csum do
Case 1. c(i  1, j  ci )  nil ,
Set c(i, j )  c(i  1, j );
Case 2. c(i  1, j  ci )  nil , but
si  kc ( i 1, j ci ) sk  S .
Set c(i, j )  c(i  1, j ).
Case 3. c(i  1, j  ci )  nil , si  kc (i 1, j ci ) sk  S ,
and c(i  1, j )  nil .
Set c(i, j )  c(i  1, j  ci )  {i}.
Case 4. c(i  1, j  ci )  nil , si  kc (i 1, j ci ) sk  S ,
and c(i  1, j )  nil .
If si  kc (i 1, j ci ) sk  kc ( i 1, j ) sk
then set c(i, j )  c(i  1, j )
else set c(i, j )  c(i  1, j  ci )  {i}.
Output opt  max{ j | c(i, j )  nil }.
Time
• outside loop: O(n)
• Inside loop: O(nM) where M=max ci
• Core: O(n log (MS))
3
• Total O(n M log (MS))
• Since input size is O(n log (MS)), this is a
log M
pseudo-polynomial-time due to M=2
Running time
(1) Time for computing one element with
recursive formula.
(2) Size of the table.

Running time = (1) x (2)


Technique 2
Speed Up

You might also like