COMS 482: unofficial class blog


Lecture 9: Dynamic Programming

Posted in Class Notes by Elliott Back on February 11th, 2005. [Del.icio.us]

Continuing the theme from last lecture, let M1, … , Mn be a series of matrices, and r0, … , rn be a series of dimensions such that Mi is an ri-1 * ri matrix.

Our goal is to compute the multiplication of all M:

best(i, j) = mink bestk(i, j)
bestk(i, j) = best(i, k) + best(k, j) + ri-1 * rk * rj

Using these rules, we can carefully fill a lower triangular table (being careful of the order of filling):

Matrix Cost/Order Algorithm:

For i = 1 to n
    B[i, i] = 0
EndFor

For gap = 1 to (n-1)
    for i = 1 to n - gap
        j = i + gap
        cost = mink {B[i, k] + B[k+1, j] + ri-1 * rk * rj}
        B[i, j] = cost
    EndFor
End For

return B[1,n]

  • Need to remember choices!
  • Whenever we assign B[i, j] we know the best k
  • Use another array Mem[i, j] = best k found when doing B[i, j]
  • Mem(1, n) tells where to split M1, … , Mn

Dividing a polygon into triangles:

This was once a very popular optimization technique for 3d rendering (and especially games) before graphics hardware supported complex shapes–triangles were fast to draw:

Surface Division w/ Triangles

  • Find the minimum weight of triangulation
  • weight = sum of the lengths of new edges
  • Polygon w/ vertices v1, … , vn– assume counterclockwise.
  • D[i, j] = distance from vi to vj, unless outside the polygon, in which case the distance is infinite
  • C(i, j) = minimum cost for triangulating polygon v1, … , vn
  • Ck(1, n) = best answer using vertex k for triangle with v1 and vn
  • C(1, n) = min Ck(1, n)
  • Ck(1, n) = C(k, n) + C(1, k) + D[k, n] + D[1, k]

Polygon Triangulating Algorithm:

For i = 1 to n
    C[i, i+1] = -D[i, i+1]
EndFor

For gap = 2 to n -1
    for i = 1 to n - gap
        j = i + gap
        C[i, j] = mink {C[i, k] + C[k, j] + D[i, k] + D[k, j]}
    EndFor
EndFor

return C[1, n]

This entry was posted on Friday, February 11th, 2005 at 1:23 pm and is tagged with , , , , , , , , , , , , , , , , , , , . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback.

Leave a Reply

Please take time to enjoy the archives: May 2005 (1) April 2005 (11) March 2005 (11) February 2005 (15) January 2005 (7)

Fresh, related resources:

Supplied by Google Blog Search