Cs 440 Theory of Algorithms / Cs 468 Al Ith Ibiif Ti Cs 468 Algorithms in Bioinformatics
Cs 440 Theory of Algorithms / Cs 468 Al Ith Ibiif Ti Cs 468 Algorithms in Bioinformatics
CS 468 Al
Algorithms
ith
iin Bi
Bioinformatics
i f
ti
Dynamic Programming
Part II
0
1
0
0
0
0
0
1
1
0
0
0
0
1
0
0
0
1
0
1
0
1
0
1
1
1
0
1
0
1
0
1
8-1
Warshalls Algorithm
Main idea: a path exists between two vertices i, j, iff
there is an edge
g from i to j; or
there is a path from i to j going through vertex 1; or
there is a path from i to j going through vertex 1 and/or 2; or
there is a path from i to j going through vertex 1, 2, and/or 3; or
...
there is a path from i to j going through any of the other vertices
3
3
3
3
1
1
1
1
2
0
1
0
0
R0
0 1
0 0
0 0
1 0
2
R1
0 0
1 0
0 0
0 1
0
1
0
0
4
1
1
0
0
0
1
0
0
R2
0 0
1 0
0 0
1 1
1
1
0
1
0
1
0
1
2
R3
0 0
1 0
0 0
1 1
1
1
0
1
0
1
0
1
2
R4
0 0
1 1
0 0
1 1
1
1
0
1
0
1
0
1
8-2
Warshalls Algorithm
On the kth iteration,, the algorithm
g
determine if a p
path exists
between two vertices i, j using just vertices among 1,,k allowed
as intermediate
R(k)[i,j]
[i j] =
R(k-1)[i,j]
(path using just 1 ,,k-1)
or
(R(k-1)[i,k] and R(k-1)[k,j]) (path from i to k
and from k to i
k
using just 1 ,,k-1)
kth iteration
j
8-3
8-4
8-6
8-7
6
1
4
Design and Analysis of Algorithms - Chapter 8
8-9
i
D(k-1)[[k,j]
,j]
D(k-1)[i,j]
j
8-11
8-12
Timee efficiency:
e c e cy: (
(n3)
Space efficiency: Matrices can be written over their predecessors
Copyright 2007 Pearson Addison-Wesley. All rights reserved
Knapsack Problem by DP
Given n items of
integer weights:
values:
w1 w2 wn
v1 v2 vn
w3 = 3, v3= 20
1
2
3
w4 = 2,
2 v4= 15 4
Copyright 2007 Pearson Addison-Wesley. All rights reserved
Knapsack Problem
V[i, j] = max (V[i 1, j], V[i 1, j wi] + vi)
objecti not used
objecti used
8-16
Knapsack Problem
V[i, j] = max (V[i 1, j], V[i 1, j wi] + vi)
objecti not used
objecti used
8-17
8-18
8-19