Sie sind auf Seite 1von 172

Dynamic

Programming

1
Dynamic Programming

"Dynamic programming algorithm stores the


results for small sub problems and looks
them up, (rather than re computing them,)
when it needs them later to solve larger sub
problems"

2
Optimal substructure:
 We say that a problem exhibits optimal
substructure if an optimal solution to the
problem contains within its optimal solution to
subproblems.

 Example: Matrix-multiplication problem

3
Subtleties
 One should be careful not to assume that optimal substructure
applies when it does not. consider the following two problems in
which we are given a directed graph G = (V, E) and vertices u, v
 V.
 Unweighted shortest path:
 Find a path from u to v consisting of the fewest edges.
Good for Dynamic programming.
 Unweighted longest simple path:
 Find a simple path from u to v consisting of the most edges.
Not good for Dynamic programming.

4
Steps in developing a dynamic
programming algorithm

 Characterize the structure of an optimal


solution
 Recursively define the value of an
optimal solution
 Compute the value of an optimal solution
in a bottom-up fashion
 Construct an optimal solution from
computed information
5
Matrix chain multiplication

Problem:
We are given a sequence(chain)
<A1, A2, A3,…An> of n matrices
to be multiplied, we wish to compute
the product A1A2….An.using least work.

6
Since matrix multiplication is associative so all ways
of parenthesization will yield the same answer.

The product of A1A2A3A4 can be parenthesized in


five distinct ways

(A1(A2(A3A4)))
(A1((A2A3)A4))
((A1A2)(A3A4))
((A1(A2A3))A4)
(((A1A2)A3)A4)
7
Although answer by each one will be same, but the
cost of evaluating varies drastically.

Example: A1:10  100


A2 : 100  5
A3: 5  50
((A1A2)A3):total cost = 10.100 .5+10.5.50=
7500
(A1(A2A3)):total cast 100.5.50 +10.100.50 =
75000

8
P (k ) P (n  k ) ifn  2

Matrix chain multiplication problem:

Given a chain<A1, A2, …An> of n matrices


where for i = 1,2…n; matrix Ai has
dimension pi-1x pi,
fully parenthesize the product A1A2…An in a
way that minimizes the number of scalar
multiplications.
P(n) : no of alternative ways to parenthesize

P(n) = ∑k=1n-1P(k)P(n-k)
9
Let us use Dynamic programming approach

Let us divide the problem as: for i <j


Ai..j denotes AiAi+1…Aj

10
To evaluate Ai..j We must find an index k
Ai..j = Ai..k Ak+1..j for some k in between i and j.
Total cost to get Ai..j =

cost of computing Ai..k + cost of Ak+1..j + cost


of combining
Optimal cost of Ai..j can be obtained by
getting each of Ai..k and A k+1..j optimally.

Thus optimal solution of the original problem can be


obtained from the optimal solutions of
subproblems.

11
Recursive solution

Let m[i ,j] = minimum no. of multiplications


needed to compute the matrix Ai..j.
So answer of the given problem is m[1,n].
Now m[i,j] can be defined recursively as:
m[i , i] = 0
Let k be the index for the optimal solution
m[i,j] = m[i ,k ] + m[k+1 , j] +
pi-1pkpj 12
Recursive definition for minimum cost pf
parenthesizing the product A1A2….An
becomes:

0 if i=j
m[i ,j] =
min{m[i ,k] + m[k+1 ,j] + pi-1pkpj

13
To keep track of successive decisions let us use
S[i j] to denote the value of index k from where
sequence Ai…Aj is to be partitioned.

Optimal costs are calculated by using a tabular bottom


approach.
Two auxiliary tables
m[1..n,1..n] for storing m[i ,j] costs and
s[1..n,1..n] for recording indices (k) for optimal
costs are used.

14
for finding m[i ,j]
optimal results for Ai..k and Ak+1..j are used
and
size of Ai..k is k-i+1 which is less than j-i+1
Also
size ofAk+1..j is j-k which is also less than j-i+1

So Table m[i,j] is filled by bottom up


approach.

15
Matrix-chain-order (p)
1 n ← len[p] - 1
2 For i ← 1 to n
3 do m[i ,i] ← 0
4 for i ← 2 to n
5 do for l ← 1 to n- i +1
6 do j ← i+l -1
7 m[i,j] ← α
8 for k ← I to j – 1
9 do q ← m[I , k] + m [ k+1 ,j] +
pi-1pkpj
16
10 If q < m[I,j]
11 then m[I,j] ← q
12 s[i,j] ← k
13 Return m , s

17
MatrixDimension
A1 30 x 35
A2 35 x 15
A3 15 x 5
A4 5 x 10
A5 10 x 20
A6 20 x 25
m[2,2] + m[3,5] + p1p2p5 = 13000
M[2,5] = min m[2,3] +m[4,5] + p1p3p5 = 7125
m[2,4] + m[5,5] + p1p4p5 = 11375
= 7125
18
M
1
6 i
15125 2
5 3
10500
11875
j 4 5375
7125 4
9375 5
3500
3 2500 6
4375 5000
7875 1000
750 0
2 2625 0
15750
0
0
1 0 0

19
S
1
6 i
2
3
3
5 3
j 3
4 3 4
3
3 5 5
3 3
1 3 5
4
2 2 3
1

20
Optimal solution
 Optimal way to multiplication of these six
matrices

((A1(A2A3))((A4A5)A6))

21
Example
 Let us begin with a simple capital budgeting
problem.

A corporation has $5 million to allocate to its three


plants for possible extension.
Each plant has submitted a number of proposals .
Each proposal gives the cost of expansion(c ) and
total revenue expected( r ).

22
An Example of
Dynamic
Programming

23
Courses
A student has to take examination A B C
in three courses, A,B,C. He days
has three days available for
study. He feels it would be best 0 0 1 0
to devote a whole day to study
of the same course, so that he
may study a course for one 1 1 1 1
day, two days, or three days or
not at all. His estimates of the
grades he may get by study
are as follows: 2 1 3 3
How should he study so
that he maximizes the 3 3 4 3
sum of his grades
24
Let u1, u2,u3 be the no of days he should study
the courses A,B,C resp.

let f1(u1) f2(u2), f3(u3) be the grades earned by


such a study.
The problem is

Maximize f1(u1) + f2(u2) +f3(u3)


Subject to u1 +u2 +u3 ≤ 3
u1, u2, u3 ≥ 0 and integers

25
let us introduces the state variables xj as:
X3 = u 1 + u 2 + u 3 ≤ 3
X2 = u 1 + u 2 = x 3 – u 3
X1 = u 1 = x2 – u2
Xj denotes no of days available to study
courses from course 1 to course j
Fj(xj)= max [fj(uj) + Fj-1(x j-1)] X j-1 = xj - uj
F1(x1) = f1(u1)
F3 (x3) = max [ f1(u1) + f2(u2) +f3(u3)

26
F2(3)=max( f2(0)+ F1(3-0), f2(1)+F1(3-1),f2(2)+F1(3-2),f2(3)+F1(3-3)

F1(x1)

F2(u2) +F1(x1) F2(x2)


Uj 0 1 2 3
j
U2
1 0 1 1 3 x2
0 1 2 3

Cours
0 1 - - - 1
es A B C
days 1 2 1 - - 2
0 0 1 0
2 2 2 3 - 3
1 1 1 1
2 1 3 3 3 4 2 4 4 4
3 3 4 3 27
Cours F2(u +F1(x1 F2(x
es A B C 2) ) 2)
U2
days x2
0 1 2 3
0 0 1 0 0 1 - - - 1
1 1 1 1 1 2 1 - - 2
2 1 3 3 2 2 2 3 - 3
3 3 4 3 3 4 2 4 4 4
F3(u3)
+F2(x2) F3(x
3)
U3 0 1 2 3
x3

0 1 - - - 1
1 2 2 - - 2
2 3 3 4 - 4
28

3 4 4 5 4 5
 Thus the optimal way to study
u3 = 2
F2(3-2)=F2[1] obtained by decision u2 = 0
F1(3-2-0)=F1[1] =1 obtained by taking u1 = 1

Thus he should study as follows:


Course A one day
B zero days
C two days
grade total = 5
29
Optimal Binary search
Tree

30
Optimal Binary search Tree
It is one of the most important data structures in
Computer Science

One of its main application is to implement a


dictionary.

It is natural to pose a question about an optimal


binary search tree for which the average no
of comparisons in a search is the smallest
31
Let the set of identifiers be
= {for , do , while, int, if}Binary search trees
Average no of searches (if assumed equal probability of
occurrence of each)
= 12/5 11/5

for for

do int
do while

if while
int

if

32
General case with different
probabilities
Let us take the set of n distinct keys {k 1, k2….kn}
Where k1< k2 < …<.kn
And
p(i)= prob. of ki
some searches may not be in K, so we have k+1 dummy
keys d0,d1,…dn representing values not in K.
let
do - all values less than k1
dn all values greater than kn
di all values between ki and ki+1
33
Optimal Binary search trees

cost:2.75
cost:2.80
optimal!!

34
n n

i 1
pi  q
i 0
i 1

we assume that the actual cost of a search is the number of nodes examined .i.e. the depth of the
node found by the search in T plus one

the expected cost of a search in T is

n n
E[search cost in T]   (depth T (ki )  1)  pi   (depth T (d i )  1)  qi
i 1 i 0
n n
 1   depth T (ki )  pi   depth T (d i )  qi
i 1 i 0

35
 For a given set of probabilities, our goal is
to construct a binary search tree whose
expected search is smallest. We call such a
tree an optimal binary search tree. 36
 Note: optimal binary search tree need not be
of least height

 Key of max probablity need not be at root

37
The structure of an optimal binary search
tree

 Consider any subtree of a binary search tree.

It must contain keys in a contiguous range ki,


...,kj, for some 1  i  j  n.
In addition, a subtree that contains keys ki, ..., kj
must also have as its leaves the dummy keys
di-1, ..., dj.

38
If an optimal binary search tree T has a subtree
T' containing keys ki, ..., kj,

then this subtree T' must be optimal as well ,for


the subproblem with keys ki, ..., kj and dummy
keys di-1, ..., dj.

39
Given keys ki,… kj one of the keys say kr will be
root of optimal tree of these keys.
left subtree of it will contain keys
ki … kr-1,
di-1,… dr-1
While right subtree will have
kr+1… .kj
dr,… dj
Note for the subtree rooted at ki
Left subtree will have di-1 only
While for the tree rooted at kj will contain only dj as
right subtree.

40
e[i,j] = expected cost of searching in optimal
binary search tree for ki,…kj

Note

if j = I-1

e[i, i-1] = q i-1


41
kr

Ki, kr-1 Kr+1,…kj

e[I,r-1]
e[r+1,j]

42
l j l j
w(i, j )   pl
l i
  ql
l i 1

 e[i,j] = pr + e[i, r-1] + w[i,r-1] +e[r+1,j]


+w[r+1,j]

 = e[i,r-1] + e[r+1,j] +w[i,j]

43
Step 2: A recursive solution

j j
w(i, j )   pl   ql
l i l i 1

w(i, j )  w(i, j  1)  pj  qj

 qi 1 if j  i -1,
e[i, j ]  
min {e[i, r  1]  e[r  1, j ]  w(i, j )} if i  j.
ir  j

44
computing the expected search cost of an
optimal binary search tree

OPTIMAL-BST(p,q,n)
1 for i  1 to n + 1
2 do e[i, i – 1]  qi-1
3 w[i, i – 1]  qi-1
4 for l  1 to n
5 do for i  1 to n – l + 1
6 do j  i + l – 1
7 e[i, j]  
8 w[i, j]  w[i, j – 1] + pj+qj 45
9 for r  i to j
10 do t  e[i, r –1]+e[r +1, j]+w[i, j]
11 if t < e[i, j]
12 then e[i, j]  t
13 root[i, j]  r
14 return e and root
 the OPTIMAL-BST procedure takes (n3), just
like MATRIX-CHAIN-ORDER
46
The table e[i,j], w[i,j], and root[i,j] computer by
OPTIMAL-BST on the key distribution.

47
 Root[i,j] = index r for which kr is root of the
optimal binary search tree for ki,….kj

48
 Knuth has shown that there are always roots of
optimal subtrees such that root[i, j –1]  root[i+1, j]
for all 1  i  j  n. We can use this fact to modify
Optimal-BST procedure to run in (n2) time.

49
 Draw all binary search trees for set
 { while ,if and do} and find expected costs

while if

if do while

do

a b

50
do
do while

if while
do

while if if

c d e

51
Ex1: pi = 1/7 =qi
Ex2 p = (0.5, 0.1 , 0.05)
q = (0.15, 0.1, 0.05, 0.05)
ex1 ex2
a 19/7 3.0
b 17/7 2.25
c 19/7 1.85
d 19/7 2.40
e 19/7 1.95
52
To apply dynamic programming approach we
need to view the construction of such a tree
as the result of a sequence of decisions.
A possible approach would be to make a
decision as to which of the ki’ s should be
assigned to the root node of the tree.
If we choose kr as root then internal nodes k1,
k2, …kk-1 as well as external nodes for the
classes d0, d1,…dk-1 will lie in the left subtree
“l”of the root. Remaining will be be in the
right subtree “r.”
53
Plant1 Plant2 Plant 3
Proposal C1 r1 C2 r2 C3 r3

1 0 0 0 0 0 0
2 1 5 2 8 1 4
3 2 6 3 9 - -
4 - - - -
4 12

54
Each plant will only be permitted to work on one of
its proposals.
The goal is to maximize the firm’s revenue resulting
from the allocation of the $ 5 million.
A straight forward way to solve this is to try all
possibilities and choose the best.
In this case there are only 3 4  2 = 24 ways of
allocating the money.
Many of these are infeasible.
55
Let us break the problem into three stages:
Each stage represents the money allocated
to a single plant.
So stage one represents the money
allocated to plant 1
Stage 2 the money to plant 2
Stage 3 the money allocated to plant 3
Each stage is divided into states.A state
encompasses the information required to
go from one stage to the next

56
{0,1,2,3,4,5}: the amount of money spent on
plant 1.
{0,1,2,3,4,5}: the amount of money spent on
plants 1 and 2
{5}: the amount of money spent on plants 1,2
and 3.

Associated with each state is a revenue.


Note that to make a decision at stage 3, it is
only necessary to know how much was
spent on plants 1 and 2, not how it was
spent.

57
The easy possibility is stage 1

If the available Then the optimal the


capital is revenue
proposal is for stage 1
is
0 1 0
1 2 5
2 3 6
3 3 6
4 3 6
5 3 6

58
We are now ready to compute for stage 2.
In this case we want to find the best solution
for both plants 1 and 2.
We want to calculate the best revenue for a
given amount for plants 1 and 2.
We simply go through all the plant 2
proposals, allocate the given amount of
fund to it and use the above table to see
how plant 1 will spend the remainder.

59
uppose we want to determine best allocation for state
value 4.
proposal 1 of 2 gives revenue 0, leaves 4 for plant 1
which returns 6: total:6
proposal 2 of 2 gives revenue 8 leaves 2 for plant 1
which returns 6: total:14
proposal 3 of 2 gives revenue 9, leaves 1 for plant 1
which returns 5: total:6
proposal 4 of 2 gives revenue 12, leaves 0 for plant 1
which returns 0: total:12

60
If available Then the And the
capital x2 optimal revenue for
proposal is stages 1
and 2 is

0 1 0
1 1 5
2 2 8
3 2 13
4 2 or 3 14
5 4 17

61
We can now go on to stage 3.
The only value we are interested in is x3 = 5.
One again we go through all the proposals
for this stage, determine the amount of
money remaining and use the above table
to decide the value for previous stages.
Proposal 1 of plant 3 gives revenue 0 and
leaves 5 to prev. stage giving revenue = 17
Proposal 2 of plant 3 gives revenue 4 leaves
4 prev stages give 14 so total revenue =
18
62
Here calculations are done recursively.
Stage 2 calculations are based on stage 1,
Stage 3 only on stage 2.
In fact given at a state all future decisions
are made independent of how you got to
the state.
This is principle of optimality.
Dynamic programming rests on this
assumption.

63
Principle of Optimality
 an optimal sequence of decisions has
the property that whatever the initial
state and decisions are, the remaining
decisions must constitute an optimal
decision sequence with regard to the
state resulting from the first
decision.

64
All pair shortest paths
The Floyd Warshall Algorithm

Let G=(V,E) be a directed graph with n


vertices.
Let C be a cost adjacency matrix for G
such that C(i,i)= 0 for all i.
All pair shortest path problem is to
determine a matrix A such that A(i,j) is
the length of a shortest path from i to
j.
65
We assume that there is no cycle of
negative length.
Let us examine a shortest path from i to j in
G.
This path originates at vertex i and goes
through some intermediate vertices and
terminates at vertex j.
Let k be an intermediate vertex on this
shortest path then the subpath from i to k
and from k to j must be shortest paths
from i to k and k to j.
66
So the principle of optimality holds.

If k is the intermediate vertex with highest


index then the i to k path is a shortest path
in G going through no vertex with index
greater than k-1.
Similarly the k to j path is a shortest k to j
path in G going through no vertex greater
than k –1.

67
Let
Ak(i , j) represent the length of a shortest path from i
to j going through no vertex of index greater than k,
A(i, j) represent shortest distance between i and j
A(i,j) = min {min{Ak-1(i , k) + A k-1 ( k , j)},
cost (i, j)}
1  kn
Clearly
A0(i,j) = cost (i , j )
Ak(i, j)
= min{Ak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1 -3 2 -4
3 0 -4 1 -1 ^ 3 4 5 1
7 4 0 5 3 4 ^ 4 2 1
2 -1 -5 0 -2 4 3 ^ 2 1
8 5 1 6 0 4 3 4 ^ 1
4 3 4 5 ^

82
 Path from node 1 to node 3

 Using matrix M distance between 1 and 3 is -3


 using matrix P we can find path also
 P(1,3) I n the final matrix is 4
 So node 4 is preceding node 3
 P(1,4 ) is 5
 So predecessor of 4 is 5
 P(1,5) is 1 itself
 So the path is
 1 - 5 –4 - 3

83
Warshall’s Algorithm

 Inventor Stephen Warshall


 Algorithm for computing the
transitive closure of a graph
 Idea of this algorithm can be
interpreted as an application of
“Dynamic Programming”
technique
84
Warshall’s Algorithm
 Adjacency Matrix
 DEFINITION: The adjacency matrix of a
graph with n vertices is an n-by-n boolean
matrix A with one row and one column for
each of the graphs vertices, in which aij
the element in the ith row and the jth
column is equal to 1 if there is an edge
from the ith vertex to the jth vertex and
equal to 0 if there is no such edge.
85
 DEFINITION: The transitive closure of a
directed graph with n vertices can be
defined as the n-by-n boolean matrix
T={tij}, in which the element in ith row and
the jth column is 1 if there exist a
nontrivial directed path (i.e., a directed
path of a positive length) from the ith
vertex to the jth vertex otherwise, tij is 0.

86
Warshall’s Algorithm

(a) Digraph
(b) Adjacency matrix

(c) Transitive closure


87
Warshall’s Algorithm
 Algorithm
 Series of n-by-n matrices
R ( 0) ,..., R ( k 1) , R ( k ) ,...R ( n )

 kth matrix
 directed path in the digraph
 Element rij(k ) is 1 only if there is a path from
ith vertex to the jth vertex with intermediate
vertices numbered not higher than k
88
Warshall’s Algorithm
 Recurrence Relation
(k )
 rij is 1
 vi, list of vertices numbered not higher
than k, vj ( k 1)
rij
 2 possibilities
 - List does not contain kth vertex , it
( k 1)
r
contains vertices numbered not ik higher
( k 1)
than k-1. Then kj is 1
r

89
 - List contains kth vertex. Then
 vi, vertices numbered ≤ k-1, vk, vertices
numbered ≤ k-1, vj
 => and are both 1

90
Warshall’s Algorithm
If an element rij is 1 in ( k 1)
, it remains 1 in .

R
(k )
If an element rij is 0 in , it has to be changed to 1 in if
R

and only if the element in its row( k i1) and column k and the
element in its column j and rowR k are both 1’s in .
 Basis: Adjacency matrix A

(k )
R

91
Warshall’s Algorithm
1 2 3 4
1 0 1 0 0
2 0 0 1 0
3 0 0 0 1
4 0 0 0 0

1 2 3 4
1 0 1 0 0
2 0 0 1 0
3 0 0 0 1
4 0 0 0 0 92
Warshall’s Algorithm
1 2 3 4
1 0 1 0 0
2 0 0 1 0
3 0 0 0 1
4 0 0 0 0

1 2 3 4
1 0 1 1 0
2 0 0 1 0
3 0 0 0 1 93

4 0 0 0 0
Warshall’s Algorithm
1 2 3 4
1 0 1 1 0
2 0 0 1 0
3 0 0 0 1
4 0 0 0 0

1 2 3 4
1 0 1 1 1
2 0 0 1 1
3 0 0 0 1
94

4 0 0 0 0
Warshall’s Algorithm
1 2 3 4
1 0 1 1 1
2 0 0 1 1
3 0 0 0 1
4 0 0 0 0

1 2 3 4
1 0 1 1 1
2 0 0 1 1
3 0 0 0 1
95

4 0 0 0 0
Warshall’s Algorithm

 Time efficiency in Θ(n³)


 Space efficiency in Θ(n³)

 Possible improvement of space


efficiency to be in Θ(n²)

96
Shortest path problem
Suppose we have a graph that represents the high
way system of a state or country. In this graph ,
the vertices represent cities and the edges
represent sections of the high way. Each edge
has a weight representing the distance between
the two cities connected by the edge.
A motorist wishing to derive from city A to city b
would be interested answers to the following
questions:
Is there a path from A to B?
If there are more than one path from A to B which
path is the shortest.
97
The problem of finding shortest path from a specified
Vertex S to another specified vertex T can be stated as
follows:
A simple weighted digraph G on vertex is described
by an N*N , weight matrix D where
dij=length of the directed edge from vertex i to
vertex j, dij >= 0
dij =0
dij = α if there is no edge from i to j

98
In general dij �dji ,and triangle inequality need not be
Satisfied , ie dij + djk may be less than dik .

The distance of a directed path P is defined as the sum


of the lengths of the edge in P.

The problem is to find the shortest path and its distance


(length) from a starting vertex S to a terminal vertex T.

99
Dijkstra’s Algorithm
d[i,j] – Weight of the arc from i to j
Distance[i] – keeps the distance of the shortest path known
thus far from s to i.
Initially distance[s]=0 and distance[i] = α for all i �
≠ s

A set perm contains all nodes whose minimal(coverall)dist


from s is known – i.e, those nodes whose distance value
is permanent and will not get changed.
If i is a member of perm, distance[i],is minimal
distance from s to i.

100
Initially only member of perm is s.
As soon as t becomes a member of perm, algorithm termin
The algorithm maintains a variable “current “ which is the
node that has been added to perm, most recently. Initially
Current =s. Whenever a node current is added to perm,
distance must be recomputed for all vertices which
are successors of current.

For every successor i of current


if distance[current]+weight[current,i] is less than
(distance from s to i through current is smaller than)
any other distance from s to i found thus far t
Thus
Distance[i] must be reset.
101
The distances have to be recomputed to every successor
of current

distance[j] represents the shortest path from


s to j that includes only members of perm(except for
i itself).This means that for the node k, not in perm and
for which distance [k] is smallest, there is no path from
s to k, whose length is shorter than distance[k] thus k
can be added to perm. Current is then reset to k and
process is repeated

102
2
a d
8
7 2
2 1
10
3
b 2
e g
4
1 4
7
c 3 f

103
A B C D E F G
α 0 α α α α α
7 0 1 α α α α
7 0 1 α α α α
4 0 1 α 5 4 α
4 0 1 α 5 4 α
4 0 1 14 5 4 11
4 0 1 14 5 4 11
4 0 1 12 5 4 11
4 0 1 12 5 4 11
4 0 1 12 5 4 7
4 0 1 12 5 4 7
104
Path can also determined moving backward.
Start back from node G till we find a place where
present value of distance is obtained. At that place
the current gives the node which precedes G.

BCEG

105
Bellman Ford Algorithm

106
107
TheBellman Ford algorithm solves the single
source shortest paths problem in general
case in which edge weights may be negative.
Algorithm returns a boolean value indicating
whether or not there is a negative weigh
cycle that is reachable from the source.

108
 When there is no cycle of negative length,
there is a shortest path between any two
vertices of an n- vertex graph that has at most
n-1 edges in it.
 Distl [u] be length of a shortest path between
source v to a vertex u under the constraint
that sh.path contains at most l edges.
 Distn-1 [u] is the length of shortest path from v
to u
109
Recursive relation

 Dist k [u] = min{dist k-1 [u], min{distk-1 [i] +


cost[i,u]}

110
 6 2 -1 5

 1 -2 1 3
 5 3
 5 -2 3
6
4
 -1

111
distk [1..7]

K 1 2 3 4 5 6 7
1 0 6 5 5 ∞ ∞ ∞

2 0 3 3 5 5 4 ∞

3 0 1 3 5 2 4 7
4 0 1 3 5 0 4 5
5 0 1 3 5 0 4 3
6 0 1 3 5 0 4 3

112
113
114
115
116
117
118
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

119
120
dist pred
S 0 s
T 6, s
X α
Y 7 s
Z α

Edges are added in the order


5 8 -4 -2 -3 9 7 2 6 7
tx ty tz xt yx yz zx zs st sy 121
 Old new
dist pred dist pred
S 0 s S 0 s
T 6, s T 6, s
X α X 4 yy
Y 7 s Y 7 s
Z α Z 2 t

Edges are added in the order


5 8 -4 -2 -3 9 7 2 6 7
tx ty tz xt yx yz zx zs st sy 122
dist Old new dist pred
pred
S 0 s
S 0 s
T 6, s T 2, x
X 4 y X 4 y
Y 7 s Y 7 s
Z 2 t
Z 2 t

Edges are added in the order


5 8 -4 -2 -3 9 7 2 6 7
tx ty tz xt yx yz zx zs st sy 123
dist pred Old new dist pred
S 0 s S 0 s
T 2, x T 2, x
X 4 y X 4 y
Y 7 s Y 7 s
Z 2 t Z -2 t

Edges are added in the order


5 8 -4 -2 -3 9 7 2 6 7 124
dist pred
S 0 s
T 2 x
X 4 y
Y 7 s
Z -2 t

Shortest path from s to t


S------------------------------t
S ------------------------ x t
S-----------------------y x t
S y x t

S y x t
7 -3 -2 125
Problem: find shortest paths from A to all others adding
edges in the order
be, db, bd, ab, ac, dc, bc, ed

B
2

-1
2
3 1 E

A
-3
D
4
C 5

126
Review: Dynamic programming
 DP is a method for solving certain kind of
problems
 DP can be applied when the solution of a
problem includes solutions to subproblems
 We need to find a recursive formula for the
solution
 We can recursively solve subproblems, starting
from the trivial case, and save their solutions in
memory
 In the end we’ll get the solution of the whole
problem
127
be solved with dynamic
programming
 Simple Subproblems
 We should be able to break the original problem to
smaller subproblems that have the same structure
 Optimal Substructure of the problems
 The solution to the problem must be a composition
of subproblem solutions
 Subproblem Overlap
 Optimal subproblems to unrelated problems can
contain subproblems in common

128
Longest Common Subsequence
X = < A, B, C, B, D, A, B >
Y = < B, D, C, A, B, A >
 < B, C, A > is a common subsequence of
both X and Y.
 < B, C, B, A > or < B, C, A, B > is the longest
common subsequence of X and Y.

129
Longest-common-subsequence
problem:
 We are given two sequences X =
<x1,x2,...,xm> and Y = <y1,y2,...,yn> and wish to
find a maximum length common
subsequence of X and Y.

 We Define Xi = < x1,x2,...,xi >.

130
(Optimal substructure of LCS)
 Let X = <x1,x2,...,xm> and Y = <y1,y2,...,yn> be the
sequences, and let Z = <z1,z2,...,zk> be any LCS of X
and Y.
1. If xm = yn
then zk = xm = yn and Zk-1 is an LCS of Xm-1 and Yn-1.
2. If xm  yn
then zk  xm implies Z is an LCS of Xm-1 and Y.
3. If xm  yn
then zk  yn implies Z is an LCS of X and Yn-1.
131
A recursive solution to
subproblem
 Define c [i, j] is the length of the LCS of Xi
and Yj .

 0 if i=0 or j=0

c[i, j ]   c[i  1, j  1]  1 if i,j>0 and xi=y j
max{c[i, j  1], c[i  1, j ]} if i,j>0 and x  y
 i j

132
Computing the length of an LCS

LCS_LENGTH(X,Y)
1 m  length[X]
2 n  length[Y]
3 for i  1 to m
4 do c[i, 0]  0
5 for j  1 to n
6 do c[0, j]  0
7 for i  1 to m
8 do for j  1 to n

133
9 do if xi = yj
10 then c[i, j]  c[i-1, j-1]+1
11 b[i, j]  “”
12 else if c[i–1, j]  c[i, j-1]
13 then c[i, j]  c[i-1, j]
14 b[i, j]  “”
15 else c[i, j]  c[i, j-1]
16 b[i, j]  “”
17 return c and b

134
Complexity: O(mn)
135
PRINT_LCS
PRINT_LCS(b, X, c, j )
1 if i = 0 or j = 0
2 then return
Complexity: O(m+n)
3 if b[i, j] = “”
4 then PRINT_LCS(b, X, i-1, j-1)
5 print xi
6 else if b[i, j] = “”
7 then PRINT_LCS(b, X, i-1, j)
8 then PRINT_LCS(b, X, i, j-1)

136
Review: Longest Common
Subsequence (LCS)
 Problem: how to find the longest pattern of
characters that is common to two text strings X
and Y
 Dynamic programming algorithm: solve
subproblems until we get the final solution
 Subproblem: first find the LCS of prefixes of X
and Y.
 this problem has optimal substructure: LCS of
two prefixes is always a part of LCS of bigger
strings
137
Review: Longest Common
Subsequence (LCS) continued
 Define Xi, Yj to be prefixes of X and Y of
length i and j; m = |X|, n = |Y|
 We store the length of LCS(Xi, Yj) in c[i,j]
 Trivial cases: LCS(X0 , Yj ) and LCS(Xi, Y0) is
empty (so c[0,j] = c[i,0] = 0 )
 Recursive formula for c[i,j]:

c[i  1, j  1]  1 if x[i ]  y[ j ],
c[i, j ]  
 max(c[i, j  1], c[i  1, j ]) otherwise
c[m,n] is the final solution 138
Review: Longest Common
Subsequence (LCS)
 After we have filled the array c[ ], we can use
this data to find the characters that constitute
the Longest Common Subsequence

 Algorithm runs in O(m*n), which is much


better than the brute-force algorithm: O(n 2m)

139
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 , bi and W are integer values)
 Problem: How to pack the knapsack to achieve
maximum total value of packed items?

140
0-1 Knapsack problem:
a picture
Weight Benefit value
wi bi
Items
3
2
This is a knapsack 3 4
Max weight: W = 20 4 5

5 8
W = 20

9 10
141
0-1 Knapsack problem
 Problem, in other words, is to find
max  bi subject to  wi  W
iT iT
 The problem is called a “0-1” problem,
because each item must be entirely accepted
or rejected.
 Just another version of this problem is the
“Fractional Knapsack Problem”, where we
can take fractions of items.
142
0-1 Knapsack problem: brute-
force approach
Let’s first solve this problem with a
straightforward algorithm
 Since there are n items, there are 2n possible
combinations of items.
 We go through all combinations and find the
one with the most total value and with total
weight less or equal to W
 Running time will be O(2n)
143
0-1 Knapsack problem: brute-
force approach
 Can we do better?
 Yes, with an algorithm based on dynamic
programming
 We need to carefully identify the
subproblems
Let’s try this:
If items are labeled 1..n, then a subproblem
would be to find an optimal solution for
Sk = {items labeled 1, 2, .. k}
144
Defining a Subproblem
If items are labeled 1..n, then a subproblem
would be to find an optimal solution for Sk =
{items labeled 1, 2, .. k}
 This is a valid subproblem definition.
 The question is: can we describe the final
solution (Sn ) in terms of subproblems (Sk)?
 Unfortunately, we can’t do that. Explanation
follows….

145
Defining a Subproblem
w1 =2 w2 =4 w3 =5 w4 =3 Weight Benefit
b1 =3 b2 =5 b3 =8 b4 =4 Item wi bi
? #
1 2 3
Max weight: W = 20 S4 2 3 4
For S4:
S5
Total weight: 14; 3 4 5
total benefit: 20 4 5 8
5 9 10
w1 =2 w2 =4 w3 =5 w4 =9
b1 =3 b2 =5 b3 =8 b4 =10

For S5:
Solution for S4 is
Total weight: 20 not part of the
total benefit: 26 solution for S5!!! 146
Defining a Subproblem
(continued)
 As we have seen, the solution for S4 is not part
of the solution for S5
 So our definition of a subproblem is flawed
and we need another one!
 Let’s add another parameter: w, which will
represent the exact weight for each subset of
items
 The subproblem then will be to compute
B[k,w]
147
Recursive Formula for
subproblems
 Recursive formula for subproblems:
 B[k  1, w] if wk  w
B[k , w]  
max{B[k  1, w], B[k  1, w  wk ]  bk } else
 It means, that the best subset of Sk that has
total weight w is one of the two:
1) the best subset of Sk-1 that has total weight w,
or
2) the best subset of Sk-1 that has total weight w-
wk plus the item k
148
Recursive Formula
 B[k  1, w] if wk  w
B[k , w]  
max{B[k  1, w], B[k  1, w  wk ]  bk } else
 The best subset of Sk that has the total weight
w, either contains item k or not.
 First case: wk>w. Item k can’t be part of the
solution, since if it was, the total weight would
be > w, which is unacceptable
 Second case: wk <=w. Then the item k can be
in the solution, and we choose the case with
greater value
149
0-1 Knapsack Algorithm
for w = 0 to W
B[0,w] = 0
for i = 0 to n
B[i,0] = 0
for w = 0 to W
if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w
150
Running time
for w = 0 to W
O(W)
B[0,w] = 0
for i = 0 to n
Repeat n times
B[i,0] = 0
for w = 0 to W
O(W)
< the rest of the code >

What is the running time of this


algorithm?
O(n*W)
Remember that the brute-force algorithm
takes O(2n) 151
Example

Let’s run our algorithm on the


following data:

n = 4 (# of elements)
W = 5 (max weight)
Elements (weight, benefit):
(2,3), (3,4), (4,5), (5,6)

152
Example
i 0 1 2 3 4
W
0 0
1 0
2 0
3 0
4 0
5 0

for w = 0 to W
B[0,w] = 0

153
Example
i 0 1 2 3 4
W
0 0 0 0 0 0
1 0
2 0
3 0
4 0
5 0

for i = 0 to n
B[i,0] = 0

154
Example (4) Items:
i 1: (2,3)
0 1 2 3 4
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
i=1
1 0 0 4: (5,6)
2 0
bi=3
3 0 wi=2
4 0 w=1
5 0 w-wi =-1
if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w 155
Example (5) Items:
i 1: (2,3)
0 1 2 3 4
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
i=1
1 0 0 4: (5,6)
2 0 3
bi=3
3 0 wi=2
4 0 w=2
5 0 w-wi =0
if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w 156
Example (6) Items:
i 1: (2,3)
0 1 2 3 4
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
i=1
1 0 0 4: (5,6)
2 0 3
bi=3
3 0 3 wi=2
4 0 w=3
5 0 w-wi=1
if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w 157
Example (7) Items:
i 1: (2,3)
0 1 2 3 4
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
i=1
1 0 0 4: (5,6)
2 0 3
bi=3
3 0 3 wi=2
4 0 3 w=4
5 0 w-wi=2
if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w 158
Example (8) Items:
i 1: (2,3)
0 1 2 3 4
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
i=1
1 0 0 4: (5,6)
2 0 3
bi=3
3 0 3 wi=2
4 0 3 w=5
5 0 3 w-wi=2
if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w 159
Example (9) Items:
i 1: (2,3)
0 1 2 3 4
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
i=2
1 0 0 0 4: (5,6)
2 0 3
bi=4
3 0 3 wi=3
4 0 3 w=1
5 0 3 w-wi=-2
if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w 160
Example (10) Items:
i 1: (2,3)
0 1 2 3 4
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
i=2
1 0 0 0 4: (5,6)
2 0 3 3
bi=4
3 0 3 wi=3
4 0 3 w=2
5 0 3 w-wi=-1
if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w 161
Example (11) Items:
i 1: (2,3)
0 1 2 3 4
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
i=2
1 0 0 0 4: (5,6)
2 0 3 3
bi=4
3 0 3 4 wi=3
4 0 3 w=3
5 0 3 w-wi=0
if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w 162
Example (12) Items:
i 1: (2,3)
0 1 2 3 4
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
i=2
1 0 0 0 4: (5,6)
2 0 3 3
bi=4
3 0 3 4 wi=3
4 0 3 4 w=4
5 0 3 w-wi=1
if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w 163
Example (13) Items:
i 1: (2,3)
0 1 2 3 4
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
i=2
1 0 0 0 4: (5,6)
2 0 3 3
bi=4
3 0 3 4 wi=3
4 0 3 4 w=5
5 0 3 7 w-wi=2
if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w 164
Example (14) Items:
i 1: (2,3)
0 1 2 3 4
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
i=3
1 0 0 0 0 4: (5,6)
2 0 3 3 3
bi=5
3 0 3 4 4 wi=4
4 0 3 4 w=1..3
5 0 3 7
if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w 165
Example (15) Items:
i 1: (2,3)
0 1 2 3 4
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
i=3
1 0 0 0 0 4: (5,6)
2 0 3 3 3
bi=5
3 0 3 4 4 wi=4
4 0 3 4 5 w=4
5 0 3 7 w- wi=0
if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w 166
Example (15) Items:
i 1: (2,3)
0 1 2 3 4
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
i=3
1 0 0 0 0 4: (5,6)
2 0 3 3 3
bi=5
3 0 3 4 4 wi=4
4 0 3 4 5 w=5
5 0 3 7 7 w- wi=1
if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w 167
Example (16) Items:
i 1: (2,3)
0 1 2 3 4
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
i=3
1 0 0 0 0 0 4: (5,6)
2 0 3 3 3 3
bi=5
3 0 3 4 4 4 wi=4
4 0 3 4 5 5 w=1..4
5 0 3 7 7
if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w 168
Example (17) Items:
i 1: (2,3)
0 1 2 3 4
W 2: (3,4)
0 0 0 0 0 0 3: (4,5)
i=3
1 0 0 0 0 0 4: (5,6)
2 0 3 3 3 3
bi=5
3 0 3 4 4 4 wi=4
4 0 3 4 5 5 w=5
5 0 3 7 7 7
if wi <= w // item i can be part of the solution
if bi + B[i-1,w-wi] > B[i-1,w]
B[i,w] = bi + B[i-1,w- wi]
else
B[i,w] = B[i-1,w]
else B[i,w] = B[i-1,w] // wi > w 169
Comments

 This algorithm only finds the max possible


value that can be carried in the knapsack
 To know the items that make this maximum
value, an addition to this algorithm is
necessary
 Please see LCS algorithm from the previous
lecture for the example how to extract this data
from the table we built

170
Conclusion
 Dynamic programming is a useful technique of
solving certain kind of problems
 When the solution can be recursively described
in terms of partial solutions, we can store these
partial solutions and re-use them as necessary
 Running time (Dynamic Programming
algorithm vs. naïve algorithm):
 LCS: O(m*n) vs. O(n * 2m)
 0-1 Knapsack problem: O(W*n) vs. O(2n)

171
The End

172

Das könnte Ihnen auch gefallen