dynamic programming 2 mani chandy [email protected]

37
Dynamic Programming 2 Mani Chandy [email protected]

Post on 22-Dec-2015

263 views

Category:

Documents


7 download

TRANSCRIPT

Page 1: Dynamic Programming 2 Mani Chandy mani@cs.caltech.edu

Dynamic Programming 2

Mani Chandy

[email protected]

Page 2: Dynamic Programming 2 Mani Chandy mani@cs.caltech.edu

The Pattern

• Given a problem P, obtain a sequence of problems Q0, Q1, …., Qm, where:

– You have a solution to Q0

– The solution to P can be obtained from the solution to Qm,

– The solution to a problem Qj, j > 0, can be obtained from solutions to problems Qk, k < j, that appear earlier in the sequence.

Page 3: Dynamic Programming 2 Mani Chandy mani@cs.caltech.edu

Dynamic Progamming Pattern

PGiven problem P

Propose a partial ordering of problems

Q0Qm

You know how to compute solution to Q0

You can compute thesolution to P from the solution to Qm

Page 4: Dynamic Programming 2 Mani Chandy mani@cs.caltech.edu

Creative Step

Finding problems Qi from problem P

More mechanical step: Determining the function that computes the solution Sk for problem Qk from the solutions Sj of problems Qj for j < k.

Page 5: Dynamic Programming 2 Mani Chandy mani@cs.caltech.edu

Example: Matrix Multiplication

1 X N

N X N N X NNX1

What is the cost of multiplying matrices of these sizes?

Page 6: Dynamic Programming 2 Mani Chandy mani@cs.caltech.edu

Cost of multiplying 2 matrices

p x q

q x r

p rows and q columns.

Cost is 2pqr because resultant matrix has pr elements, andThe cost of computing each element is 2q operations.

Page 7: Dynamic Programming 2 Mani Chandy mani@cs.caltech.edu

Parenthesization

1 X N

N X N N X NNX1

If we multiply these matrices first the cost is 2N3.

N X NResulting matrix

Page 8: Dynamic Programming 2 Mani Chandy mani@cs.caltech.edu

Parenthesization1 X N

NX1

N X N

Cost of multiplication is N2.

Thus, total cost is proportional to N3 + N2 + N if we parenthesizethe expression in this way.

Page 9: Dynamic Programming 2 Mani Chandy mani@cs.caltech.edu

Different Ordering

1 X N

N X N N X NNX1

Cost is proportional to N2

Page 10: Dynamic Programming 2 Mani Chandy mani@cs.caltech.edu

The Ordering Matters!

One ordering costs O(N3)

The other ordering costs O(N2)

1 X N

N X N N X NNX1

1 X NN X N N X N

NX1

Page 11: Dynamic Programming 2 Mani Chandy mani@cs.caltech.edu

Generalization: Parenthesization

A1 op An A3 A2 op op …. op

Associative operation

Cost of operation depends on parameters of the operands.

Parenthesize to minimize total cost.

( ) ( )( )

Page 12: Dynamic Programming 2 Mani Chandy mani@cs.caltech.edu

Creative Step

Come up with partial-ordering of problems Qi given problem P.

Propose a partial ordering of problems

Q0Qm

Page 13: Dynamic Programming 2 Mani Chandy mani@cs.caltech.edu

Creative Step: Solution

Qi,j is: optimally parenthesize the expression Ai op ….. op Aj

Relatively “mechanical” steps:1. Find partial ordering of problems Qi,j 2. Find function f that computes solution Si,j from solutions of problems earlier in the ordering.

Page 14: Dynamic Programming 2 Mani Chandy mani@cs.caltech.edu

Partial Ordering Structure

Q1,1 Q2,2Q3,3 Q4,4

Q1,2 Q2,3 Q3,4

Q1,3 Q2,4

Q1,4

Depends on

Solutions known

Solution to givenproblem obtainedfrom solution to thisproblem Q1,n.

Page 15: Dynamic Programming 2 Mani Chandy mani@cs.caltech.edu

The Recurrence Relation

Let C[j,k] be the minimum cost of executing Aj op … op Ak.

Base Case: ???? C[j,j] = 0

Induction Step: ????C[j,k] for k > j is: min over all v of C[j,v]+C[v+1,k] + cost of the operation combining [j…v] and [v+1 … k]

Proof: ???

Page 16: Dynamic Programming 2 Mani Chandy mani@cs.caltech.edu

For matrix multiplication

Let j-th matrix have size: p(j-1) X pj

Then the size of matrix obtained by combining [ j … v] is: ?

p (j-1) X pv

Then the size of matrix obtained by combining [ v+1 … k] is: ?

pv X pk

Cost of multiplying [j … v] and [v+1 … k] is p (j-1) X pv X pk

Page 17: Dynamic Programming 2 Mani Chandy mani@cs.caltech.edu

Computational ComplexityRecurrence Relation for matrix multiplication:

C[j,k] for k > j is: min over all v of (C[j,v]+C[v+1,k] + p(j-1)pvpk)

Time to compute C[j,k] is proportional to (k-j)

Total time to compute C, for all j,k is:0 x N {for bottom level, i.e., for C[j,j] } +1 x (N-1) { for first level, i.e., for C[j,j+1]} + …. +k x (N-k) {for k-th level, i.e., for C[j, j+ k]} + …..

Show that total time is proportional to O(N3).

Page 18: Dynamic Programming 2 Mani Chandy mani@cs.caltech.edu

Proof Structure

What is the theorem that we are proving?

We make an assertion about the meaning of a term.

For instance, C[j,k] is the minimum cost of executing Aj op …. op Ak

We are proving that this assertion is correct.

Page 19: Dynamic Programming 2 Mani Chandy mani@cs.caltech.edu

Proof Structure

Almost always, we use induction.

Base case: establish that the value of C[j,j] is correct.

Induction step: Assume that the value of C[j, j+u] is correct for all u where u is less than V, and prove that the value of C[j, j+V] iscorrect.

Remember what we are proving:C[j,k] is the minimum cost of executing Aj op …. op Ak

Page 20: Dynamic Programming 2 Mani Chandy mani@cs.caltech.edu

The Central Idea

Bellman’s optimality principle

Qi,j Qu,v

Pick optimal

Discard others

Qa,z

The discarded solutions forthe smaller problem remaindiscarded because the optimalsolution dominates them.

Page 21: Dynamic Programming 2 Mani Chandy mani@cs.caltech.edu

All-Points Shortest Path

Given a weighted directed graph. • The edge-weight W[j,k] represents the distance from vertex j to vertex k.• There are no cycles of negative weight.• For all j, k, compute D[j,k] where D[j,k] is the length of the shortest path from vertex j to vertex k.

Page 22: Dynamic Programming 2 Mani Chandy mani@cs.caltech.edu

The Creative Step

Come up with partial-ordering of problems Qi given problem P.

There are different problem sets Qi some better than others.

Page 23: Dynamic Programming 2 Mani Chandy mani@cs.caltech.edu

Creative Step

Let F[j,k,m] be the length of the shortest path that has 1. at most m hops, and2. is from vertex j to vertex k

What is the partial-ordering of problems Q[j,k,m]?

Page 24: Dynamic Programming 2 Mani Chandy mani@cs.caltech.edu

A recurrence relation

F[j,k,m] = min over all r of F[j,r,m-1] + W[r,k]

Base case: F[j,k,1] = ?????

W[j,k](assume W[j,j] = 0 for all j)

Obtaining solution for given problem P

D[j,k] = F[j,k,n-1]

Page 25: Dynamic Programming 2 Mani Chandy mani@cs.caltech.edu

Proof of Correctness

What are we proving?

We are proving that the meaning we gave to F[j,k,m] is correct

Base CaseWe show that F[j,k,1] is indeed the length of the shortest pathfrom vertex j to vertex k that traverses at most one edge.

Induction StepAssume that F[j,k,m] is the length of the shortest path from j to kthat traverses at most m edges, for all m less than p, and prove thatF[j,k,p] is the min length from j to k that traverses at most p edges

Page 26: Dynamic Programming 2 Mani Chandy mani@cs.caltech.edu

Proof of Induction Step

Consider any path Z from a vertex j to a different vertex kwhere the path traverses at most p edges.

We must prove: length(Z) is at least F[j,k,p]

Let r be the prefinal vertex in path Z, i.e., r is the last vertexbefore k in path Z. Partition the path Z into the path Z’ fromvertex j to vertex r followed by the edge (r,k).Then: length(Z) = length(Z’) + W[r,k]

By the induction assumption: length(Z’) ?????

Page 27: Dynamic Programming 2 Mani Chandy mani@cs.caltech.edu

Proof of induction step

length(Z’) >= F[j,r,p-1]From the induction hypothesis

jr

k

at most p-1 hops 1hop

Path Z

Path Z’

Page 28: Dynamic Programming 2 Mani Chandy mani@cs.caltech.edu

Proof of Induction Step

length(Z) = length(Z’) + W[r,k]

length(Z’) >= F[j,r,p-1]

Hence, length(Z) >= F[j,r,p-1] + W[r,k]

F[j,k,p] = min over all r of F[j,r,p-1] + W[r,k]

Hence, length(Z) >= F[j,k,p]

Page 29: Dynamic Programming 2 Mani Chandy mani@cs.caltech.edu

Proof of Induction Step

We have shown that any path with at most p hopsfrom vertex j to vertex k has length at least F[j,k,p].

Next, we show there exists a path with at most p hopsfrom vertex j to vertex k that has length F[j,k,p].

From these two we conclude that the minimum lengthpath with at most p hops from vertex j to vertex k haslength F[j,k,p].

Left to you

Page 30: Dynamic Programming 2 Mani Chandy mani@cs.caltech.edu

Complexity?

n4

Can you do better?

Come up with partial-ordering of problems Qi given problem P.

Let F[j,k,m] be the length of the shortest path from vertex j tovertex k that has at most m hops.

2m

Page 31: Dynamic Programming 2 Mani Chandy mani@cs.caltech.edu

Recurrence Relation

Let F[j,k,m] be the length of the shortest path from vertex j tovertex k that has at most 2m hops.

Derive a recurrence relation from this meaning of F[j,k,m].

F[j,k,0] = ??? Length of shortest path from vertex j to vertex k that has at most 1 edge.

W[j,k]

F[j,k,m] = ??? Length of shortest path from vertex j to vertex k that has at most 2m edges.

Page 32: Dynamic Programming 2 Mani Chandy mani@cs.caltech.edu

Derive Recurrence Relation

F[j,k,m] = ??? Length of shortest path from vertex j to vertex k that has at most 2m edges.

F[j,k,m+1] = min over all v of (F[j,v,m] + F[v,k,m])

Page 33: Dynamic Programming 2 Mani Chandy mani@cs.caltech.edu

Proof

Exactly the same proof structure as the previous case.

Consider any path Z from j to k with at most 2(m+1) hops.

Let the number of hops in the path be t.Partition path Z into path Y followed by path Y’ wherethe number of hops in path Y is floor(t/2). Let Y end atvertex r.

Y Y’

1

2

3

4

56

7 8Path Z

r

Page 34: Dynamic Programming 2 Mani Chandy mani@cs.caltech.edu

Proof

length(Z) = length(Y) + length(Y’)

From the induction hypothesis: length(Y) >= F[j,r,m] and length(Y’) >= F[r,k,m]

Since Z has at most 2(m+1) hops, Y and Y’ each have at most2m hops.

Hence length(Z) >= F[j,r,m] + F[r,k,m]

From recurrence relation, F[j,r,m]+F[r,k,m] >= F[j,k,m+1]

Hence, length(Z) >= F[j,k,m+1]

Page 35: Dynamic Programming 2 Mani Chandy mani@cs.caltech.edu

Proof

We have shown that every path with at most 2m hops fromvertex j to vertex k has length at least F[j,k,m].

Now show that there exists a path with at most 2m hops fromvertex j to vertex k that has length F[j,k,m].

Proof has the same structure and is left to you.

Page 36: Dynamic Programming 2 Mani Chandy mani@cs.caltech.edu

Complexity?

n3 log(n)

Can you do better?

Come up with partial-ordering of problems Qi given problem P.

Let F[j,k,m] be the length of the shortest path from vertex j tovertex k that traverses intermediate vertices only in set {1, … m-1}

Page 37: Dynamic Programming 2 Mani Chandy mani@cs.caltech.edu

Derive and Prove Recurrence