To Read Dynprog2
To Read Dynprog2
To Read Dynprog2
Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill
Dynamic Programming
A1 A2 A3 A4 A5 A6 A7 A8 A9
m[ 3 , 7 ]
• This suggests the following recursive rule for computing m[i, j]:
m[i, i] = 0
m[i, j] = mini k < j (m[i, k] + m[k+1, j] + pi-1pkpj ) for i < j
Computing m[i, j]
• For a specific k,
1. if (j > i)
2. then k = s[i, j]
3. X = Mult(A, i, k) // X = A[i]...A[k]
4. Y = Mult(A, k+1, j)// Y = A[k+1]...A[j]
5. return X*Y // Multiply X*Y
6. else return A[i] // Return ith matrix
Example: DP for CMM
• The initial set of dimensions are <5, 4, 6, 2, 7>: we are
multiplying A1 (5x4) times A2 (4x6) times A3 (6x2) times A4
(2x7). Optimal sequence is (A1 (A2A3 )) A4.
Finding a Recursive Solution
• Figure out the “top-level” choice you have to make (e.g., where to
split the list of matrices)
• List the options for that decision
• Each option should require smaller sub-problems to be solved
• Recursive function is the minimum (or max) over all the options
( ,
Fastest time
Fastest time through prev
to any given
station =min station (same
line)
Fastest time
)
Time it
through prev
station (other + takes to
switch lines
line)
Recursive Formula (II)
• Let fi [ j] denote the fastest possible time to get the chassis through S i, j
• Have the following formulas:
f1[ 1] = e1 + a1,1
f1[ j] = min( f1[ j-1] + a1, j, f2 [ j-1]+t2, j-1+ a1, j )
• Total time:
f * = min( f1[n] + x1, f2 [ n]+x2)
Analysis
• Only loop is lines 3-13 which iterate n-1 times: Algorithm is O(n).
• The array l records which line is used for each station number
Example
Polygons
• A polygon is a piecewise linear closed curve in the plane. We
form a cycle by joining line segments end to end. The line
segments are called the sides of the polygon and the endpoints
are called the vertices.
• A polygon is simple if it does not cross itself, i.e. if the edges do
not intersect one another except for two consecutive edges
sharing a common vertex. A simple polygon defines a region
consisting of points it encloses. The points strictly within this
region are in the interior of this region, the points strictly on the
outside are in its exterior, and the polygon itself is the boundary
of this region.
Convex Polygons
• A simple polygon is said to be convex if given any two points on
its boundary, the line segment between them lies entirely in the
union of the polygon and its interior.
• Convexity can also be defined by the interior angles. The
interior angles of vertices of a convex polygon are at most 180
degrees.
Triangulations
• Given a convex polygon, assume that its vertices are labeled in
counterclockwise order P=<v0,…,vn-1>. Assume that indexing of
vertices is done modulo n, so v0 = vn. This polygon has n sides,
(vi-1 ,vi ).
• Given two nonadjacent vj , where i < j, the line segment (vi ,vj ) is
a chord. (If the polygon is simple but not convex, a segment
must also lie entirely in the interior of P for it to be a chord.)
Any chord subdivides the polygon into two polygons.
• A triangulation of a convex polygon is a maximal set T of
chords. Every chord that is not in T intersects the interior of
some chord in T. Such a set of chords subdivides interior of a
polygon into set of triangles.
Example: Polygon Triangulation
• The associated binary tree has n leaves, and hence n-1 internal
nodes. Since each internal node other than the root has one edge
entering it, there are n-2 edges between the internal nodes.
Lemma
v1
DP Solution (II)
• For the basis case, the weight of the trivial 2-sided polygon is
zero, implying that t[i, i] = 0 (line (vi-1, vi)).
DP Solution (III)
• In general, to compute t[i, j], consider the subpolygon <vi-1, vi ,
…, vj>, where i j. One of the chords of this polygon is the side
(vi-1, vj). We may split this subpolygon by introducting a triangle
whose base is this chord, and whose third vertex is any vertex
vk, where i k j-1. This subdivides the polygon into 2
subpolygons <vi-1,...vk> & <vk+1,... vj>, whose minimum weights
are t[i, k] and t[k+1, j].
• We have following recursive rule for computing t[i, j]:
t[i, i] = 0
t[i, j] = mini k j-1 (t[i, k] + t[k+1, j] + w(vi-1vkvj )) for i < k
Weighted-Polygon-Triangulation(V)
1. n length[V] - 1 // V = <v0 ,v1 ,…,vn>
2. for i 1 to n // initialization: O(n) time
3. do t[i, i] 0
4. for L 2 to n // L = length of sub-chain
5. do for i 1 to n-L+1
6. do j i + L - 1
7. t[i, j]
8. for k i to j - 1
9. do q t[i, k] + t[k+1, j] + w(vi-1 , vk , vj)
10. if q < t[i, j]
11. then t[i, j] q
12. s[i, j] k
13. return t and s