Dynamic Pg m Ming

52
Dynamic Programming Briana B. Morrison With thanks to Dr. Hung

Transcript of Dynamic Pg m Ming

  • Dynamic ProgrammingBriana B. MorrisonWith thanks to Dr. Hung

    *

    TopicsWhat is Dynamic ProgrammingBinomial CoefficientFloyds AlgorithmChained Matrix MultiplicationOptimal Binary Search TreeTraveling Salesperson

    *

    Why Dynamic Programming?Divide-and-Conquer: a top-down approach.Many smaller instances are computed more than once.

    Dynamic programming: a bottom-up approach.Solutions for smaller instances are stored in a table for later use.

    *

    Dynamic ProgrammingAn Algorithm Design TechniqueA framework to solve Optimization problemsElements of Dynamic ProgrammingDynamic programming version of a recursive algorithm.Developing a Dynamic Programming AlgorithmExample: Multiplying a Sequence of Matrices

    *

    Why Dynamic Programming? It sometimes happens that the natural way of dividing an instance suggested by the structure of the problem leads us to consider several overlapping subinstances. If we solve each of these independently, they will in turn create a large number of identical subinstances. If we pay no attention to this duplication, it is likely that we will end up with an inefficient algorithm. If, on the other hand, we take advantage of the duplication and solve each subinstance only once, saving the solution for later use, then a more efficient algorithm will result.

    *

    Why Dynamic Programming? The underlying idea of dynamic programming is thus quite simple: avoid calculating the same thing twice, usually by keeping a table of known results, which we fill up as subinstances are solved.

    Dynamic programming is a bottom-up technique. Examples: 1) Fibonacci numbers2) Computing a Binomial coefficient

    *

    Dynamic Programming Dynamic Programming is a general algorithm design technique. Invented by American mathematician Richard Bellman in the 1950s to solve optimization problems. Programming here means planning.

    Main idea: solve several smaller (overlapping) subproblems. record solutions in a table so that each subproblem is only solved once. final state of the table will be (or contain) solution.

    *

    Dynamic ProgrammingDefine a container to store intermediate resultsAccess container versus recomputing results

    Fibonacci numbers example (top down)Use vector to store results as calculated so they are not re-calculated

    *

    Dynamic ProgrammingFibonacci numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 24

    Recurrence Relation of Fibonacci numbers ?

    *

    Example: Fibonacci numbers Recall definition of Fibonacci numbers:

    f(0) = 0f(1) = 1f(n) = f(n-1) + f(n-2) for n 2

    Computing the nth Fibonacci number recursively (top-down): f(n)

    f(n-1) + f(n-2)

    f(n-2) + f(n-3) f(n-3) + f(n-4)

    ...

    *

    Fib vs. fibDynint fib(int n) {if (n = 0) // check for a previously computed result and returnreturn fibList[n];// otherwise execute the recursive algorithm to obtain the resultif (n