Download presentation
Presentation is loading. Please wait.
1
Dynamic Programming Mani Chandy mani@cs.caltech.edu
2
The Pattern Given a problem P, obtain a sequence of problems Q 0, Q 1, …., Q m, where: –You have a solution to Q 0 –The solution to P can be obtained from the solution to Q m, –The solution to a problem Q j, j > 0, can be obtained from solutions to problems Q k, k < j, that appear earlier in the sequence.
3
Dynamic Progamming Pattern P Given problem P Propose a partial ordering of problems Q0Q0 QmQm You know how to compute solution to Q 0 You can compute the solution to P from the solution to Q m
4
Creative Step Finding problems Q i from problem P More mechanical step: Determining the function that computes the solution S k for problem Q k from the solutions S j of problems Q j for j < k.
5
Example: Matrix Multiplication 1 X N N X N NX1NX1 What is the cost of multiplying matrices of these sizes?
6
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, and The cost of computing each element is 2q operations.
7
Parenthesization 1 X N N X N NX1NX1 If we multiply these matrices first the cost is 2N 3. N X N Resulting matrix
8
Parenthesization 1 X N NX1NX1 N X N Cost of multiplication is N 2. Thus, total cost is proportional to N 3 + N 2 + N if we parenthesize the expression in this way.
9
Different Ordering 1 X N N X N NX1NX1 Cost is proportional to N 2
10
The Ordering Matters! One ordering costs O(N 3 ) The other ordering costs O(N 2 ) 1 X N N X N NX1NX1 1 X N N X N NX1NX1
11
Generalization: Parenthesization A1A1 op AnAn A3A3 A2A2 op …. op Associative operation Cost of operation depends on parameters of the operands. Parenthesize to minimize total cost. () () ()
12
Creative Step Come up with partial-ordering of problems Q i given problem P. Propose a partial ordering of problems Q0Q0 QmQm
13
Creative Step: Solution Q i,j is: optimally parenthesize the expression A i op ….. op A j Relatively “mechanical” steps: 1.Find partial ordering of problems Q i,j 2.Find function f that computes solution S i,j from solutions of problems earlier in the ordering.
14
Partial Ordering Structure Q 1,1 Q 2,2 Q 3,3 Q 4,4 Q 1,2 Q 2,3 Q 3,4 Q 1,3 Q 2,4 Q 1,4 Depends on Solutions known Solution to given problem obtained from solution to this problem Q 1,n.
15
The Recurrence Relation Let C[j,k] be the minimum cost of executing A j op … op A k. 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: ???
16
For matrix multiplication Let j-th matrix have size: p (j-1) X p j Then the size of matrix obtained by combining [ j … v] is: ? p (j-1) X p v Then the size of matrix obtained by combining [ v+1 … k] is: ? p v X p k Cost of multiplying [j … v] and [v+1 … k] is p (j-1) X p v X p k
17
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 A j op …. op A k We are proving that this assertion is correct.
18
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] is correct. Remember what we are proving: C[j,k] is the minimum cost of executing A j op …. op A k
19
The Central Idea Bellman’s optimality principle Q i,j Q u,v Pick optimal Discard others Q a,z The discarded solutions for the smaller problem remain discarded because the optimal solution dominates them.
20
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.
21
The Creative Step Come up with partial-ordering of problems Q i given problem P. There are different problem sets Q i some better than others.
22
Creative Step Let F[j,k,m] be the length of the shortest path from vertex j to vertex k that has at most m hops. What is the partial-ordering of problems Q[j,k,m]?
23
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]
24
Proof of Correctness What are we proving? We are proving that the meaning we gave to F[j,k,m] is correct Base Case We show that F[j,k,1] is indeed the length of the shortest path from vertex j to vertex k that traverses at most one edge. Induction Step Assume that F[j,k,m] is the length of the shortest path from j to k that traverses at most m edges, for all m less than p, and prove that F[j,k,p] is the min length from j to k that traverses at most p edges
25
Complexity? n4n4 Can you do better? Come up with partial-ordering of problems Q i given problem P. Let F[j,k,m] be the length of the shortest path from vertex j to vertex k that has at most m hops. 2m2m
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.