Document

Lecture 3: Dynamic Programming
主講人:虞台文
Content

What is Dynamic Programming?
Matrix Chain-Products
Sequence Alignments
Knapsack Problem
All-Pairs Shortest Path Problem
Traveling Salesman Problem

Conclusion





Lecture 3: Dynamic Programming
What is Dynamic
Programming?
What is Dynamic Programming?



Dynamic Programming (DP) tends to break the
original problem to sub-problems, i.e., in a smaller
size
The optimal solution in the bigger sub-problems is
found through a retroactive formula which
connects the optimal solutions of sub-problems.
Used when the solution to a problem may be
viewed as the result of a sequence of decisions.
Properties for Problems Solved by DP

Simple Subproblems
–

Optimal Substructure of the problems
–

The original problem can be broken into smaller
subproblems with the same structure
The solution to the problem must be a
composition of subproblem solutions (the
principle of optimality)
Subproblem Overlap
–
Optimal subproblems to unrelated problems can
contain subproblems in common
The Principle of Optimality



The basic principle of dynamic programming
Developed by Richard Bellman
An optimal path has the property that whatever
the initial conditions and control variables (choices)
over some initial period, the control (or decision
variables) chosen over the remaining period must
be optimal for the remaining problem, with the
state resulting from the early decisions taken to
be the initial condition.
Example: Shortest Path Problem
10
Start
5
3
Goal
Example: Shortest Path Problem
25
10
28
Start
Goal
5
40
3
Example: Shortest Path Problem
25
10
28
Start
Goal
5
40
3
Is the greedy solution optimal?
Recall  Greedy Method for
Shortest Paths on a Multi-stage Graph

Problem
–
Find a shortest path from v0 to v3
Is the greedy solution optimal?
Recall  Greedy Method for
Shortest Paths on a Multi-stage Graph

Problem
–
Find a shortest path from v0 to v3
The optimal path

Example  Dynamic Programming
3  d min (v1,1 , v3 )
1  d (v , v )

min 1,2
3
d min (v0 , v3 )  
5  d min (v1,3 , v3 )
7  d min (v1,4 , v3 )
Lecture 3: Dynamic Programming
Matrix Chain-Products
Matrix Multiplication
C=A×B
 A is d × e and B is e × f

f
B
e
e
Cij   Aik  Bkj
e
k 1
d

O(def )
j
A
C
i
i,j
f
d
Matrix Chain-Products



Given a sequence of matrices, A1, A2, …, An, find the
most efficient way to multiply them together.
Facts:
–
A(BC) = (AB)C
–
Different parenthesizing may need different numbers of
operation.
Example: A:10 × 30, B: 30 × 5, C : 5 × 60
–
(AB)C = (10×30×5) + (10×5×60) = 1500 + 3000 = 4500 ops
–
A(BC) = (30×5×60) + (10×30×60) = 9000 + 18000 = 27000 ops
Matrix Chain-Products



Given a sequence of matrices, A1, A2, …, An, find the
most efficient way to multiply them together.
A Brute-force Approach:
–
Try all possible ways to parenthesize A=A1A2…An
–
Calculate number of operations for each one
–
Pick the best one
Time Complexity:
–
#paranethesizations = #binary trees of n nodes
–
O(4n)
A Greedy Approach

Idea #1:
–

repeatedly select the product that uses the
most operations.
Counter-example:
–
–
–
A: 10  5, B: 5  10, C: 10  5, and D: 5  10
Greedy idea #1 gives (AB)(CD), which takes
500+1000+500 = 2000 ops
A((BC)D) takes 500+250+250 = 1000 ops
Another Greedy Approach

Idea #2:
–

repeatedly select the product that uses the
least operations.
Counter-example:
–
–
–
A: 101  11, B: 11  9, C: 9  100, and D: 100  999
Greedy idea #2 gives A((BC)D), which takes
109989+9900+108900=228789 ops
(AB)(CD) takes 9999+89991+89100=189090 ops
Ai : di  di 1
DP  Define Subproblem
Original
Problem
(P1n)
A1 
 Ai 
 Aj 
 An
Subproblem (Pij, i j)
Suppose #operations for the
optimal solution of Pij is Nij
#operations for the optimal solution
of the original problem P1n is N1n
Ai : di  di 1
DP  Define Subproblem
Original
Problem
(P1n)
A1 
 Ai 
 Aj 
 An
Subproblem (Pij, i j)
Suppose #operations for the
optimal solution of Pij is Nij
#operations for the optimal solution
of the original problem P1n is N1n
Ai : di  di 1
DP  Define Subproblem
Original
Problem
(P1n)
A1 
 Ai 
 Aj 
 An
Subproblem (Pij, i j)
Suppose #operations for the
optimal solution of Pij is Nij
#operations for the optimal solution
of the original problem P1n is N1n
Ai : di  di 1
DP  Principle of Optimality
didk+1
 Ai 
dkdj+1
 Ak    Ak 1 
Nik
 Aj 
Nk+1,n
Nijij  min  Nik  N k 1,1, j  di  d k 1  d j 1
ik  j
Nij  min  Nik  N k 1, j  di  d k 1  d j 1
ik  j
DP  Implementation
Nij 1 2
1
2
i
n
j
n
Nij  min  Nik  N k 1, j  di  d k 1  d j 1
ik  j
DP  Implementation
Nij 1 2
1
2
i
n
j
n
Nij  min  Nik  N k 1, j  di  d k 1  d j 1
ik  j
DP  Implementation
Nij 1 2
1
2
i
n
?
j
n
Nij  min  Nik  N k 1, j  di  d k 1  d j 1
ik  j
DP  Implementation
Nij 1 2
j
1
2
i
n
?
n
Nij  min  Nik  N k 1, j  di  d k 1  d j 1
ik  j
DP  Implementation
Nij 1 2
j
1
2
?
i
n
n
Nij  min  Nik  N k 1, j  di  d k 1  d j 1
ik  j
DP  Implementation
Nij 1 2
1
2
i
n
j
n
Nij  min  Nik  N k 1, j  di  d k 1  d j 1
ik  j
DP  Implementation
Nij 1 2
1
2
i
n
?
j
n
Nij  min  Nik  N k 1, j  di  d k 1  d j 1
ik  j
DP  Implementation
Nij 1 2
j
1
2
i
n
?
n
Nij  min  Nik  N k 1, j  di  d k 1  d j 1
ik  j
DP  Implementation
Nij 1 2
1
2
i
n
j
n
Nij  min  Nik  N k 1, j  di  d k 1  d j 1
ik  j
DP  Implementation
Nij 1 2
1
2
i
n
?
j
n
Nij  min  Nik  N k 1, j  di  d k 1  d j 1
ik  j
DP  Implementation
Nij 1 2
j
1
2
i
n
?
n
Nij  min  Nik  N k 1, j  di  d k 1  d j 1
ik  j
DP  Implementation
Nij 1 2
1
2
i
n
j
n
Nij  min  Nik  N k 1, j  di  d k 1  d j 1
ik  j
DP for Matrix Chain-Products
Algorithm matrixChain(S):
Input: sequence S of n matrices to be multiplied
Output: number of operations in an optimal parenthesization of S
for i 1 to n // main diagonal terms are all zero
Ni,i  0
for d  2 to n // each diagonal do following
for i 1 to nd+1 // do from top to bottom for each diagonal
j  i+d1
Ni,j  infinity
for k  i to j1 // counting minimum
Ni,j  min(Ni,j, Ni,k +Nk+1,j +di dk+1 dj+1)
Nij  min  Nik  N k 1, j  di  d k 1  d j 1
ik  j
Time Complexity
Algorithm matrixChain(S):
Input: sequence S of n matrices to be multiplied
Output: number of operations in an optimal parenthesization of S
for i 1 to n // main diagonal terms are all zero
Ni,i  0
for d  2 to n // each diagonal do following
for i 1 to nd+1 // do from top to bottom for each diagonal
j  i+d1
Ni,j  infinity
for k  i to j1 // counting minimum
Ni,j  min(Ni,j, Ni,k +Nk+1,j +di dk+1 dj+1)
3
O(n )
Nij  min  Nik  N k 1, j  di  d k 1  d j 
ik  j
Exercises
1.
The matrixChain algorithm only computes
#operations of an optimal
parenthesization. But, it doesn’t report
the optimal parenthesization scheme.
Please modify the algorithm so that it can
do so.
2.
Given an example with 5 matrices to
illustrate your idea using a table.
Lecture 3: Dynamic Programming
Sequence Alignment
Question
 Given
two strings
X  x1 , x2 ,
, xm
and
Y  y1 , y2 ,
are they similar?
what is their distance?
, yn
Example
X: applicable
Y: plausibly
How similar they are?
Can you give them a score?
Example
X: applicable
Y: plausibly
Mismatch
Match
Match
Indel
Indel
Indel
Indel
Match
Indel
Match
Indel
Match
Indel
X’: applica---ble
Y’: -p-l--ausibly
Matches
Three cases: Mismatches
Insertions & deletions (indel)
Example
X: applicable
Y: plausibly
Mismatch
Match
Match
Indel
Indel
Indel
The values depends on
applications. It can be
described using a so-called
substitution matrix, to be
discussed shortly.
Indel
Match
Indel
Match
Indel
Match
Indel
X’: applica---ble
Y’: -p-l--ausibly
Matches (+1)
Three cases: Mismatches (1)
Insertions & deletions (indel) (1)
Example
X: applicable
Y: plausibly
Mismatch
Match
Match
Indel
Indel
Indel
Indel
Match
Indel
Match
Indel
Match
Indel
X’: applica---ble
Score = 5(+1) + 1(1) + 7 (1) = 3
Y’: -p-l--ausibly
Matches (+1)
Three cases: Mismatches (1)
Insertions & deletions (indel) (1)
Example
X: applicable
Y: plausibly
Mismatch
Match
Match
Indel
Indel
Indel
Indel
Match
Indel
Match
Indel
Match
Indel
X’: applica---ble
Score = 5(+1) + 1(1) + 7 (1) = 3
Y’: -p-l--ausibly
Matches (+1)
Three cases: Mismatches (1)
Insertions & deletions (indel) (1)
Sequence Alignment

In bioinformatics, a sequence alignment is a way
of arranging the primary sequences of DNA, RNA,
or protein to identify regions of similarity that
may be a consequence of functional, structural, or
evolutionary relationships between the sequences.
Global and Local Alignments
L G P S S K Q T G K G S - S R I W D N
Global alignment
L N - I T K S A G K G A I M R L G D A
- - - - - - - T G K G - - - - - - - -
Local alignment
- - - - - - - A G K G - - - - - - - -
Global and Local Alignments
L G P S S K Q T G K G S - S R I W D N
Global alignment
L N - I T K S A G K G A I M R L G D A
- - - - - - - T G K G - - - - - - - -
Local alignment
- - - - - - - A G K G - - - - - - - -
Global and Local Alignments

Global Alignment
–
–
–

attempts to align the entire sequence
most useful when the sequences in the query set
are similar and of roughly equal size.
Needleman–Wunsch algorithm (1971).
Local Alignment
–
–
Attempts to align partial regions of sequences
with high level of similarity.
Smith-Waterman algorithm (1981)
Needleman–Wunsch Algorithm



Find the best global alignment of any two sequences
under a given substitution matrix.
Maximize a similarity score, to give maximum match
Maximum match = largest number of residues of one
sequence that can be matched with another allowing
for all possible gaps

Based on dynamic programming

Involves an iterative matrix method of calculation
Substitution Matrix


In bioinformatics, a substitution matrix estimates
the rate at which each possible residue in a
sequence changes to each other residue over time.
Substitution matrices are usually seen in the
context of amino acid or DNA sequence alignment,
where the similarity between sequences depends
on the mutation rates as represented in the
matrix.
Substitution Matrix (DNA) w/o Gap Cost
A
C
G
T
A 2 1 1
C 1 2 1
G 1 1 2
T 1 1 1
1
1
1
2
Substitution Matrix (DNA) w/ Gap Cost
T

A 2 1 1 1
C 1 2 1 1
G 1 1 2 1
T 1 1 1 2
 2 2 2 2
2
2
2
2
0
A
C
G
Substitution Matrix (3D-BLAST)
DP  Define Subproblem


Consider two strings, s of length n and t of
length m. Let S be the substitution matrix.
Subproblem: Let Pij is defined to be the
optimal aligning for the two substrings:
t[1..i] and s[1..j],
and let Mij be the matching score.

Original Problem: Pmn (matching score Mmn)
DP  Principle of Optimality
 M i 1, j 1  Sti , s j
 M i 1, j 1  Sti , s j


M ij  max  M i , j 1  gap _ score  max  M i , j 1  S  , s j

M

gap
_
score
 M i 1, j  Sti ,
 i 1, j
j1 j
i1
i
 
 ?
T

A 2 1 1 1
C 1 2 1 1
G 1 1 2 1
T 1 1 1 2
 2 2 2 2
2
2
2
2
0
A
Example
s : ACGGTAG
t : CCTAAG
C
G
Step 1. Create a scoring matrix
Mij  A C G G T A G

C
C
T
A
A
G
Step 2. Make an empty table for Mij
Step 3. Initialize base conditions
Step 4. Fill table by
 M i 1, j 1  Sti , s j

M ij  max  M i , j 1  S, s j

 M i 1, j  Sti ,
Step 5. Trace back
T

A 2 1 1 1
C 1 2 1 1
G 1 1 2 1
T 1 1 1 2
 2 2 2 2
2
2
2
2
0
A
Example
s : ACGGTAG
t : CCTAAG
C
G
Step 1. Create a scoring matrix
Mij  A C G G T A G

0 2 4 6 8 10 12 14
C 2
C 4
T 6
A 8
A 10
G 12
Step 2. Make an empty table for Mij
Step 3. Initialize base conditions
Step 4. Fill table by
 M i 1, j 1  Sti , s j

M ij  max  M i , j 1  S, s j

 M i 1, j  Sti ,
Step 5. Trace back
T

A 2 1 1 1
C 1 2 1 1
G 1 1 2 1
T 1 1 1 2
 2 2 2 2
2
2
2
2
0
A
s : ACGGTAG
t : CCTAAG
Example
C
G
Step 1. Create a scoring matrix
Mij  A C G G T A G

0 2 4 6 8 10 12 14
C 2 1 0 2 4 6 8 10
C 4 3 1 1 3 3 5 7
T 6 5 1 0 2 1 3 5
A 8 4 3 0
1 1 1
1
A 10 6 5 2 1
0
1
2
G 12 8 7 3 0
0
1
3
Step 2. Make an empty table for Mij
Step 3. Initialize base conditions
Step 4. Fill table by
 M i 1, j 1  Sti , s j

M ij  max  M i , j 1  S, s j

 M i 1, j  Sti ,
Step 5. Trace back
s : ACGGTAG s’ : A C G G T A G
t : CCTAAG t’ : C C T A - A G
Example
Step 1. Create a scoring matrix
Mij  A C G G T A G

0 2 4 6 8 10 12 14
C 2 1 0 2 4 6 8 10
C 4 3 1 1 3 3 5 7
T 6 5 1 0 2 1 3 5
A 8 4 3 0
1 1 1
1
A 10 6 5 2 1
0
1
2
G 12 8 7 3 0
0
1
3
Step 2. Make an empty table for Mij
Step 3. Initialize base conditions
Step 4. Fill table by
 M i 1, j 1  Sti , s j

M ij  max  M i , j 1  S, s j

 M i 1, j  Sti ,
Step 5. Trace back
Needleman–Wunsch Algorithm
Step 1. Create a scoring matrix
Step 2. Make an empty table for Mij
for i = 2 to m+1 do
for j from 2 to n +1 do
 M i 1, j 1  Sti , s j

M ij  max  M i , j 1  S, s j

 M i 1, j  Sti ,
Step 3. Initialize base conditions
Step 4. Fill table by
 M i 1, j 1  Sti , s j

M ij  max  M i , j 1  S, s j

 M i 1, j  Sti ,
Step 5. Trace back
Needleman–Wunsch Algorithm
s’  ””, t’  ””
while i < 1 and j < 1 do
if M ij  M i 1, j 1  St ,s
i j
s’ sj+ s’
t’ ti+ t’
else if M ij  M i , j 1  S,s
j
s’ sj+ s’
t’ gap+ t’
else
s’ gap+ s’
t’ ti+ t’
while i > 1 do t’ gap+ t’
while j > 1 do s’ gap+ s’
Step 1. Create a scoring matrix
Step 2. Make an empty table for Mij
Step 3. Initialize base conditions
Step 4. Fill table by
 M i 1, j 1  Sti , s j

M ij  max  M i , j 1  S, s j

 M i 1, j  Sti ,
Step 5. Trace back
Local Alignment Problem

Given two strings
s = s1……sn,
t = t1…….tm
Find substrings s’, t’ whose similarity
(optimal global alignment value) is maximum.
Example: Local Alignment
GTAGT CATCAT ATG TGACTGAC G
TC CATDOGCAT CC TGACTGAC A
GTAGT CATCAT
difference
block
TC CATDOGCAT
Best aligned subsequeces
ATGCC TGACTGAC G
difference
block
CCTACTAC
difference
block
difference
block
TGACTGAC A
Recursive Formulation

Global Alignment (Needleman–Wunsch Algorithm)
 M i 1, j 1  Sti , s j
 M i 1, j 1  S si ,t j


M ij  max  M i , j 1  gap _ score  max  M i , j 1  S  , s j

M

gap
_
score
 M i 1, j  Sti ,
 i 1, j

Local Alignment (Smith-Waterman Algorithm)
 M i 1, j 1  Sti , s j
 M i 1, j 1  S si ,t j


 M i , j 1  S  , s j
 M i , j 1  gap _ score
 max 
M ij  max 
 M i 1, j  Sti ,
 M i 1, j  gap _ score

0

0
Exercises
3.
Find the best local aligned
substrings for the following
two DNA strings:
GAATTCAGTTA
GGATCGA
You have to give the detail.
Hint: start from the left
table.
Exercises
4.
What is longest common sequence (LCS)
problem? How to solve LCS using
dynamic programming technique?
Lecture 3: Dynamic Programming
Knapsack Problem
Knapsack Problems



Given some items, pack the knapsack to get the maximum
total value. Each item has some weight and some benefit.
Total weight that we can carry is no more than some fixed
capacity.
Fractional knapsack problem
–
Items are divisible: you can take any fraction of an item.
–
Solved with a greedy algorithm.
0-1 knapsack problem
–
Items are indivisible; you either take an item or not.
–
Solved with dynamic programming.
0-1 Knapsack Problem



Given a knapsack with maximum capacity W,
and a set S consisting of n items
Each item i has some weight wi and benefit
value bi (all wi and W are integer values)
Problem: How to pack the knapsack to
achieve maximum total value of packed
items?
Example: 0-1 Knapsack Problem
Which boxes should be chosen
to maximize the amount of
money while still keeping the
overall weight under 15 kg ?
Example: 0-1 Knapsack Problem
x1 {0,1}

x4 {0,1}
Maximize

x2 {0,1}
4 x1  2 x2  10 x3  2 x4  1x5
Unknowns or Variables
xi 's, i  1,

x5 {0,1}
x3 {0,1}
Objective Function
,5
Constraints
Subject to
12 x1  1x2  4 x3  2 x4  1x5  15
xi 0,1 , i  1,
,5
Formulation: 0-1 Knapsack Problem
n
Maximize
b x
i 1
i i
n
Subject to
w x
i 1
i i
W
xi  {0,1}
0-1 Knapsack Problem:
Brute-Force Approach

Since there are n items, there are 2n
possible combinations of items.

We go through all combinations and find
the one with maximum value and with total
weight less or equal to W

Running time will be O(2n)
DP  Define Subproblem




Suppose that items are labeled 1,..., n.
Define a subproblem, say, Pk as to finding an
optimal solution for items in Sk = {1, 2,..., k}.
 original problem is Pn.
Is such a scheme workable?
Is the principle of optimality held?
A Counterexample
1. 2kgs, 3$
P1
P2
2. 3kgs, 4$
P3
P4
3. 4kgs, 5$
4. 5kgs, 8$
20 kgs
5. 9kgs, 10$
P5
A Counterexample
1. 2kgs, 3$
Subproblem
Optimum
Value
2. 3kgs, 4$
P1
1
3$
P2
1, 2
7$
P3
1, 2, 3
12$
4. 5kgs, 8$
P4
1, 2, 3, 4
20$
5. 9kgs, 10$
P5
1, 3, 4, 5
26$
3. 4kgs, 5$
20 kgs
Solution for P4 is not part of
the solution for P5 !!!
A Counterexample
1. 2kgs, 3$
Subproblem
Optimum
Value
2. 3kgs, 4$
P1
1
3$
P2
1, 2
7$
P3
1, 2, 3
12$
4. 5kgs, 8$
P4
1, 2, 3, 4
20$
5. 9kgs, 10$
P5
1, 3, 4, 5
26$
3. 4kgs, 5$
20 kgs
DP  Define Subproblem




Suppose that items are labeled 1,..., n.
Define a subproblem, say, Pk as to finding an
optimal solution for items in Sk = {1, 2,..., k}.
 original problem is Pn.

Is such a scheme workable?
Is the principle of optimality held?

New version
DP  Define Subproblem




Suppose that items are labeled 1,..., n.
Define a subproblem, say, Pk,w as to finding
an optimal solution for items in Sk = {1, 2,..., k}
and with total weight no more than w.
 original problem is Pn,W.
Is such a scheme workable?
Is the principle of optimality held?
DP  Principle of Optimality
Denote the benefit for the optimal solution
of Pk,w as Bk,w.
 Bk , w

 Bk 1, w

max Bk 1, w , Bk 1, w wk  bk




if wk  w
if wk  w
DP  Principle of Optimality
In this case, it is
th
impossible
to
include
the
k
Denote the benefit for the optimal solution
object.
of P as B .
k,w
 Bk , w
k,w

 Bk 1, w

max Bk 1, w , Bk 1, w wk  bk



Not include
include
the kth object the kth object

if wk  w
if wk  w
There are two
possible
choices.
Bk , w
Example

 Bk 1, w

max Bk 1, w , Bk 1, w wk  bk



w
1. 2kgs, 3$
0
2. 3kgs, 4$
1
3. 4kgs, 5$
5 kgs
k
4. 5kgs, 6$
2
3
4
5
0
1
2

3
if wk  w
if wk  w
4
Bk , w
Example

 Bk 1, w

max Bk 1, w , Bk 1, w wk  bk



5 kgs
k
1. 2kgs, 3$
0
0
0
2. 3kgs, 4$
1
0
2
0
3
0
4
0
5
0
4. 5kgs, 6$
if wk  w
Step 1. Setup table and initialize
base conditions.
w
3. 4kgs, 5$

if wk  w
1
0
2
0
3
0
4
0
Bk , w
Example

 Bk 1, w

max Bk 1, w , Bk 1, w wk  bk



5 kgs
k
1. 2kgs, 3$
0
0
0
2. 3kgs, 4$
1
0
0
2
0
3
3
0
3
4
0
3
5
0
3
4. 5kgs, 6$
if wk  w
Step 2. Fill all table entries
progressively.
w
3. 4kgs, 5$

if wk  w
1
0
2
0
3
0
4
0
Bk , w
Example

 Bk 1, w

max Bk 1, w , Bk 1, w wk  bk



5 kgs
k
1. 2kgs, 3$
0
0
0
2. 3kgs, 4$
1
0
0
0
2
0
3
3
3
0
3
4
4
0
3
4
5
0
3
7
4. 5kgs, 6$
if wk  w
Step 2. Fill all table entries
progressively.
w
3. 4kgs, 5$

if wk  w
1
0
2
0
3
0
4
0
Bk , w
Example

 Bk 1, w

max Bk 1, w , Bk 1, w wk  bk



k
1. 2kgs, 3$
0
0
0
2. 3kgs, 4$
1
0
0
0
0
2
0
3
3
3
3
0
3
4
4
4
0
3
4
5
5
0
3
7
7
3. 4kgs, 5$
4. 5kgs, 6$
if wk  w
Step 2. Fill all table entries
progressively.
w
5 kgs

if wk  w
1
0
2
0
3
0
4
0
Bk , w
Example

 Bk 1, w

max Bk 1, w , Bk 1, w wk  bk



if wk  w
Step 2. Fill all table entries
progressively.
k
1. 2kgs, 3$
0
0
0
2. 3kgs, 4$
1
0
0
0
0
0
2
0
3
3
3
3
3
0
3
4
4
4
4
0
3
4
5
5
5
0
3
7
7
7
w
3. 4kgs, 5$
5 kgs

if wk  w
4. 5kgs, 6$
1
0
2
0
3
0
4
0
Bk , w
Example

 Bk 1, w

max Bk 1, w , Bk 1, w wk  bk



if wk  w
Step 3. Trace back
k
1. 2kgs, 3$
0
0
0
2. 3kgs, 4$
1
0
0
0
0
0
2
0
3
3
3
3
3
0
3
4
4
4
4
0
3
4
5
5
5
0
3
7
7
7
w
3. 4kgs, 5$
5 kgs

if wk  w
4. 5kgs, 6$
1
0
2
0
3
0
4
0
Pseudo-Polynomial Time Algorithm



The time complexity for 0-1 knapsack
using DP is O(Wn).
Not a polynomial-time algorithm if W is
large.
This is a pseudo-polynomial time algorithm.
Lecture 3: Dynamic Programming
All-Pairs
Shortest Path Problem
All-Pairs Shortest Path Problem

Given weighted graph G(V,E), we want
to determine the cost dij of the
shortest path between each pair of
nodes in V.
Floyd's Algorithm

Let dijk be the minimum cost of a path from node i
to node j, using only nodes in Vk={v1,…,vk}.
k 1
k 1
k 1

min
d
,
d

d
k 0


ij
ik
kj 
k
dij  
k 0

cij
The all-pairs shortest path problem
is to find all paths with costs
dij  dijn i  j
i
k
dikk 1
Vk1
dijk 1
Vk
dkjk 1
j
Floyd's Algorithm
Input Parameter: D
Output Parameter: D, next
all_paths(D, next)
n = D.NumberOfRows;
// initialize next: if no intermediate
// vertices are allowed next[i][j] = j
for i = 1 to n
for j = 1 to n
next[i][j] = j;
for k = 1 to n // compute D(k)
for i = 1 to n
for j = 1 to n
if (D[i][k] + D[k][j] < D[i][j])
D[i][j] = D[i][k] + D[k][j];
next[i][j] = next[i][k];
k 1
k 1
k 1

min
d
,
d

d
k 0



ij
ik
kj
k
dij  
k 0

cij
3
O(n )
Floyd's Algorithm
Input Parameters: next, i, j
Output Parameters: None
print_path(next, i, j) {
// if no intermediate vertices, just
// print i and j and return
if (j == next[i][j]) {
print(i + “ ” + j);
return;
}
// output i and then the path from the vertex
// after i (next[i][j]) to j
print(i + “ ”);
print_path(next,next[i][j], j);
}
Lecture 3: Dynamic Programming
Traveling Salesman
Problem
Traveling Salesman Problem (TSP)
Traveling Salesman Problem (TSP)
How many feasible paths?
n cities
n ! (n  1)!

2n
2
Example (TSP)
(1234) = 18
1
6
3
2
2
7
4
5
(1243) = 19
(1324) = 23
6
4
(1342) = 19
(1423) = 23
(1432) = 18
Subproblem Formulation for TSP
g(i, S)
length of the shortest path from i to 1
visiting each city in S exactly once.
g(1, V  {1})
1
length of the
optimal TSP tour.
i
S
Goal: g(1, V  {1})
Subproblem Formulation for TSP
g(i, S)
length of the shortest path from i to 1
visiting each city in S exactly once.
1
S{j}S
g (i, S )  min dij  g ( j , S  { j})
jS
i
dij
j
Goal: g(1, V  {1})
Example
11
6
7
33
D   dij 
5
jS
22
2
4
g (i, S )  min dij  g ( j , S  { j})
g (1,{2,3, 4})
d12
6
44
g (2,{3, 4})
d23
44
 2 6 4 
2  7 6


6 7  5


4
6
5



d24
g (3,{4}) g (4,{3})
d34
g (4, )
d43
g (3, )
d13
d14
g (3,{2, 4})
d32
d34
g (2,{4}) g (4,{2})
d24
g (4, )
d42
g (2, )
g (4,{2,3})
d42
d43
g (2,{3}) g (3,{2})
d23
d32
g (3, )
g (2, )
Goal: g(1, V  {1})
Example
11
6
7
33
D   dij 
5
jS
22
2
4
g (i, S )  min dij  g ( j , S  { j})
g (1,{2,3, 4}) 18
2 d12
6
44
44
 2 6 4 
2  7 6


6 7  5


4
6
5



9
d13 6
d14 4
16
g (2,{3, 4})
13
g (3,{2, 4})
14
g (4,{2,3})
d23 7
d32 7
d42 6
d24 6
d34 5
8
10
11
g (3,{4}) g (4,{3}) g (2,{4}) g (4,{2})
d435
9
13
g (2,{3}) g (3,{2})
d34 5
d43 5
d24 6
d42 6
d23 7
d32 7
g (4, )
4
g (3, )
6
g (4, )
4
g (2, )
2
g (3, )
6
g (2, )
2
Goal: g(1, V  {1})
g (i, S )  min dij  g ( j , S  { j})
jS
DP  TSP Algorithm
Input Parameter: D
Output Parameter: P // path
TSP(D)
n = Dim(D);
for i = 1 to n
g[i, ] = D[i, 1];
for k = 1 to n2 // compute g for subproblems
for all S  V{1} with |S|=k
for all i  S  {1}
g[i, S] = minjS{D[i, j], g[j, S  {j}] };
P[i, S] = arg minjS{D[i, j], g[j, S  {j}] };
// compute the TSP tour
g[1, V{1}] = minjV{1}{D[1, j], g[j, V  {1, j}]};
P[1, V{1}] = arg minjV{1}{D[1, j], g[j, V  {1, j}]};
Goal: g(1, V  {1})
g (i, S )  min dij  g ( j , S  { j})
jS
DP  TSP Algorithm
Input Parameter: D
Output Parameter: P // path
TSP(D)
n = Dim(D);
for i = 1 to n
g[i, ] = D[i, 1];
for k = 1 to n2 // compute g for subproblems
for all S  V{1} with |S|=k
for all i  S  {1}
g[i, S] = minjS{D[i, j], g[j, S  {j}] };
P[i, S] = arg minjS{D[i, j], g[j, S  {j}] };
// compute the TSP tour
g[1, V{1}] = minjV{1}{D[1, j], g[j, V  {1, j}]};
P[1, V{1}] = arg minjV{1}{D[1, j], g[j, V  {1, j}]};
n
O(2 )