Download presentation
Presentation is loading. Please wait.
1
Dynamic Programming Reporter : 林明慧
2
Outline The Longest Common Subsequence Problem 0/1 Knapsack Problem
3
The Longest Common Subsequence Problem
The longest common subsequence problem is to find a longest common subsequence between two strings. A straightforward method is to find all common subsequences by an exhaustive search.
4
The Longest Common Subsequence Problem
Example A = a b a a d e c B = b a f c Try subsequences chosen from B with length three : a f c b f c b a c b a f
5
The Longest Common Subsequence Problem
Disadvantage Exceedingly time-consuming New method By tracing back the procedure finding the length of the longest common subsequence.
6
The Longest Common Subsequence Problem
A = x1 x2 … xm , B = y1 y2 … yn Two possibilities : xm = yn. xm is contained in the longest common subsequence. x1 x2 … xm-1 y1 y2 … yn-1 xm yn. x1 x2 … xm with y1 y2 … yn-1 x1 x2 … xm-1 with y1 y2 … yn Example : A = a b a a d e c B = b a f c e c f c
7
Recursive formula x1 x2 … xi-1 xi xm y1 y2 yj-1 yj yn 1 1
8
Example A = a b a a d e c , B = c a a c e d c xi yj a b d e c 1 2 3 4
1 2 3 4 5 6 7 L(i,j) 1 2 1 2 1 2 3 1 2 3 1 2 3 4 4 1 2 3 1 1 2 1 1 1 1 1 2 1 2 1 1 2
9
Experiment_1 A = a b a a d e c , B = c a a c e d c xi yj a b d e c 1 2
1 2 3 4 5 6 7 L(i,j) 1 2 3 1 2 3 1 2 3 4 1 1 1 2 1 2 3
10
Experiment_2 A = a b a a d e c , B = c a a c e d c xi yj a b d e c 1 2
1 2 3 4 5 6 7 L(i,j) 1 2 3 1 2 3 1 2 3 4 1 1 1 2 1 2
11
Result_1 A = a b a a d e c , B = c a a c e d c
A longest common subsequence is a a e c. A = a b a a d e c , B = c a a c e d c xi yj a b d e c 1 2 3 4 5 6 7 L(i,j)
12
Result_2 A = a b a a d e c , B = c a a c e d c
A longest common subsequence is a a d c. A = a b a a d e c , B = c a a c e d c xi yj a b d e c 1 2 3 4 5 6 7 L(i,j)
13
0/1 Knapsack Problem The 0/1 knapsack problem is to maximize the total profit under the constraint that the total weight of all chosen objects is at most capacity.
14
0/1 Knapsack Problem It is defined as follows:
Given n objects and a knapsack. Object i has a weight Wi. Knapsack has a capacity M. If object i is placed into the knapsack, we will obtain a profit Pi.
15
Example Capacity M = 10 i Wi Pi 1 10 40 2 3 20 5 30 x2=0 x3=0 A C F
40 10-10=0 G S x3=1 30 D 7-5=2 x2=1 20 H T 10-3=7 x3=0 x1=0 B 7-0=7 I 10-0=10 x3=1 E 30 x2=0 10-5=5 10-0=10 J x3=0 10-0=10
16
Example d(S,T) = max{40+d(A,T) , 0+d(B,T)} d(A,T) d(B,T) A 40 S T B S
C D E F G H I J T d(S,T) = max{40+d(A,T) , 0+d(B,T)} A 40 d(A,T) S T B d(B,T)
17
Example d(A,T) = 0+0+0 = 0 d(B,T) = max{20+d(D,T) , 0+d(E,T)} d(D,T)
S A B C D E F G H I J T d(A,T) = = 0 d(B,T) = max{20+d(D,T) , 0+d(E,T)} A C F T D d(D,T) 20 B T d(E,T) E
18
Example S A B C D E F G H I J T d(D,T) = max{30+d(G,T) , 0+d(H,T)} = max{30+0 , 0+0} = max{30 , 0} = 30 d(E,T) = max{30+d(I,T) , 0+d(J,T)} = max{30+0 , 0+0} = max{30 , 0} = 30 G 30 D T H E I J T 30
19
Example S A B C D E F G H I J T d(B,T) = max{20+d(D,T) , 0+d(E,T)} = max{20+30 , 0+30} = max{50 , 30} = 50 d(S,T) = max{40+d(A,T) , 0+d(B,T)} = max{40+0 , 0+50} = max{40 , 50} = 50 B D E T 20 d(D,T) d(E,T) S A B T 40 d(A,T) d(B,T)
20
Example Capacity M = 10 0 + 20 + 30 + 0 = 50 i Wi Pi 1 10 40 2 3 20 5
= 50 x2=0 x3=0 A C F x1=1 40 G S x3=1 30 D x2=1 20 H T x3=0 x1=0 B x3=1 I E 30 x2=0 J x3=0
21
Another Expression Recursive formula
22
Example Capacity M = 10 Capacity j i 1 2 3 4 5 6 7 8 9 10 W1=10, P1=40
1 2 3 4 5 6 7 8 9 10 W1=10, P1=40 40 W2=3, P2=20 20 W3=5, P3=30 30 50 P[1,10] = max{P[1-1,10],40+P[1-1,10-10]} = 40 P[2,10] = max{P1,P2} = 40 P[2,3] = max{P[2-1,3],20+P[2-1,3-3]} = 20 P[3,3] = P2 = 20 P[3,5] = max{P[3-1,5],30+P[3-1,5-5]} = 30 P[3,8] = P2+P3 = 50 P[3,10] = max{P1,(P2+P3)} = 50
23
The End Have A Nice Holiday!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.