1 / 12

Algorithm Design Techniques: Dynamic Programming

Algorithm Design Techniques: Dynamic Programming. Introduction. Dynamic Programming Divide-and-conquer solution to a problem with overlapping subproblems Similar to bottom-up recursion where results are saved in a table as they are computed Typically applied to optimization problems

selia
Download Presentation

Algorithm Design Techniques: Dynamic Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Algorithm Design Techniques:Dynamic Programming

  2. Introduction • Dynamic Programming • Divide-and-conquer solution to a problem with overlapping subproblems • Similar to bottom-up recursion where results are saved in a table as they are computed • Typically applied to optimization problems • Typically best used when objects have a linear ordering and cannot be rearranged • http://www.cs.sunysb.edu/~algorith/lectures-good/node13.html • http://www.cs.sunysb.edu/~algorith/lectures-good/node12.html

  3. Example: Fibonacci • Basic recursive solution F(N) = F(N-1)+F(N-2) • F1 calculated three times • Instead, calculate numbers 1->n, store values as they are calculated • Use space instead of time F4 F2 F3 F0 F1 F2 F1 F1 F0

  4. Steps (CLR pg323) • Characterize the structure of an optimal solution (optimal substructure) • Recursively define the value of an optimal solution • Compute the value of an optimal solution in a bottom-up fashion • Construct an optimal solution from computed information

  5. Ordering Matrix Multiplications • Matrices • A (50x10), B (10x40), C (40x30), D (30x5) • Compute ABCD • Associative (not commutative) • Fits the ordering property • Decide how to parenthesize to achieve fewest operations

  6. Ordering Matrix Multiplications • (A((BC)D) • BC = 10x40x30 = 12,000 operations • (BC)D = 12,000 + 10x30x5 = 13,500 • A((BC)D) = 13,500 + 50x10x5 = 16,000 • (A(B(CD)) = 10,500 • ((AB)(CD)) = 36,000 • (((AB)C)D) = 87,500 • ((A(BC))D) = 34,500

  7. Ordering Matrix Multiplications N-1 • T(N) = Σ T(i)T(N-i) i=1 • Basic recursive solution is exponential • MLeft, Right = minLeft<=i<=Right{MLeft,i+Mi+1,Right+cLeft-1cicRight}

  8. Ordering Matrix Multiplications 10,500 • N2/2 values are calculated • O(N3) running time A = 50x10 B = 10x40 C = 40x30 D = 30x5 27,000 8,000 20,000 12,000 6,000 0 0 0 0 C D B A

  9. Optimal Binary Search Tree • Create binary search tree to minimize N Σ pi(1+di) i=1 if a two and the am egg

  10. Optimal Binary Search Tree • Basic greedy strategy does not work • But, problem has the ordering property and optimal substructure property i-1 Right • CLeft, Right = min{pi + CLeft, i-1+Ci+1, Right + Spj + Spj } j=Left j=i+1

  11. Optimal Binary Search Tree (pg431)

  12. Memoization • Maintain top-down recursion, but memoize (keep "memos" of) results allocate array for memo; initialize memo[1] and memo[2] to 1; fib(n) { if memo[n] is not zero, return memo[n];memo[n] = fib(n-1) + fib(n-2);return memo[n]; } • http://www.nist.gov/dads/HTML/memoize.html

More Related