lecture 12: dynamic programming ii

69
Lecture 12: Dynamic Programming II Michael Dinitz October 7, 2021 601.433/633 Introduction to Algorithms Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 1 / 23

Upload: others

Post on 30-Dec-2021

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 12: Dynamic Programming II

Lecture 12: Dynamic Programming II

Michael Dinitz

October 7, 2021

601.433/633 Introduction to Algorithms

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 1 / 23

Page 2: Lecture 12: Dynamic Programming II

Introduction

Today: two more examples of dynamic programming

� Longest Common Subsequence (strings)

� Optimal Binary Search Tree (trees)

Important problems, but really: more examples of dynamic programming

Both in CLRS (unlike Weighted Interval Scheduling)

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 2 / 23

Page 3: Lecture 12: Dynamic Programming II

Longest Common Subsequence

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 3 / 23

Page 4: Lecture 12: Dynamic Programming II

Definitions

String: Sequence of elements of some alphabet ({0,1}, or {A −Z} ∪ {a − z}, etc.)Definition: A sequence Z = (z1, . . . , zk) is a subsequence of X = (x1, . . . ,xm) if thereexists a strictly increasing sequence (i1, i2, . . . , ik) such that xij = zj for all j ∈ {1,2, . . . ,k}.

Example: (B,C,D,B) is a subsequence of (A,B,C,B,D,A,B)� Allowed to skip positions, unlike substring!

Definition: In Longest Common Subsequence problem (LCS) we are given two strings

X = (x1, . . . ,xm) and Y = (y1, . . .yn). Need to find the longest Z which is a subsequence

of both X and Y.

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 4 / 23

Page 5: Lecture 12: Dynamic Programming II

Definitions

String: Sequence of elements of some alphabet ({0,1}, or {A −Z} ∪ {a − z}, etc.)Definition: A sequence Z = (z1, . . . , zk) is a subsequence of X = (x1, . . . ,xm) if thereexists a strictly increasing sequence (i1, i2, . . . , ik) such that xij = zj for all j ∈ {1,2, . . . ,k}.

Example: (B,C,D,B) is a subsequence of (A,B,C,B,D,A,B)� Allowed to skip positions, unlike substring!

Definition: In Longest Common Subsequence problem (LCS) we are given two strings

X = (x1, . . . ,xm) and Y = (y1, . . .yn). Need to find the longest Z which is a subsequence

of both X and Y.

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 4 / 23

Page 6: Lecture 12: Dynamic Programming II

Subproblems

First and most important step of dynamic programming: define subproblems!

� Not obvious: X and Y might not even be same length!

Prefixes of strings

� Xi = (x1,x2, . . . ,xi) (so X = Xm)

� Yj = (y1,y2, . . . ,yj) (so Y = Yn)

Definition: Let OPT(i, j) be longest common subsequence of Xi and Yj

So looking for optimal solution OPT = OPT(m,n)� Last time OPT denotes value of solution, here denotes solution. Be flexible in notation

Two-dimensional table!

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 5 / 23

Page 7: Lecture 12: Dynamic Programming II

Subproblems

First and most important step of dynamic programming: define subproblems!

� Not obvious: X and Y might not even be same length!

Prefixes of strings

� Xi = (x1,x2, . . . ,xi) (so X = Xm)

� Yj = (y1,y2, . . . ,yj) (so Y = Yn)

Definition: Let OPT(i, j) be longest common subsequence of Xi and Yj

So looking for optimal solution OPT = OPT(m,n)� Last time OPT denotes value of solution, here denotes solution. Be flexible in notation

Two-dimensional table!

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 5 / 23

Page 8: Lecture 12: Dynamic Programming II

Subproblems

First and most important step of dynamic programming: define subproblems!

� Not obvious: X and Y might not even be same length!

Prefixes of strings

� Xi = (x1,x2, . . . ,xi) (so X = Xm)

� Yj = (y1,y2, . . . ,yj) (so Y = Yn)

Definition: Let OPT(i, j) be longest common subsequence of Xi and Yj

So looking for optimal solution OPT = OPT(m,n)� Last time OPT denotes value of solution, here denotes solution. Be flexible in notation

Two-dimensional table!

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 5 / 23

Page 9: Lecture 12: Dynamic Programming II

Subproblems

First and most important step of dynamic programming: define subproblems!

� Not obvious: X and Y might not even be same length!

Prefixes of strings

� Xi = (x1,x2, . . . ,xi) (so X = Xm)

� Yj = (y1,y2, . . . ,yj) (so Y = Yn)

Definition: Let OPT(i, j) be longest common subsequence of Xi and Yj

So looking for optimal solution OPT = OPT(m,n)� Last time OPT denotes value of solution, here denotes solution. Be flexible in notation

Two-dimensional table!

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 5 / 23

Page 10: Lecture 12: Dynamic Programming II

Optimal Substructure

Second step of dynamic programming: prove optimal substructure

� Relationship between subproblems: show that solution to subproblem can be found from

solutions to smaller subproblems

Theorem

Let Z = (z1, . . . , zk) be an LCS of Xi and Yj (so Z = OPT(i, j)).1. If xi = yj: then zk = xi = yj and Zk−1 = OPT(i − 1, j − i)2. If xi ≠ yj and zk ≠ xi: then Z = OPT(i − 1, j)3. If xi ≠ yj and zk ≠ yj: then Z = OPT(i, j − 1)

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 6 / 23

Page 11: Lecture 12: Dynamic Programming II

Optimal Substructure

Second step of dynamic programming: prove optimal substructure

� Relationship between subproblems: show that solution to subproblem can be found from

solutions to smaller subproblems

Theorem

Let Z = (z1, . . . , zk) be an LCS of Xi and Yj (so Z = OPT(i, j)).1. If xi = yj:

then zk = xi = yj and Zk−1 = OPT(i − 1, j − i)2. If xi ≠ yj and zk ≠ xi: then Z = OPT(i − 1, j)3. If xi ≠ yj and zk ≠ yj: then Z = OPT(i, j − 1)

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 6 / 23

X ToMe

Page 12: Lecture 12: Dynamic Programming II

Optimal Substructure

Second step of dynamic programming: prove optimal substructure

� Relationship between subproblems: show that solution to subproblem can be found from

solutions to smaller subproblems

Theorem

Let Z = (z1, . . . , zk) be an LCS of Xi and Yj (so Z = OPT(i, j)).1. If xi = yj: then zk = xi = yj and Zk−1 = OPT(i − 1, j − i)

2. If xi ≠ yj and zk ≠ xi: then Z = OPT(i − 1, j)3. If xi ≠ yj and zk ≠ yj: then Z = OPT(i, j − 1)

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 6 / 23

at

Page 13: Lecture 12: Dynamic Programming II

Optimal Substructure

Second step of dynamic programming: prove optimal substructure

� Relationship between subproblems: show that solution to subproblem can be found from

solutions to smaller subproblems

Theorem

Let Z = (z1, . . . , zk) be an LCS of Xi and Yj (so Z = OPT(i, j)).1. If xi = yj: then zk = xi = yj and Zk−1 = OPT(i − 1, j − i)2. If xi ≠ yj and zk ≠ xi:

then Z = OPT(i − 1, j)3. If xi ≠ yj and zk ≠ yj: then Z = OPT(i, j − 1)

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 6 / 23

f

Page 14: Lecture 12: Dynamic Programming II

Optimal Substructure

Second step of dynamic programming: prove optimal substructure

� Relationship between subproblems: show that solution to subproblem can be found from

solutions to smaller subproblems

Theorem

Let Z = (z1, . . . , zk) be an LCS of Xi and Yj (so Z = OPT(i, j)).1. If xi = yj: then zk = xi = yj and Zk−1 = OPT(i − 1, j − i)2. If xi ≠ yj and zk ≠ xi: then Z = OPT(i − 1, j)

3. If xi ≠ yj and zk ≠ yj: then Z = OPT(i, j − 1)

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 6 / 23

Page 15: Lecture 12: Dynamic Programming II

Optimal Substructure

Second step of dynamic programming: prove optimal substructure

� Relationship between subproblems: show that solution to subproblem can be found from

solutions to smaller subproblems

Theorem

Let Z = (z1, . . . , zk) be an LCS of Xi and Yj (so Z = OPT(i, j)).1. If xi = yj: then zk = xi = yj and Zk−1 = OPT(i − 1, j − i)2. If xi ≠ yj and zk ≠ xi: then Z = OPT(i − 1, j)3. If xi ≠ yj and zk ≠ yj:

then Z = OPT(i, j − 1)

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 6 / 23

Page 16: Lecture 12: Dynamic Programming II

Optimal Substructure

Second step of dynamic programming: prove optimal substructure

� Relationship between subproblems: show that solution to subproblem can be found from

solutions to smaller subproblems

Theorem

Let Z = (z1, . . . , zk) be an LCS of Xi and Yj (so Z = OPT(i, j)).1. If xi = yj: then zk = xi = yj and Zk−1 = OPT(i − 1, j − i)2. If xi ≠ yj and zk ≠ xi: then Z = OPT(i − 1, j)3. If xi ≠ yj and zk ≠ yj: then Z = OPT(i, j − 1)

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 6 / 23

Page 17: Lecture 12: Dynamic Programming II

Optimal Substructure: Proof (I)

Case 1: If xi = yj, then zk = xi = yj and Zk−1 = OPT(i − 1, j − i)Proof Sketch.

Contradiction.

Part 1: Suppose xi = yj = a, but zk ≠ a. Add a to end of Z, still have LCS, longer than longest

LCS. Contradiction

Part 2: Suppose Zk−1 ≠ OPT(i − 1, j − 1).�⇒ ∃W LCS of Xi−1,Yj−1 of length > k − 1 �⇒ ≥ k�⇒ (W,a) common subsequence of Xi,Yj of length > k

� Contradiction to Z being LCS of Xi and Yj

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 7 / 23

Page 18: Lecture 12: Dynamic Programming II

Optimal Substructure: Proof (I)

Case 1: If xi = yj, then zk = xi = yj and Zk−1 = OPT(i − 1, j − i)Proof Sketch.

Contradiction.

Part 1: Suppose xi = yj = a, but zk ≠ a.

Add a to end of Z, still have LCS, longer than longest

LCS. Contradiction

Part 2: Suppose Zk−1 ≠ OPT(i − 1, j − 1).�⇒ ∃W LCS of Xi−1,Yj−1 of length > k − 1 �⇒ ≥ k�⇒ (W,a) common subsequence of Xi,Yj of length > k

� Contradiction to Z being LCS of Xi and Yj

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 7 / 23

Page 19: Lecture 12: Dynamic Programming II

Optimal Substructure: Proof (I)

Case 1: If xi = yj, then zk = xi = yj and Zk−1 = OPT(i − 1, j − i)Proof Sketch.

Contradiction.

Part 1: Suppose xi = yj = a, but zk ≠ a. Add a to end of Z, still have LCS, longer than longest

LCS. Contradiction

Part 2: Suppose Zk−1 ≠ OPT(i − 1, j − 1).�⇒ ∃W LCS of Xi−1,Yj−1 of length > k − 1 �⇒ ≥ k�⇒ (W,a) common subsequence of Xi,Yj of length > k

� Contradiction to Z being LCS of Xi and Yj

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 7 / 23

q

Michael Dinitz
Page 20: Lecture 12: Dynamic Programming II

Optimal Substructure: Proof (I)

Case 1: If xi = yj, then zk = xi = yj and Zk−1 = OPT(i − 1, j − i)Proof Sketch.

Contradiction.

Part 1: Suppose xi = yj = a, but zk ≠ a. Add a to end of Z, still have LCS, longer than longest

LCS. Contradiction

Part 2: Suppose Zk−1 ≠ OPT(i − 1, j − 1).

�⇒ ∃W LCS of Xi−1,Yj−1 of length > k − 1 �⇒ ≥ k�⇒ (W,a) common subsequence of Xi,Yj of length > k

� Contradiction to Z being LCS of Xi and Yj

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 7 / 23

Page 21: Lecture 12: Dynamic Programming II

Optimal Substructure: Proof (I)

Case 1: If xi = yj, then zk = xi = yj and Zk−1 = OPT(i − 1, j − i)Proof Sketch.

Contradiction.

Part 1: Suppose xi = yj = a, but zk ≠ a. Add a to end of Z, still have LCS, longer than longest

LCS. Contradiction

Part 2: Suppose Zk−1 ≠ OPT(i − 1, j − 1).�⇒ ∃W LCS of Xi−1,Yj−1 of length > k − 1 �⇒ ≥ k�⇒ (W,a) common subsequence of Xi,Yj of length > k

� Contradiction to Z being LCS of Xi and Yj

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 7 / 23

Page 22: Lecture 12: Dynamic Programming II

Optimal Substructure: Proof (II)

Case 2: If xi ≠ yj and zk ≠ xi then Z = OPT(i − 1, j)

Proof.

Since zk ≠ xi, Z a common subsequence of Xi−1,Yj

OPT(i − 1, j) a common subsequence of Xi,Yj�⇒ �OPT(i − 1, j)� ≤ �OPT(i, j)� = �Z� (def of OPT(i, j) and Z)

�⇒ Z = OPT(i − 1, j)

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 8 / 23

Page 23: Lecture 12: Dynamic Programming II

Optimal Substructure: Proof (II)

Case 2: If xi ≠ yj and zk ≠ xi then Z = OPT(i − 1, j)Proof.

Since zk ≠ xi, Z a common subsequence of Xi−1,Yj

OPT(i − 1, j) a common subsequence of Xi,Yj�⇒ �OPT(i − 1, j)� ≤ �OPT(i, j)� = �Z� (def of OPT(i, j) and Z)

�⇒ Z = OPT(i − 1, j)

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 8 / 23

Page 24: Lecture 12: Dynamic Programming II

Optimal Substructure: Proof (II)

Case 2: If xi ≠ yj and zk ≠ xi then Z = OPT(i − 1, j)Proof.

Since zk ≠ xi, Z a common subsequence of Xi−1,Yj

OPT(i − 1, j) a common subsequence of Xi,Yj�⇒ �OPT(i − 1, j)� ≤ �OPT(i, j)� = �Z� (def of OPT(i, j) and Z)

�⇒ Z = OPT(i − 1, j)

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 8 / 23

Page 25: Lecture 12: Dynamic Programming II

Optimal Substructure: Proof (II)

Case 2: If xi ≠ yj and zk ≠ xi then Z = OPT(i − 1, j)Proof.

Since zk ≠ xi, Z a common subsequence of Xi−1,Yj

OPT(i − 1, j) a common subsequence of Xi,Yj�⇒ �OPT(i − 1, j)� ≤ �OPT(i, j)� = �Z� (def of OPT(i, j) and Z)

�⇒ Z = OPT(i − 1, j)

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 8 / 23

Page 26: Lecture 12: Dynamic Programming II

Optimal Substructure: Proof (III)

Case 3: If xi ≠ yj and zk ≠ yj then Z = OPT(i, j − 1)Proof.

Symmetric to Case 2.

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 9 / 23

Page 27: Lecture 12: Dynamic Programming II

Structure Corollary

Corollary

OPT(i, j) =�������������

� if i = 0 or j = 0,OPT(i − 1, j − 1) ○ xi if i, j > 0 and xi = yjmax(OPT(i, j − 1),OPT(i − 1, j)) if i, j > 0 and xi ≠ yj

Gives obvious recursive algorithm

� Can take exponential time (good exercise at home!)

Dynamic Programming!

� Top-Down: are problems getting “smaller”? What does “smaller” mean?

� Bottom-Up: two-dimensional table! What order to fill it in?

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 10 / 23

Page 28: Lecture 12: Dynamic Programming II

Structure Corollary

Corollary

OPT(i, j) =�������������

� if i = 0 or j = 0,OPT(i − 1, j − 1) ○ xi if i, j > 0 and xi = yjmax(OPT(i, j − 1),OPT(i − 1, j)) if i, j > 0 and xi ≠ yj

Gives obvious recursive algorithm

� Can take exponential time (good exercise at home!)

Dynamic Programming!

� Top-Down: are problems getting “smaller”? What does “smaller” mean?

� Bottom-Up: two-dimensional table! What order to fill it in?

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 10 / 23

Page 29: Lecture 12: Dynamic Programming II

Dynamic Programming Algorithm

LCS(X,Y) {for(i = 0 to m) M[i,0] = 0;for(j = 0 to n) M[0, j] = 0;for(i = 1 to m) {

for(j = 1 to n) {if(xi = yj)

M[i, j] = 1 +M[i − 1, j − 1];else

M[i, j] =max(M[i, j − 1],M[i − 1, j]);}

}return M[m,n];

}

Running Time: O(mn)

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 11 / 23

Ftt

Page 30: Lecture 12: Dynamic Programming II

Dynamic Programming Algorithm

LCS(X,Y) {for(i = 0 to m) M[i,0] = 0;for(j = 0 to n) M[0, j] = 0;for(i = 1 to m) {

for(j = 1 to n) {if(xi = yj)

M[i, j] = 1 +M[i − 1, j − 1];else

M[i, j] =max(M[i, j − 1],M[i − 1, j]);}

}return M[m,n];

}

Running Time: O(mn)

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 11 / 23

OcmG u

3an

Page 31: Lecture 12: Dynamic Programming II

Correctness

Theorem

M[i, j] = �OPT(i, j)�

Proof.

Induction on i + j (or could do on iterations in the algorithm)

Base Case: i + j = 0 �⇒ i = j = 0 �⇒ M[i, j] = 0 = �OPT(i, j)�Inductive Step: Divide into three cases

1. If i = 0 or j = 0, then M[i, j] = 0 = �OPT(i, j)�2. If xi = yj, then M[i, j] = 1 +M[i − 1, j − 1] = 1 + �OPT(i − 1, j − 1)� = �OPT(i, j)�3. If xi ≠ yj, then

M[i, j] =max(M[i, j − 1],M[i − 1, j]) (def of algorithm)

=max(�OPT(i, j − 1)�, �OPT(i − 1, j)�) (induction)

= �OPT(i, j)� (structure thm/corollary)

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 12 / 23

Page 32: Lecture 12: Dynamic Programming II

Correctness

Theorem

M[i, j] = �OPT(i, j)�Proof.

Induction on i + j (or could do on iterations in the algorithm)

Base Case: i + j = 0 �⇒ i = j = 0 �⇒ M[i, j] = 0 = �OPT(i, j)�Inductive Step: Divide into three cases

1. If i = 0 or j = 0, then M[i, j] = 0 = �OPT(i, j)�2. If xi = yj, then M[i, j] = 1 +M[i − 1, j − 1] = 1 + �OPT(i − 1, j − 1)� = �OPT(i, j)�3. If xi ≠ yj, then

M[i, j] =max(M[i, j − 1],M[i − 1, j]) (def of algorithm)

=max(�OPT(i, j − 1)�, �OPT(i − 1, j)�) (induction)

= �OPT(i, j)� (structure thm/corollary)

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 12 / 23

Page 33: Lecture 12: Dynamic Programming II

Correctness

Theorem

M[i, j] = �OPT(i, j)�Proof.

Induction on i + j (or could do on iterations in the algorithm)

Base Case: i + j = 0 �⇒ i = j = 0 �⇒ M[i, j] = 0 = �OPT(i, j)�

Inductive Step: Divide into three cases

1. If i = 0 or j = 0, then M[i, j] = 0 = �OPT(i, j)�2. If xi = yj, then M[i, j] = 1 +M[i − 1, j − 1] = 1 + �OPT(i − 1, j − 1)� = �OPT(i, j)�3. If xi ≠ yj, then

M[i, j] =max(M[i, j − 1],M[i − 1, j]) (def of algorithm)

=max(�OPT(i, j − 1)�, �OPT(i − 1, j)�) (induction)

= �OPT(i, j)� (structure thm/corollary)

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 12 / 23

Page 34: Lecture 12: Dynamic Programming II

Correctness

Theorem

M[i, j] = �OPT(i, j)�Proof.

Induction on i + j (or could do on iterations in the algorithm)

Base Case: i + j = 0 �⇒ i = j = 0 �⇒ M[i, j] = 0 = �OPT(i, j)�Inductive Step: Divide into three cases

1. If i = 0 or j = 0, then M[i, j] = 0 = �OPT(i, j)�

2. If xi = yj, then M[i, j] = 1 +M[i − 1, j − 1] = 1 + �OPT(i − 1, j − 1)� = �OPT(i, j)�3. If xi ≠ yj, then

M[i, j] =max(M[i, j − 1],M[i − 1, j]) (def of algorithm)

=max(�OPT(i, j − 1)�, �OPT(i − 1, j)�) (induction)

= �OPT(i, j)� (structure thm/corollary)

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 12 / 23

Page 35: Lecture 12: Dynamic Programming II

Correctness

Theorem

M[i, j] = �OPT(i, j)�Proof.

Induction on i + j (or could do on iterations in the algorithm)

Base Case: i + j = 0 �⇒ i = j = 0 �⇒ M[i, j] = 0 = �OPT(i, j)�Inductive Step: Divide into three cases

1. If i = 0 or j = 0, then M[i, j] = 0 = �OPT(i, j)�2. If xi = yj, then M[i, j] = 1 +M[i − 1, j − 1] = 1 + �OPT(i − 1, j − 1)� = �OPT(i, j)�

3. If xi ≠ yj, thenM[i, j] =max(M[i, j − 1],M[i − 1, j]) (def of algorithm)

=max(�OPT(i, j − 1)�, �OPT(i − 1, j)�) (induction)

= �OPT(i, j)� (structure thm/corollary)

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 12 / 23

n nint ction

Aalg structure corollary

Page 36: Lecture 12: Dynamic Programming II

Correctness

Theorem

M[i, j] = �OPT(i, j)�Proof.

Induction on i + j (or could do on iterations in the algorithm)

Base Case: i + j = 0 �⇒ i = j = 0 �⇒ M[i, j] = 0 = �OPT(i, j)�Inductive Step: Divide into three cases

1. If i = 0 or j = 0, then M[i, j] = 0 = �OPT(i, j)�2. If xi = yj, then M[i, j] = 1 +M[i − 1, j − 1] = 1 + �OPT(i − 1, j − 1)� = �OPT(i, j)�3. If xi ≠ yj, then

M[i, j] =max(M[i, j − 1],M[i − 1, j]) (def of algorithm)

=max(�OPT(i, j − 1)�, �OPT(i − 1, j)�) (induction)

= �OPT(i, j)� (structure thm/corollary)

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 12 / 23

Page 37: Lecture 12: Dynamic Programming II

Computing a Solution

Like we talked about last lecture: backtrack through dynamic programming table.

Details in CLRS 15.4

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 13 / 23

Page 38: Lecture 12: Dynamic Programming II

Optimal Binary Search Trees

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 14 / 23

Page 39: Lecture 12: Dynamic Programming II

Problem Definition

Input: probability distribution / search frequency of keys

� n distinct keys k1 < k2 < ⋅ ⋅ ⋅ < kn� For each i ∈ [n], probability pi that we search for ki (so ∑n

i=1 pi = 1)What’s the best binary search tree for these keys and frequencies?

Cost of searching for ki in tree T is depthT(ki) + 1 (say depth of root = 0)�⇒ E[cost of search in T] = ∑n

i=1 pi(depthT(ki) + 1)Definition: c(T) = ∑n

i=1 pi(depthT(ki) + 1)Problem: Find search tree T minimizing cost.

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 15 / 23

Page 40: Lecture 12: Dynamic Programming II

Problem Definition

Input: probability distribution / search frequency of keys

� n distinct keys k1 < k2 < ⋅ ⋅ ⋅ < kn� For each i ∈ [n], probability pi that we search for ki (so ∑n

i=1 pi = 1)What’s the best binary search tree for these keys and frequencies?

Cost of searching for ki in tree T is depthT(ki) + 1 (say depth of root = 0)�⇒ E[cost of search in T] = ∑n

i=1 pi(depthT(ki) + 1)

Definition: c(T) = ∑n

i=1 pi(depthT(ki) + 1)Problem: Find search tree T minimizing cost.

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 15 / 23

Page 41: Lecture 12: Dynamic Programming II

Problem Definition

Input: probability distribution / search frequency of keys

� n distinct keys k1 < k2 < ⋅ ⋅ ⋅ < kn� For each i ∈ [n], probability pi that we search for ki (so ∑n

i=1 pi = 1)What’s the best binary search tree for these keys and frequencies?

Cost of searching for ki in tree T is depthT(ki) + 1 (say depth of root = 0)�⇒ E[cost of search in T] = ∑n

i=1 pi(depthT(ki) + 1)Definition: c(T) = ∑n

i=1 pi(depthT(ki) + 1)Problem: Find search tree T minimizing cost.

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 15 / 23

Page 42: Lecture 12: Dynamic Programming II

Obvious Approach

Natural approach: greedy (make highest probability key the root). Does this work?

Set p1 > p2 > . . .pn, but with pi − pi+1 extremely small (say 1�2n)

E[cost of search in T] ≈ n�2Balanced search tree: E[cost] ≤ O(log n)

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 16 / 23

Page 43: Lecture 12: Dynamic Programming II

Obvious Approach

Natural approach: greedy (make highest probability key the root). Does this work?

Set p1 > p2 > . . .pn, but with pi − pi+1 extremely small (say 1�2n)

y

su kataIKate y ka

E[cost of search in T] ≈ n�2Balanced search tree: E[cost] ≤ O(log n)

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 16 / 23

Page 44: Lecture 12: Dynamic Programming II

Obvious Approach

Natural approach: greedy (make highest probability key the root). Does this work?

Set p1 > p2 > . . .pn, but with pi − pi+1 extremely small (say 1�2n)

y

su kataIKate y ka

E[cost of search in T] ≈

n�2Balanced search tree: E[cost] ≤ O(log n)

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 16 / 23

Page 45: Lecture 12: Dynamic Programming II

Obvious Approach

Natural approach: greedy (make highest probability key the root). Does this work?

Set p1 > p2 > . . .pn, but with pi − pi+1 extremely small (say 1�2n)

y

su kataIKate y ka

E[cost of search in T] ≈ n�2

Balanced search tree: E[cost] ≤ O(log n)

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 16 / 23

In Ya

or

Page 46: Lecture 12: Dynamic Programming II

Obvious Approach

Natural approach: greedy (make highest probability key the root). Does this work?

Set p1 > p2 > . . .pn, but with pi − pi+1 extremely small (say 1�2n)

y

su kataIKate y ka

E[cost of search in T] ≈ n�2Balanced search tree: E[cost] ≤ O(log n)

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 16 / 23

Eras

Page 47: Lecture 12: Dynamic Programming II

Intuition

Suppose root is kr. What does optimal tree look like?

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 17 / 23

it at sd tian

Page 48: Lecture 12: Dynamic Programming II

Intuition

Suppose root is kr. What does optimal tree look like?

y

su kataIKate y ka

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 17 / 23

Isn

Page 49: Lecture 12: Dynamic Programming II

Subproblems

Definition

Let OPT(i, j) with i ≤ j be optimal tree for keys {ki,k2, . . . ,kj}: tree T minimizing

c(T) = ∑j

a=i pa(depthT(ka) + 1)By convention, if i > j then OPT(i, j) empty

So overall goal is to find OPT(1,n).

Theorem (Optimal Substructure)

Let kr be the root of OPT(i, j). Then the left subtree of OPT(i, j) is OPT(i, r − 1), and the

right subtree of OPT(i, j) is OPT(r + 1, j).

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 18 / 23

girl

Page 50: Lecture 12: Dynamic Programming II

Subproblems

Definition

Let OPT(i, j) with i ≤ j be optimal tree for keys {ki,k2, . . . ,kj}: tree T minimizing

c(T) = ∑j

a=i pa(depthT(ka) + 1)By convention, if i > j then OPT(i, j) empty

So overall goal is to find OPT(1,n).Theorem (Optimal Substructure)

Let kr be the root of OPT(i, j). Then the left subtree of OPT(i, j) is OPT(i, r − 1), and the

right subtree of OPT(i, j) is OPT(r + 1, j).

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 18 / 23

Page 51: Lecture 12: Dynamic Programming II

Proof Sketch of Optimal Substructure

Definitions:

� Let T = OPT(i, j), TL its left subtree, TR its right subtree.

� Suppose for contradiction TL ≠ OPT(i, r − 1), let T′ = OPT(i, r − 1)�⇒ c(T′) < c(TL) (def of OPT(i, r − 1))� Let T̂ be tree get by replacing TL with T

Whole bunch of math (see lecture notes): get that c(T̂) < c(T)Contradicts T = OPT(i, j)Symmetric argument works for TR = OPT(r + 1, j)

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 19 / 23

Page 52: Lecture 12: Dynamic Programming II

Proof Sketch of Optimal Substructure

Definitions:

� Let T = OPT(i, j), TL its left subtree, TR its right subtree.

� Suppose for contradiction TL ≠ OPT(i, r − 1), let T′ = OPT(i, r − 1)�⇒ c(T′) < c(TL) (def of OPT(i, r − 1))� Let T̂ be tree get by replacing TL with T

Whole bunch of math (see lecture notes): get that c(T̂) < c(T)Contradicts T = OPT(i, j)

Symmetric argument works for TR = OPT(r + 1, j)

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 19 / 23

Page 53: Lecture 12: Dynamic Programming II

Proof Sketch of Optimal Substructure

Definitions:

� Let T = OPT(i, j), TL its left subtree, TR its right subtree.

� Suppose for contradiction TL ≠ OPT(i, r − 1), let T′ = OPT(i, r − 1)�⇒ c(T′) < c(TL) (def of OPT(i, r − 1))� Let T̂ be tree get by replacing TL with T

Whole bunch of math (see lecture notes): get that c(T̂) < c(T)Contradicts T = OPT(i, j)Symmetric argument works for TR = OPT(r + 1, j)

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 19 / 23

Page 54: Lecture 12: Dynamic Programming II

Cost Corollary

Corollary

c(OPT(i, j)) = ∑j

a=i pa +mini≤r≤j(c(OPT(i, r − 1)) + c(OPT(r + 1, j)))Let kr be root of OPT(i, j)c(OPT(i, j)) = j�

a=ipa(depthOPT(i,j)(ka) + 1)

= r−1�a=i(pa(depthOPT(i,r−1)(ka) + 2)) + pr + j�

a=r+1pa(depthOPT(r+1,j)(ka) + 2)

= j�a=i

pa + r−1�a=i(pa(depthOPT(i,r−1)(ka) + 1)) + j�

a=r+1pa(depthOPT(r+1,j)(ka) + 1)

= j�a=i

pa + c(OPT(i, r − 1)) + c(OPT(r + 1, j)).

Same logic holds for any possible root �⇒ take min

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 20 / 23

Pdef at est

structure7

algebra

def c f ontcast

Page 55: Lecture 12: Dynamic Programming II

Cost Corollary

Corollary

c(OPT(i, j)) = ∑j

a=i pa +mini≤r≤j(c(OPT(i, r − 1)) + c(OPT(r + 1, j)))Let kr be root of OPT(i, j)c(OPT(i, j)) = j�

a=ipa(depthOPT(i,j)(ka) + 1)

= r−1�a=i(pa(depthOPT(i,r−1)(ka) + 2)) + pr + j�

a=r+1pa(depthOPT(r+1,j)(ka) + 2)

= j�a=i

pa + r−1�a=i(pa(depthOPT(i,r−1)(ka) + 1)) + j�

a=r+1pa(depthOPT(r+1,j)(ka) + 1)

= j�a=i

pa + c(OPT(i, r − 1)) + c(OPT(r + 1, j)).Same logic holds for any possible root �⇒ take min

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 20 / 23

Page 56: Lecture 12: Dynamic Programming II

Algorithm

Fill in table M:

M[i, j] = �������0 if i > jmini≤r≤j �∑j

a=i pa +M[i, r − 1] +M[r + 1, j]� if i ≤ j

Top-Down (memoization): are problems getting smaller? Yes! j − i decreases in every recursive

call.

Correctness. Claim M[i, j] = c(OPT(i, j)). Induction on j − i.� Base case: if j − i < 0 then M[i, j] = OPT(i, j) = 0� Inductive step:

M[i, j] =mini≤r≤j��

j�a=i

pa +M[i, r − 1] +M[r + 1, j]�� (alg def)

=mini≤r≤j��

j�a=i

pa + c(OPT(i, r − 1)) + c(OPT(r + 1, j))�� (induction)

= c(OPT(i, j)) (cost corollary)

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 21 / 23

Page 57: Lecture 12: Dynamic Programming II

Algorithm

Fill in table M:

M[i, j] = �������0 if i > jmini≤r≤j �∑j

a=i pa +M[i, r − 1] +M[r + 1, j]� if i ≤ jTop-Down (memoization): are problems getting smaller?

Yes! j − i decreases in every recursive

call.

Correctness. Claim M[i, j] = c(OPT(i, j)). Induction on j − i.� Base case: if j − i < 0 then M[i, j] = OPT(i, j) = 0� Inductive step:

M[i, j] =mini≤r≤j��

j�a=i

pa +M[i, r − 1] +M[r + 1, j]�� (alg def)

=mini≤r≤j��

j�a=i

pa + c(OPT(i, r − 1)) + c(OPT(r + 1, j))�� (induction)

= c(OPT(i, j)) (cost corollary)

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 21 / 23

Page 58: Lecture 12: Dynamic Programming II

Algorithm

Fill in table M:

M[i, j] = �������0 if i > jmini≤r≤j �∑j

a=i pa +M[i, r − 1] +M[r + 1, j]� if i ≤ jTop-Down (memoization): are problems getting smaller? Yes! j − i decreases in every recursive

call.

Correctness. Claim M[i, j] = c(OPT(i, j)). Induction on j − i.� Base case: if j − i < 0 then M[i, j] = OPT(i, j) = 0� Inductive step:

M[i, j] =mini≤r≤j��

j�a=i

pa +M[i, r − 1] +M[r + 1, j]�� (alg def)

=mini≤r≤j��

j�a=i

pa + c(OPT(i, r − 1)) + c(OPT(r + 1, j))�� (induction)

= c(OPT(i, j)) (cost corollary)

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 21 / 23

Page 59: Lecture 12: Dynamic Programming II

Algorithm

Fill in table M:

M[i, j] = �������0 if i > jmini≤r≤j �∑j

a=i pa +M[i, r − 1] +M[r + 1, j]� if i ≤ jTop-Down (memoization): are problems getting smaller? Yes! j − i decreases in every recursive

call.

Correctness. Claim M[i, j] = c(OPT(i, j)). Induction on j − i.

� Base case: if j − i < 0 then M[i, j] = OPT(i, j) = 0� Inductive step:

M[i, j] =mini≤r≤j��

j�a=i

pa +M[i, r − 1] +M[r + 1, j]�� (alg def)

=mini≤r≤j��

j�a=i

pa + c(OPT(i, r − 1)) + c(OPT(r + 1, j))�� (induction)

= c(OPT(i, j)) (cost corollary)

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 21 / 23

Page 60: Lecture 12: Dynamic Programming II

Algorithm

Fill in table M:

M[i, j] = �������0 if i > jmini≤r≤j �∑j

a=i pa +M[i, r − 1] +M[r + 1, j]� if i ≤ jTop-Down (memoization): are problems getting smaller? Yes! j − i decreases in every recursive

call.

Correctness. Claim M[i, j] = c(OPT(i, j)). Induction on j − i.� Base case: if j − i < 0 then M[i, j] = OPT(i, j) = 0

� Inductive step:

M[i, j] =mini≤r≤j��

j�a=i

pa +M[i, r − 1] +M[r + 1, j]�� (alg def)

=mini≤r≤j��

j�a=i

pa + c(OPT(i, r − 1)) + c(OPT(r + 1, j))�� (induction)

= c(OPT(i, j)) (cost corollary)

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 21 / 23

Page 61: Lecture 12: Dynamic Programming II

Algorithm

Fill in table M:

M[i, j] = �������0 if i > jmini≤r≤j �∑j

a=i pa +M[i, r − 1] +M[r + 1, j]� if i ≤ jTop-Down (memoization): are problems getting smaller? Yes! j − i decreases in every recursive

call.

Correctness. Claim M[i, j] = c(OPT(i, j)). Induction on j − i.� Base case: if j − i < 0 then M[i, j] = OPT(i, j) = 0� Inductive step:

M[i, j] =mini≤r≤j��

j�a=i

pa +M[i, r − 1] +M[r + 1, j]�� (alg def)

=mini≤r≤j��

j�a=i

pa + c(OPT(i, r − 1)) + c(OPT(r + 1, j))�� (induction)

= c(OPT(i, j)) (cost corollary)Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 21 / 23

Page 62: Lecture 12: Dynamic Programming II

Algorithm: Bottom-up

What order to fill the table in?

� Obvious approach: for(i = 1 to n − 1) for(j = i + 1 to n) Doesn’t work!

� Take hint from induction: j − iOBST {

Set M[i, j] = 0 for all j > i;Set M[i, i] = pi for all ifor(` = 1 to n − 1) {

for(i = 1 to n − `) {j = i + `M[i, j] =mini≤r≤j �∑j

a=i pa +M[i, r − 1] +M[r + 1, j]�;}

}return M[1,n];

}

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 22 / 23

Page 63: Lecture 12: Dynamic Programming II

Algorithm: Bottom-up

What order to fill the table in?

� Obvious approach: for(i = 1 to n − 1) for(j = i + 1 to n) Doesn’t work!

� Take hint from induction: j − iOBST {

Set M[i, j] = 0 for all j > i;Set M[i, i] = pi for all ifor(` = 1 to n − 1) {

for(i = 1 to n − `) {j = i + `M[i, j] =mini≤r≤j �∑j

a=i pa +M[i, r − 1] +M[r + 1, j]�;}

}return M[1,n];

}

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 22 / 23

If

Page 64: Lecture 12: Dynamic Programming II

Analysis

Correctness: same as top-down

Running Time:

� # table entries: O(n2)� Time to compute table entry M[i, j]: O(j − i) = O(n)

Total running time: O(n3)

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 23 / 23

Page 65: Lecture 12: Dynamic Programming II

Analysis

Correctness: same as top-down

Running Time:

� # table entries:

O(n2)� Time to compute table entry M[i, j]: O(j − i) = O(n)

Total running time: O(n3)

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 23 / 23

Page 66: Lecture 12: Dynamic Programming II

Analysis

Correctness: same as top-down

Running Time:

� # table entries: O(n2)

� Time to compute table entry M[i, j]: O(j − i) = O(n)Total running time: O(n3)

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 23 / 23

Page 67: Lecture 12: Dynamic Programming II

Analysis

Correctness: same as top-down

Running Time:

� # table entries: O(n2)� Time to compute table entry M[i, j]:

O(j − i) = O(n)Total running time: O(n3)

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 23 / 23

Page 68: Lecture 12: Dynamic Programming II

Analysis

Correctness: same as top-down

Running Time:

� # table entries: O(n2)� Time to compute table entry M[i, j]: O(j − i) = O(n)

Total running time: O(n3)

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 23 / 23

Page 69: Lecture 12: Dynamic Programming II

Analysis

Correctness: same as top-down

Running Time:

� # table entries: O(n2)� Time to compute table entry M[i, j]: O(j − i) = O(n)

Total running time: O(n3)

Michael Dinitz Lecture 12: Dynamic Programming II October 7, 2021 23 / 23