Unit 5 Dynamic Programming

# Unit 5 Dynamic Programming - Dynamic Programming Definition...

This preview shows pages 1–5. Sign up to view the full content.

3/5/2008 1 Dynamic Programming Divide the problem into a tree of sub-problems Conquer each problem in the tree in post-order, recording problem/solution pairs in a table Combine the solution to each child problem to solve each parent problem using any applicable problem/solution pairs in the table. Sound familiar at all? Definition Algorithmic Pattern (iterative): Solution DP( Problem p ) tree b p.divide() list b tree.postOrder() for each problem, p i , in list children b p i .children() solution b for each problem, cij, in children if cache( cij ) not empty then sij b cached solution else sij b cij.solve() cache b cache + <cij, sij> end if solution b solution + sij end for cache b cache + < p i , solution> end for return cache( p ) Definition

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
3/5/2008 2 Use the dynamic programming algorithmic pattern when ALL of the following are true: The problem lends itself to division into sub division into sub- problems of the same type The sub-problems have considerable overlap in their solution An acceptable solution to the problem can be constructed from acceptable solutions to sub- problems Extra memory is readily available Applicability Academic “n” Coins Mathematics Fibonacci sequence Matrix Multiplication Graphs Shortest Path String Edit distance (similarity) String alignment Well-Known Uses How many coins will be given in change for a purchase of up to \$1.00 Build a table starting at the base case Work from the bottom sub Work from the bottom sub-problems to the top problems to the top sub sub-problems problems This will work as long as when we need to compute the value for N we already have the following values in our table: N-1, N- 5, N-10, N-12, N-25 0 1 2 3 4 5 6 7 0 1 2 3 4 1 2 3 Example: NCoins
3/5/2008 3 However, this table gives only the minimum number of coins needed This is the thing being optimized What usually also want the actual coins used To do this we build a second table as we are building the first table The second table contains, at each step, the coin that produced the optimal number of coins Example: NCoins We can then recursively reconstruct the list of coins used to produce the minimum number of coins We stop when we reach a base case (0 in this example) Note that the second table is constructed during the construction of the first table, but is never used to determine values in the first table And the first table is never used during the recursive reconstructing of the coin list 0 1 2 3 4 5 6 7 0 1 1 1 1 5 1 or 5 1 or 5 0 1 2 3 4 1 2 3 Example: NCoins 1x2 + 2x5 + 3x8 = 2 + 10 + 24 = 36 1 2 3 4 5 6 1 2 3 4 5 6 7 8 9 30 36 42 66 81 96 rows from the first matrix columns from the second matrix Example: Matrix Mult.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
3/5/2008 4 “inner” dimensions must match result is “outer” dimension Examples: 2x3
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 04/19/2008 for the course CS 250 taught by Professor Michaelr.wick during the Spring '08 term at Wisc Eau Claire.

### Page1 / 14

Unit 5 Dynamic Programming - Dynamic Programming Definition...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online