140 likes | 330 Views
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
E N D
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
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
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
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
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
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}
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
Optimal Binary Search Tree • Create binary search tree to minimize N Σ pi(1+di) i=1 if a two and the am egg
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
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