Professional Documents
Culture Documents
lgorithms
Steps:
1. Characterize the structure of an
optimal solution.
2. Recursively define the value of an
optimal solution.
3. Compute the value of an optimal
solution in a bottom-up fashion.
4. Construct an optimal solution from
computed information.
Assembly-line Sheduling
f1[1] = e1 + a1,1
f1[ j] = min( f1 [ j–1] + a1,j , f2[ j–1] + t2,j-1 +
a1,j)
f2 [1] = e2 + a2,1
f2[ j] = min( f2 [ j–1] + a2,j , f1 [ j–1] + t1,j-1 +
a2,j)
Then: f* = min ( f1[n]+x1, f2[n]+x2 ).
A recursive solution II.
The idea is to get sub problems that express the main problem space simple as possible
but without left some details.
When this process has end we must solve them for build the solution.
Optimal substructure varies across problem domains in two ways:
• how many subproblems are used in an optimal solution to the original problem
• how many choices we have in determining which subproblem(s) to use in an optimal
solution
The running time of a dynamic programming algorithm depends on the product of two
factors:
That requires make a choice among subproblems as to which we’ll use in solving the
problem.
Its cost is usually the subproblem cost plus a cost that is directly attributable to the choice
itself.
Overlapping sub problems
T (1) 1
n 1
for n > 1
T ( n) 1 (T ( k ) T ( m k ) 1)
i 1
n 1
T ( n) 2 T (i ) n
i 1
We have as a result:
n 1 n 1
T ( n ) 2 2 i 1
n 2 2i n 2( 2 n 1 1) n 2 n 1
i 1 i 1
The work is at least and exponential of n.
Compare this top down with the bottom up, DP. The latter is more efficient
because it takes advantage of the overlapping-subproblems property.
There are only (n2) different subproblems, and DP solves each exactly once.
Reconstructing an optimal solution
S1= ACCGGTCGAGTGCGCGGAAGCCGGCCGAA
S2= GTCGTTCGGAATGCCGTTGCTCTGTAAA
A subsequence of a given
sequence is just the given
sequence with zero or more
elements left out.
FORMALLY
4 B
5 D
6 A
7 B
j 0 1 2 3 4 5 6
i Yj B D C A B A
0 Xi 0 0 0 0 0 0 0
1 A 0
2 B 0
3 C 0
4 B 0
5 D 0
6 A 0
7 B 0
j 0 1 2 3 4 5 6
i Yj B D C A B A
0 Xi 0 0 0 0 0 0 0
1 A 0 0
2 B 0
3 C 0
4 B 0
5 D 0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