dynamic programming (16.0/15) the 3-d paradigm 1st = divide and conquer 2nd = greedy algorithm...

9
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

Upload: gilbert-shields

Post on 27-Dec-2015

217 views

Category:

Documents


2 download

TRANSCRIPT

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

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

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

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(n2m ) time

• 2m subsequences of x (each element either in or out

• O(n) for scanning y with x-subsequence (m n)

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

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]

]),1[],1,[max(

1]1,1[],[

jicjic

jicjic

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

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

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

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]

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

DP Table for LCS

y B D C A B A

x

A

B

C

B

D

A

B

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

DP Table for LCS

y B D C A B A

x

A

B

C

B

D

A

B

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

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

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

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

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)

])[][]1[(],1[],[{min],[1

jvkvivwjktkitjitjki

v[i-1]

v[i]

v[j]

v[i-1]

v[i]

v[j]v[k] v[k]