Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dynamic Programming (16.0/15) The 3-d Paradigm 1st = Divide and Conquer 2nd = Greedy Algorithm Dynamic Programming = metatechnique (not a particular algorithm)

Similar presentations


Presentation on theme: "Dynamic Programming (16.0/15) The 3-d Paradigm 1st = Divide and Conquer 2nd = Greedy Algorithm Dynamic Programming = metatechnique (not a particular algorithm)"— Presentation transcript:

1 Dynamic Programming (16.0/15) 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

2 Longest Common Subsequence (16.3/15.4) 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(n2 m ) time 2 m subsequences of x (each element either in or out O(n) for scanning y with x-subsequence (m  n)

3 Recurrent Formula for LCS (16.3/15.4) 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]

4 DP properties(16.2/15.3) 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

5 DP (16.2/15.3) 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]

6 DP Table for LCS y B D C A B A xABCBDABxABCBDAB

7 DP Table for LCS y B D C A B A xABCBDABxABCBDAB 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 1 1 1 1 2 2 0 1 1 2 2 2 2 0 1 1 2 2 3 3 0 1 2 2 2 3 3 0 1 2 2 3 3 4 0 1 2 2 3 4 4

8 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 convex polygon 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

9 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 ; If i=j, then t[i,j]=0; else Runtime O(n 3 ) and space is O(n 2 ) v[i-1] v[i] v[j] v[i-1] v[i] v[j] v[k]


Download ppt "Dynamic Programming (16.0/15) The 3-d Paradigm 1st = Divide and Conquer 2nd = Greedy Algorithm Dynamic Programming = metatechnique (not a particular algorithm)"

Similar presentations


Ads by Google