104
Lecture 2: Dynamic Programming 主主主 : 主主主

Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

Embed Size (px)

DESCRIPTION

Lecture 2: Dynamic Programming What is Dynamic Programming?

Citation preview

Page 1: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

Lecture 2: Dynamic Programming

主講人 :虞台文

Page 2: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest Path Problem Traveling Salesman Problem

Conclusion

Page 3: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

Lecture 2: Dynamic Programming

What is Dynamic Programming?

Page 4: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

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.

Page 5: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

Properties for Problems Solved by DP

Simple Subproblems– The original problem can be broken into smaller subproblems with the same structure

Optimal Substructure of the problems– 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

Page 6: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

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.

Page 7: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

Example: Shortest Path Problem

10

5

3

GoalStart

Page 8: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

25

28

40

Example: Shortest Path Problem

10

5

3

Start Goal

Page 9: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

25

28

40

Example: Shortest Path Problem

10

5

3

Start Goal

Page 10: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

Recall Greedy Method forShortest Paths on a Multi-stage Graph

Problem– Find a shortest path from v0 to v3

Is the greedy solution optimal?

Page 11: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

Recall Greedy Method forShortest Paths on a Multi-stage Graph

Problem– Find a shortest path from v0 to v3

Is the greedy solution optimal?

The optimal path

Page 12: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

Example Dynamic Programming

min 1,1 3

min 1,2 3min 0 3

min 1,3 3

min 1,4 3

( , )( , )

31

min57

( , )( , )( , )

d v vd v v

d v vd v vd v v

Page 13: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

Lecture 2: Dynamic Programming

Matrix Chain-Products

Page 14: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

Matrix MultiplicationC = A × BA is d × e and B is e × f

O(def )

e

Bf

ej

Cd

f

i,jA

d i

1

e

ij ik kjk

C A B

Page 15: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

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

Page 16: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

Matrix Chain-Products Given a sequence of matrices, A1, A2, …, An, find the m

ost efficient way to multiply them together. A Brute-force Approach:

– Try all possible ways to parenthesize A=A1A2…An

– Calculate number of operations for each one– Pick the best one

Time Complexity:– #paranethesizations = #binary trees of n nodes– O(4n)

Page 17: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

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

Page 18: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

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

Page 19: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

DP Define Subproblem

OriginalProblem 1 i j nA A A A 1 i j nA AA A

Subproblem (P ij, i j)(P1n)

Suppose #operations for the optimal solution of Pij is Nij #operations for the optimal solution of the original problem P1n is N1n

1:i i iA d d

Page 20: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

DP Define Subproblem

OriginalProblem 1 i j nA A A A 1 i j nA AA A

Subproblem (P ij, i j)(P1n)

Suppose #operations for the optimal solution of Pij is Nij #operations for the optimal solution of the original problem P1n is N1n

1:i i iA d d

Page 21: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

DP Define Subproblem

OriginalProblem 1 i j nA A A A 1 i j nA AA A

Subproblem (P ij, i j)(P1n)

Suppose #operations for the optimal solution of Pij is Nij #operations for the optimal solution of the original problem P1n is N1n

1:i i iA d d

What is the relation btw Nij (Pij) and N1n (P1

n)?

Page 22: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

DP Principle of Optimality

1i k k jA A A A

1:i i iA d d

N ik N k+1,n

d i d k+1 d k d j+1

1 1 1,min ik ki jj i k j i k jd dNN N d 11, 1minij ik k i k ji k j j dN N N d d

Page 23: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

DP Implementation 11, 1minij ik k i k ji k j j dN N N d d

1

2

i

n

1 2 j nNij

Page 24: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

DP Implementation 11, 1minij ik k i k ji k j j dN N N d d

1

2

i

n

1 2 j nNij

Page 25: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

DP Implementation

1

2

i

n

1 2 j nNij

?

11, 1minij ik k i k ji k j j dN N N d d

Page 26: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

DP Implementation

1

2

i

n

1 2 j nNij

?

11, 1minij ik k i k ji k j j dN N N d d

Page 27: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

DP Implementation

1

2

i

n

1 2 j nNij

?

11, 1minij ik k i k ji k j j dN N N d d

Page 28: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

DP Implementation

1

2

i

n

1 2 j nNij

11, 1minij ik k i k ji k j j dN N N d d

Page 29: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

DP Implementation

1

2

i

n

1 2 j nNij

?

11, 1minij ik k i k ji k j j dN N N d d

Page 30: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

DP Implementation

1

2

i

n

1 2 j nNij

?

11, 1minij ik k i k ji k j j dN N N d d

Page 31: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

DP Implementation

1

2

i

n

1 2 j nNij

11, 1minij ik k i k ji k j j dN N N d d

Page 32: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

DP Implementation

1

2

i

n

1 2 j nNij

?

11, 1minij ik k i k ji k j j dN N N d d

Page 33: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

DP Implementation

1

2

i

n

1 2 j nNij

?

11, 1minij ik k i k ji k j j dN N N d d

Page 34: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

DP Implementation

1

2

i

n

1 2 j nNij

11, 1minij ik k i k ji k j j dN N N d d

Page 35: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

DP for Matrix Chain-Products

Algorithm matrixChain(S):Input: sequence S of n matrices to be multipliedOutput: number of operations in an optimal parenthesization of Sfor i 1 to n // main diagonal terms are all zero

Ni,i 0

for d 2 to n // each diagonal do followingfor i 1 to nd+1 // do from top to bottom for each diagona

lj i+d1

Ni,j infinity

for k i to j1 // counting minimum Ni,j min(Ni,j, Ni,k +Nk+1,j +di dk+1 dj+1)

11, 1minij ik k i k ji k j j dN N N d d

Page 36: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

Time ComplexityAlgorithm matrixChain(S):Input: sequence S of n matrices to be multipliedOutput: number of operations in an optimal parenthesization of Sfor i 1 to n // main diagonal terms are all zero

Ni,i 0

for d 2 to n // each diagonal do followingfor i 1 to nd+1 // do from top to bottom for each diagona

lj i+d1

Ni,j infinity

for k i to j1 // counting minimum Ni,j min(Ni,j, Ni,k +Nk+1,j +di dk+1 dj+1)

O(n3)

11, 1minij ik k i k ji k j j dN N N d d

Page 37: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

Exercises 11,min i kij ik ji k j k jN N N d d d

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.

Page 38: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

Lecture 2: Dynamic Programming

Sequence Alignment

Page 39: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

QuestionGiven two strings

are they similar?what is their distance?

1 2, , , mX x x x 1 2, , , nY y y y and

Page 40: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

Example

Y:applicableplausibly

X:

How similar they are?Can you give them a score?

Page 41: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

Example

Y’:

applica---ble

-p-l--ausibly

X’:Y:applicableplausibly

X:

Mat

ch

Mat

ch

Mat

ch

Mat

ch

Mat

ch

Mis

mat

ch

Inde

l

Inde

l

Inde

l

Inde

l

Inde

l

Inde

l

Inde

l

Three cases:MatchesMismatchesInsertions & deletions (indel)

Page 42: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

Example

Y’:

applica---ble

-p-l--ausibly

X’:Y:applicableplausibly

X:

Mat

ch

Mat

ch

Mat

ch

Mat

ch

Mat

ch

Mis

mat

ch

Inde

l

Inde

l

Inde

l

Inde

l

Inde

l

Inde

l

Inde

l

Three cases:MatchesMismatchesInsertions & deletions (indel)

(+1)(1)

(1)

The values depends on applications. It can be

described using a so-called substitution matrix, to be

discussed shortly.

Page 43: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

Example

Y’:

applica---ble

-p-l--ausibly

X’:Y:applicableplausibly

X:

Mat

ch

Mat

ch

Mat

ch

Mat

ch

Mat

ch

Mis

mat

ch

Inde

l

Inde

l

Inde

l

Inde

l

Inde

l

Inde

l

Inde

l

Three cases:MatchesMismatchesInsertions & deletions (indel)

(+1)(1)

(1)

Score = 5(+1) + 1(1) + 7 (1) = 3

Page 44: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

Example

Y’:

applica---ble

-p-l--ausibly

X’:Y:applicableplausibly

X:

Mat

ch

Mat

ch

Mat

ch

Mat

ch

Mat

ch

Mis

mat

ch

Inde

l

Inde

l

Inde

l

Inde

l

Inde

l

Inde

l

Inde

l

Three cases:MatchesMismatchesInsertions & deletions (indel)

(+1)(1)

(1)

Score = 5(+1) + 1(1) + 7 (1) = 3 Is the alignment optimal?

Page 45: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

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.

Page 46: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

Global and Local Alignments

L G P S S K Q T G K G S - S R I W D N

Global alignmentL 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 - - - - - - - -

Page 47: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

Global and Local Alignments

L G P S S K Q T G K G S - S R I W D N

Global alignmentL 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 - - - - - - - -

Page 48: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

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)

Page 49: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

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

Page 50: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

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.

Page 51: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

Substitution Matrix (DNA) w/o Gap Cost

A C G TA 2 1 1 1C 1 2 1 1G 1 1 2 1T 1 1 1 2

Page 52: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

Substitution Matrix (DNA) w/ Gap Cost

A C G TA 2 1 1 1C 1 2 1 1G 1 1 2 1T 1 1 1 2

A C G T

A 2 1 1 1 2C 1 2 1 1 2G 1 1 2 1 2T 1 1 1 2 2 2 2 2 2 0

Page 53: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

Substitution Matrix (3D-BLAST)

Page 54: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

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)

Page 55: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

DP Principle of Optimality

1, 1

, 1

1,

,

max __

i ji j

ij i j

i

t s

j

S

g

M

M MM

ap scoregap score

1, 1

, 1

1,

,

,

,

maxi j

j

i

i j

i j

i

t s

s

j t

S

S

S

M

M

M

jj1

ii1

?

Page 56: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

,1, 1

, 1

1,

,

,

maxi j

j

i

i j

ij i j

i j

t s

s

t

S

S

S

M

M M

M

ExampleStep 1. Create a scoring matrixStep 2. Make an empty table for Mij

Step 3. Initialize base conditionsStep 4. Fill table by

Step 5. Trace back

s :t :ACGGTAGCCTAAG

Mij A C G G T A G

CCTAAG

22 11 1

T

2 11 1T0 2 2 2

22 11G 2 12 1C 21 12AGCA

22 11 1

T

2 11 1T0 2 2 2

22 11G 2 12 1C 21 12AGCA

Page 57: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

,1, 1

, 1

1,

,

,

maxi j

j

i

i j

ij i j

i j

t s

s

t

S

S

S

M

M M

M

ExampleStep 1. Create a scoring matrixStep 2. Make an empty table for Mij

Step 3. Initialize base conditionsStep 4. Fill table by

Step 5. Trace back

s :t :ACGGTAGCCTAAG

Mij A C G G T A G

CCTAAG

22 11 1

T

2 11 1T0 2 2 2

22 11G 2 12 1C 21 12AGCA

22 11 1

T

2 11 1T0 2 2 2

22 11G 2 12 1C 21 12AGCA

0

2

4

6

8

10

12

2 4 6 8 10 12 14

Page 58: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

ExampleStep 1. Create a scoring matrixStep 2. Make an empty table for Mij

Step 3. Initialize base conditionsStep 4. Fill table by

Step 5. Trace back

s :t :ACGGTAGCCTAAG

Mij A C G G T A G

CCTAAG

22 11 1

T

2 11 1T0 2 2 2

22 11G 2 12 1C 21 12AGCA

22 11 1

T

2 11 1T0 2 2 2

22 11G 2 12 1C 21 12AGCA

,1, 1

, 1

1,

,

,

maxi j

j

i

i j

ij i j

i j

t s

s

t

S

S

S

M

M M

M

0 2 4 6 8 10 12 14

2

4

6

8

10

12

1 0 2 4 6 8 10

3 1 1 3 3 5 7

5 1 0 2 1 3 5

4 3 0 1 1 1 1

6 5 2 1 0 1 2

8 7 3 0 0 1 3

Page 59: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

ExampleStep 1. Create a scoring matrixStep 2. Make an empty table for Mij

Step 3. Initialize base conditionsStep 4. Fill table by

Step 5. Trace back

s :t :ACGGTAGCCTAAG

Mij A C G G T A G

CCTAAG

,1, 1

, 1

1,

,

,

maxi j

j

i

i j

ij i j

i j

t s

s

t

S

S

S

M

M M

M

0

2

4

6

8

10

12

2 4 6 8 10 12 14

1

3

5

4

6

8

0

1

1

3

5

7

2

1

0

0

2

3

4

3

2

1

1

0

6

3

1

1

0

0

8

5

3

1

1

1

10

7

5

1

2

33

1

1

0

1

1

0

1

s’ :t’ :

GG

AA

T-

GA

GT

CC

AC

Page 60: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

Needleman–Wunsch Algorithm

Step 1. Create a scoring matrixStep 2. Make an empty table for Mij

Step 3. Initialize base conditionsStep 4. Fill table by

Step 5. Trace back

,1, 1

, 1

1,

,

,

maxi j

j

i

i j

ij i j

i j

t s

s

t

S

S

S

M

M M

M

for i = 2 to m+1 do for j from 2 to n +1 do

,1, 1

, 1

1,

,

,

maxi j

j

i

i j

ij i j

i j

t s

s

t

S

S

S

M

M M

M

Page 61: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

s’ ””, t’ ”” while i < 1 and j < 1 do if

s’ sj+ s’ t’ ti+ t’ else if

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’

Needleman–Wunsch Algorithm

Step 1. Create a scoring matrixStep 2. Make an empty table for Mij

Step 3. Initialize base conditionsStep 4. Fill table by

Step 5. Trace back

,1, 1

, 1

1,

,

,

maxi j

j

i

i j

ij i j

i j

t s

s

t

S

S

S

M

M M

M

,1, 1 i jij i j t sM SM

, ,1 jij i j sM M S

Page 62: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

Local Alignment Problem

Given two strings s = s1……sn,

t = t1…….tmFind substrings s’, t’ whose similarity (optimal global alignment value) is maximum.

Page 63: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

Example: Local AlignmentGTAGT CATCAT ATG TGACTGAC G

TC CATDOGCAT CC TGACTGAC A

GTAGT CATCAT ATGCC TGACTGAC G

TC CATDOGCAT CCTACTAC TGACTGAC A

difference block

difference block difference

block

difference block

Best aligned subsequeces

Page 64: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

Global Alignment (Needleman–Wunsch Algorithm)

Local Alignment (Smith-Waterman Algorithm)

Recursive Formulation

1, 1

, 1

1,

,

max __

i ji j

ij i j

i

s t

j

S

g

M

M MM

ap scoregap score

1, 1

, 1

1,

,

,

,

maxi j

j

i

i j

i j

i

t s

s

j t

S

S

S

M

M

M

1, 1

, 1

1,

,

0

_max_

i jsi j

i jij

i j

tS

gap scoregap sco

M

MMeM r

1, 1

,

,

,

,

1

1,

ma

0

x

i j

j

i

i j

i

t s

sj

i j t

S

S

M

M

M S

Page 65: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

Exercises3. 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.

Page 66: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

Exercises

4. What is longest common sequence (LCS) problem? How to solve LCS using dynamic programming technique?

Page 67: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

Lecture 2: Dynamic Programming

Knapsack Problem

Page 68: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

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.

Page 69: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

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?

0-1 Knapsack Problem

Why it is called a 0-1 Knapsack Problem?

Page 70: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

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 ?

Page 71: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

Example: 0-1 Knapsack Problem

1 {0,1}x

2 {0,1}x

3 {0,1}x

5 {0,1}x

4 {0,1}x

Objective Function

Unknowns or Variables

Constraints's, 1, ,5ix i

, ,0,1 1, 5ix i

1 2 3 4 54 2 10 2 1Maximize

x x x x x

1 2 3 4 512 1Subject t

154 2o

1x x x x x

Page 72: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

Formulation: 0-1 Knapsack Problem

1

1

Maximize

Subject t

{0,1}

o

n

i ii

n

i ii

i

b x

w x W

x

Page 73: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

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)

Page 74: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

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?

Page 75: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

A Counterexample

1. 2kgs, 3$

2. 3kgs, 4$

3. 4kgs, 5$

4. 5kgs, 8$

5. 9kgs, 10$20 kgs

P1

P2 P3

P4

P5

Page 76: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

A Counterexample

Sub-problem

Optimum Value

P1 1 3$P2 1, 2 7$P3 1, 2, 3 12$P4 1, 2, 3, 4 20$P5 1, 3, 4, 5 26$

1. 2kgs, 3$

2. 3kgs, 4$

3. 4kgs, 5$

4. 5kgs, 8$

5. 9kgs, 10$20 kgs

Page 77: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

A Counterexample

Sub-problem

Optimum Value

P1 1 3$P2 1, 2 7$P3 1, 2, 3 12$P4 1, 2, 3, 4 20$P5 1, 3, 4, 5 26$

1. 2kgs, 3$

2. 3kgs, 4$

3. 4kgs, 5$

4. 5kgs, 8$

5. 9kgs, 10$20 kgs

Solution for P4 is not part of the solution for P5 !!!

Page 78: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

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?

Page 79: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

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?

New version

Page 80: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

DP Principle of Optimality

Denote the benefit for the optimal solution of Pk,w as Bk,w.

1,

,1, 1,

if

max , if k

k w k

k wk w k w w k k

B w wB

B B b w w

Page 81: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

DP Principle of Optimality

Denote the benefit for the optimal solution of Pk,w as Bk,w.

1,

,1, 1,

if

max , if k

k w k

k wk w k w w k k

B w wB

B B b w w

In this case, it is impossible to include the kth object.

Not includethe kth object

includethe kth object There are two

possible choices.

Page 82: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

Example

1. 2kgs, 3$

2. 3kgs, 4$

3. 4kgs, 5$

4. 5kgs, 6$5 kgs

w k 0 1 2 3 4012345

1,

,1, 1,

if

max , if k

k w k

k wk w k w w k k

B w wB

B B b w w

Page 83: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

1,

,1, 1,

if

max , if k

k w k

k wk w k w w k k

B w wB

B B b w w

Example

1. 2kgs, 3$

2. 3kgs, 4$

3. 4kgs, 5$

4. 5kgs, 6$5 kgs

w k 0 1 2 3 4012345

0 0 0 0 000000

Step 1. Setup table and initialize base conditions.

Page 84: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

1,

,1, 1,

if

max , if k

k w k

k wk w k w w k k

B w wB

B B b w w

Example

1. 2kgs, 3$

2. 3kgs, 4$

3. 4kgs, 5$

4. 5kgs, 6$5 kgs

w k 0 1 2 3 4012345

0 0 0 0 000000

03333

Step 2. Fill all table entries progressively.

Page 85: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

1,

,1, 1,

if

max , if k

k w k

k wk w k w w k k

B w wB

B B b w w

Example

1. 2kgs, 3$

2. 3kgs, 4$

3. 4kgs, 5$

4. 5kgs, 6$5 kgs

w k 0 1 2 3 4012345

0 0 0 0 000000

03333

03447

Step 2. Fill all table entries progressively.

Page 86: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

1,

,1, 1,

if

max , if k

k w k

k wk w k w w k k

B w wB

B B b w w

Example

1. 2kgs, 3$

2. 3kgs, 4$

3. 4kgs, 5$

4. 5kgs, 6$5 kgs

w k 0 1 2 3 4012345

0 0 0 0 000000

03333

03447

03457

Step 2. Fill all table entries progressively.

Page 87: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

1,

,1, 1,

if

max , if k

k w k

k wk w k w w k k

B w wB

B B b w w

Example

1. 2kgs, 3$

2. 3kgs, 4$

3. 4kgs, 5$

4. 5kgs, 6$5 kgs

w k 0 1 2 3 4012345

0 0 0 0 000000

03333

03447

03457

03457

Step 2. Fill all table entries progressively.

Page 88: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

1,

,1, 1,

if

max , if k

k w k

k wk w k w w k k

B w wB

B B b w w

Example

1. 2kgs, 3$

2. 3kgs, 4$

3. 4kgs, 5$

4. 5kgs, 6$5 kgs

w k 0 1 2 3 4012345

0 0 0 0 000000

03333

03447

03457

03457

Step 3. Trace back

Page 89: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

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.

Page 90: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

Lecture 2: Dynamic Programming

All-PairsShortest Path Problem

Page 91: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

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.

Page 92: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

Vk

Floyd's Algorithm Let be the minimum cost of a path from node i to node j, using only nodes in Vk={v1,…,vk}.

kijd

k

Vk1

i j1kijd

1kikd

1kkjd

1 1 1min , 0

0

k k kij ik kjk

ijij

d d d kd

c k

The all-pairs shortest path problem is to find all paths with costs

nij ijd d i j

Page 93: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

Floyd's AlgorithmInput Parameter: DOutput Parameter: D, nextall_paths(D, next) n = D.NumberOfRows;// initialize next: if no intermediate// vertices are allowed next[i][j] = jfor i = 1 to n

for j = 1 to nnext[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];

O(n3)

1 1 1min , 0

0

k k kij ik kjk

ijij

d d d kd

c k

Page 94: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

Floyd's AlgorithmInput Parameters: next, i, jOutput Parameters: Noneprint_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);}

Page 95: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

Lecture 2: Dynamic Programming

Traveling Salesman Problem

Page 96: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

Traveling Salesman Problem (TSP)

Page 97: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

Traveling Salesman Problem (TSP)

How many feasible paths?

n cities

! ( 1)!2 2n nn

Page 98: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

Example (TSP)

1 2

3 4

2

6 4 7 6

5

(1234) = 18(1243) = 19(1324) = 23(1342) = 19(1423) = 23(1432) = 18

Page 99: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

Subproblem Formulation for TSP

1 S

i

g(i, S) length of the shortest path from i to 1 visiting each city in S exactly once.

g(1, V {1})length of the optimal TSP tour.

Page 100: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

Subproblem Formulation for TSP

1 S

i

g(i, S) length of the shortest path from i to 1 visiting each city in S exactly once.

S{j}

j

( , ) min ( , { })ijj Sg i S d g j S j

dij

Goal: g(1, V {1})

Page 101: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

Example ( , ) min ( , { })ijj S

g i S d g j S j

Goal: g(1, V {1})

4 4

2 6 42 7 66 7 54 6 5

ijD d

(1,{2,3,4})g

(2,{3,4})g

d12

(3,{2, 4})g

d13

(4,{2,3})g

d14

(3,{4})g

d23

(4,{3})g

d24

(2,{4})g

d32

(4,{2})g

d34

(2,{3})g

d42

(3,{2})g

d43

(4, )g

d34

(3, )g

d43

(4, )g

d24

(2, )g

d42

(3, )g

d23

(2, )g

d32

11 22

33 44

2

6 4 7 6

5

11 22

33 44

2

6 4 7 6

5

Page 102: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

Example ( , ) min ( , { })ijj S

g i S d g j S j

Goal: g(1, V {1})

4 4

2 6 42 7 66 7 54 6 5

ijD d

(1,{2,3,4})g

(2,{3,4})g

d12

(3,{2, 4})g

d13

(4,{2,3})g

d14

(3,{4})g

d23

(4,{3})g

d24

(2,{4})g

d32

(4,{2})g

d34

(2,{3})g

d42

(3,{2})g

d43

(4, )g

d34

(3, )g

d43

(4, )g

d24

(2, )g

d42

(3, )g

d23

(2, )g

d32

4 6 4 2 6 2

5 5 6 6 7 7

9 11 10 8 13 97 6 7 5 6 5

16 13 142 6 4

11 22

33 44

2

6 4 7 6

5

11 22

33 44

2

6 4 7 6

5

18

Page 103: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

DP TSP AlgorithmInput Parameter: DOutput Parameter: P // pathTSP(D) n = Dim(D);for i = 1 to n

g[i, ] = D[i, 1];for k = 1 to n2 // compute g for subproblems for all S V{1} with |S|=k for all i S {1}

g[i, S] = minjS{D[i, j], g[j, S {j}] };P[i, S] = arg minjS{D[i, j], g[j, S {j}] };

// compute the TSP tourg[1, V{1}] = minjV{1}{D[1, j], g[j, V {1, j}]};P[1, V{1}] = arg minjV{1}{D[1, j], g[j, V {1, j}]};

( , ) min ( , { })ijj Sg i S d g j S j

Goal: g(1, V {1})

Page 104: Lecture 2: Dynamic Programming 主講人 : 虞台文. Content What is Dynamic Programming? Matrix Chain-Products Sequence Alignments Knapsack Problem All-Pairs Shortest

DP TSP AlgorithmInput Parameter: DOutput Parameter: P // pathTSP(D) n = Dim(D);for i = 1 to n

g[i, ] = D[i, 1];for k = 1 to n2 // compute g for subproblems for all S V{1} with |S|=k for all i S {1}

g[i, S] = minjS{D[i, j], g[j, S {j}] };P[i, S] = arg minjS{D[i, j], g[j, S {j}] };

// compute the TSP tourg[1, V{1}] = minjV{1}{D[1, j], g[j, V {1, j}]};P[1, V{1}] = arg minjV{1}{D[1, j], g[j, V {1, j}]};

O(2n)

( , ) min ( , { })ijj Sg i S d g j S j

Goal: g(1, V {1})