80 likes | 249 Views
Dynamic Programming (16.0). The 3-d Paradigm 1st = Divide and Conquer 2nd = Greedy Algorithm Dynamic Programming = metatechnique (not a particular algorithm) Programming = ``tableau method’’, not writing a code. Longest Common Subsequence (16.3).
E N D
Dynamic Programming (16.0) • The 3-d Paradigm • 1st = Divide and Conquer • 2nd = Greedy Algorithm • Dynamic Programming = metatechnique (not a particular algorithm) • Programming = ``tableau method’’, not writing a code
Longest Common Subsequence (16.3) • Problem: Given x[1..m] and y[1..n], find LCS x: A B C B D A B B C B A y: B D C A B A • Brute-force algorithm: • for every subsequence of x check if it is in y • O(n2m ) time • 2m subsequences of x (each element either in or out • O(n) for scanning y with x-subsequence (m n)
Recurrent Formula for LCS (16.3) • Let c[i,j] = length of LCS of X[i]=x[1..i],Y[j]= y[1..j] • Then c[m,n] = length of LCS of x and y • Theorem: if x[i] = y[j] otherwise • Proof. x[i] = y[j] LCS([X[i],Y[j]) = LCS(X[i-1],Y[j-1]) + x[i]
DP properties(16.2) • 1st Observation: any part of the optimal answer is also optimal • The subsequence of LCS(X,Y) is LCS for some subsequences of X and Y. • 2nd Observation: subproblems overlap • LCS(X[m],Y[n-1]) and LCS(X[m-1],Y[n]) has common subproblem LCS(X[m-1],Y[n-1]) • There are few problems in total = mn for LCS • Unlike divide and conquer
DP (16.2) • After computing solution of a subproblem, store in table • Time = O(mn) • When computing c[i,j] we need O(1) time if we have: • x[i], y[j] • c[i,j-1] • c[i-1,j] • c[i-1,j-1]
DP Table for LCS y B D C A B A x A B C B D A B
Optimal Polygon Triangulation (16.4) • Polygon has sides and vertices • Polygon is simple = not self-intersecting. • Polygon P is convex if any segment with ends in P belong to P • Triangulation of P is partition of P with chords into triangles. • Problem: Given a convexpolygon and weight function defined on triangles (e.g. the perimeter). Find triangulation of minimum weight (of minimum total length). • # of triangles = n - 2 triangles with n-3 chords
Optimal Polygon Triangulation (16.4) • Optimal sub-triangulation of optimal triangulation • Recurrent formula: t[i,j] = the weight of the optimal triangulation of the polygon <v[i-1],v[i],...,v[j]>; If i=j, then t[i,j]=0; else • Runtime O(n3) and space is O(n2) v[i] v[i] v[i-1] v[i-1] v[j] v[j] v[k] v[k]