Ch 5 Dynamic Programming - PowerPoint PPT Presentation

1 / 34
About This Presentation
Title:

Ch 5 Dynamic Programming

Description:

How do we use the minimum number of coins to make change for 8 dollars with ... That is to say, the global optimum comes from the optimums of all sub-problems. ... – PowerPoint PPT presentation

Number of Views:63
Avg rating:3.0/5.0
Slides: 35
Provided by: 14077
Category:

less

Transcript and Presenter's Notes

Title: Ch 5 Dynamic Programming


1
Ch 5 Dynamic Programming
2
Making change
  • How do we use the minimum number of coins to make
    change for 8 dollars with coins 1, 4, and 6
    dollars?
  • Using greedy method, we can make change for 8
    dollars with 1 6-dollar and 2 1-dollars, which
    needs 3 coins.
  • However, we can make change for 8 dollars with 2
    4-dollars.
  • Therefore, greedy method can not guarantee to
    derive the minimum number of coins for this
    problem.

3
Brute-and-Force Method
4
Using Dynamic Programming for Making Change
  • ci,j the minimum number of coins required to
    pay an amount of j dollars using only coins d1,
    d2, , di.
  • ci,j min( ci-1,j, 1ci,j-di )
  • Amount 0 1 2 3 4 5 6 7
    8
  • d1 1 0 1 2 3 4 5 6 7
    8
  • d2 4 0 1 2 3 1 2 3 4
    2
  • d3 6 0 1 2 3 1 2 1 2
    2

1
1
c2,6 min c1,6, 1c2,2 min 6, 12
3 c3,8 min c2,8, 1c3,2 min 2,
12 2
5
Using Dynamic Programming for Making Change
Function coins(N) N dollars for making change
d1..n the dollars of coin i c1..n,0..m
the minimum number of coins for i ? 1 to n do
ci,0 ? 0 for i ? 1 to n do for j ? 1
to m do ci,j ? if i 1 and j lt
di then 8 else if i 1 then 1
c1, j d1 else if j lt di then
ci-1,j else min( ci - 1, j, 1
ci,j - di )
6
Multistage Graphs P254, P257
  • Principle of optimality (p254)
  • Exhaustive search can guarantee to find an
    optimal solution.
  • However, dynamic programming finds optimal
    solutions for all scales of sub-problems and
    finally find an optimal solution.
  • That is to say, the global optimum comes from the
    optimums of all sub-problems.
  • A multistage is a directed graph in which the
    vertices are partitioned into k ? 2 disjoint
    sets.
  • The multistage graph problem is to find a
    minimum-cost path from s to t.

7
Multistage Graphs P259, Figure 5.2
Let cost(i,j) be the cost of a minimum-cost
path from vertex j in Vi to destination vertex t.
8
stage i
stage i1
.
l
j
t
7 min 47, 25, 17
7 min 46, 25
4
9
16
5
2
18
7
5
15
9
P259 P261
stage i
stage i1
.
l
j
t
10
P262, Algorithm 5.1
11
Multistage Graphs
  • The time for the for loop of line 7 is T(V
    E), and the time for the for loop of line 16 is
    T(k), Hence, the total time is T(V E).
  • The backward trace from vertex 1 to n also works.
  • The algorithm also works for the edges crossing
    more than 1 stage.

12
Efficient allocation of chain-like tasks on
chain-like network computers
  • Jang-Ping SHEU
  • Zen-Fu CHIANG
  • Information Processing Letters 36 (1990)241-245

13
Introduction
  • The purpose of task allocation on distributed
    computing systems is to reduce the task
    turnaround time by increasing the system
    throughput.
  • Assumption
  • cost for computation is known
  • communication cost is known
  • linear precedence relationship
  • Let be the task
    turnaround time of allocation A, then

tasks or modules
system or processors
14
Notation
  • m the number of modules
  • n the number of processors
  • ei the computation cost for module i
  • cj,j1 the communication cost between modules j
    and j1 if modules j and j 1 are allocated to
    different processors
  • m, n, ei, and cj,j1 are known

15
Allocation of a nine-module chain-like task on a
four-processor chain-like network computer
  • m 9, n 4
  • The turnaround time of processor 2 for task
    allocation as shown in the above figure is equal
    to

16
Bokharis task allocation graph
  • A node labeled lth,igt is connected to all nodes
    lti1,jgt in the layer below it.
  • If a path contains the node lth,igt of layer k,
    this represents the assignment of modules h
    through i to processor k.
  • This method expands all possible solutions in the
    allocation graph and there are a lot of paths and
    redundant nodes in the graph.
  • A path s ? lt1,1gt ? lt2,3gt ? lt4,6gt
  • lt7,9gt ? d represents the possible
  • allocation depicted in the previous page

17
The improved task allocation graph
  • m-k1 nodes
  • The edge that connects node i in layer k and node
    j in layer k 1 represents the assignment of
    modules i 1 through j to processor k.
  • The weight of edge joining node i in layer k and
    j in layer k 1 equals the sum of all
    computation costs of modules i 1 through j and
    communication costs between modules i and i 1,
    and between modules j and j 1.

18
Alogrithm(1/5)
  • Example m 4, n 4
  • Step 1
  • 1. There is one distinguished node called the
    source node, denoted s.
  • 2. There are min(m,n) layers in the task
    allocation graph.

5
2
2
3
19
Alogrithm(2/5)
  • 3. The kth layer consists of m-k1 nodes and
    sequence numbers k, k1, , m is labeled at each
    node from left to right.
  • 4. A node Labeled i in layer k is connected to
    all nodes, in layer k 1, labeled number greater
    than i. All nodes in the first layer are
    connected to node s.

20
Alogrithm(3/5)
5
2
2
3
  • 5. Each edge joining source node s to node j, for
    1?j?m, has weight
  • 6. Each edge joining node i in layer k to node j
    in layer k 1 has weight

21
Alogrithm(4/5)
  • Step 2
  • Initially all nodes in the allocation graph are
    given infinite labels except the source node,
    where it is labeled zero.

5
2
2
3
L(s) 0
L(1) 8
22
Alogrithm(5/5)
  • Step 3.
  • For k 1 to min(m,n) do
  • L(j)minL(j), max(W(i,j), L(i))
  • where L(j) is the label of node j and W(i,j) is
    the weight of the edge connecting from i to j.

5
2
2
3
8
11
11
12
9
10
9
10 min8,max8,10 10 min10,max11,9 10
min10,max11,6
Task turnaround time table
9
9
9
The method avoids a lot of computations of
redundant paths and nodes
23
P268, Example 5.15 Figure 5.6
We requires that G has no cycles with negative
length
24
All Pairs Shortest PathsP267, Algorithm 5.3
O(n3)
25
Single-Source Shortest Paths General
WeightsP271 P272, Figure 5.10
Let distku be the length of a shortest path
from the source vertex v to vertex u containing
at most k edges.
distk-1(u)
..
i
v
...
u
cost(i,u)
distk-1(i)
We requires that G has no cycles with negative
length
26
P273, Algorithm 5.4
O(n3) for adjacency matrix and O(ne) for
adjacency list
27
String EditingP284
28
Let cost(i,j) be the minimum cost of edit
sequence for transforming string x1, x2, , xi
into y1, y2, , yj.
29
cost(0,1) D(x1) x1 a ? D(x1a) ? F ?
cost(0,1) I(y1b) ? b y1 cost(1,0) I(y1)
x1 a ? I(y1b) ? ab ? cost(1,0) D(x1a) ? b
y1
cost(0,2) D(x1) x1 a ? D(x1 a) ? F ?
cost(0,2) I(y1y2 ba) ? ba y1 y2 cost(0,1)
C(x1a,y2 a) x1 a ? C(x1a,y2 a)0 ? a ?
cost(0,1) I(y1a) ? ba y1 y2
Y
b
a
b
b
X
C(xi,y2j) 2 or 0
2
1
D(xj) 1
a
1
a
I(yi) 1
b
The time complexity is O(mn) where m and n denote
the length of strings X and Y respectively.
a
b
30
0-1 Knapsack P287
0-1 knapsack where xi 0 or 1
For example m 11, using greedy method we will
choose objects 5, 2, and 1 with total profit
35. i 1 2 3 4 5 p 1 6
18 22 28 w 1 2 5 6 7
31
0-1 Knapsack
  • Let Pi,j be the maximum profit using object 1,
    2, , i, with knapsacks capacity j.
  • Pi,j max( Pi 1,j, Pi 1, j wi pi )
  • Let the knapsacks capacity m 11.
  • Weight limit 0 1 2 3 4 5 6
    7 8 9 10 11
  • w11, p11 0 1 1 1 1 1 1 1
    1 1 1 1
  • w22, p26 0 1 6 7 7 7 7 7
    7 7 7 7
  • w35, p218 0 1 6 7 7 18 19 24
    25 25 25 25
  • w46, p422 0 1 6 7 7 18 22 24
    28 29 29 40
  • w57, p528 0 1 6 7 7 18 22 28
    29 34 35 40
  • P5,11 max( P4,11, P4,428 ) max( 40,
    728) 40
  • P4,11 max( P3,11, P3,522 ) max( 25,
    1822) 40
  • P3,5 max( P2,5, P2,06 ) max( 7, 018)
    18

Time complexity is O(mn)
32
Traveling Salesperson ProblemP298
  • A salesperson would like to travel from one city
    to the other (n 1) cities just once then back
    to the original city, what is the minimum
    distance for this travel?
  • The brute-and-force method is trying all possible
    (n 1)! permutations of (n 1) cities and
    picking the path with the minimum distance.
  • There are a lot of redundant computations for the
    brute-and-force method such as the permutations
    of 6 cities are 1234561, , 1243561, ., 1324561,
    , 1342561, , 1423561, , 1432561,

33
Traveling Salesperson ProblemP299
  • The method computes from vertex 1 backward to the
    other vertices then return to vertex 1.
  • Let g(i,S) be the length of a shortest path
    starting at vertex i, going through all vertices
    in S, and terminating at vertex 1.

34
Compute from vertex 1 backward to the other
vertices then return to vertex 1
Write a Comment
User Comments (0)
About PowerShow.com