Chapter 21
Dynamic Programming Algorithms

The approach assumes a recursive solution for the given problem, with a bottom-up evaluation of the solution. The subsolutions are recorded (in tables) for reuse.

21.1 Fibonnaci Numbers

     {
f(i) =  f(i- 1)+ f(i- 2) if i > 1
       1                otherwise

A top-down approach of computing, say, f(5) is inefficient do to repeated subcomputations.

                 |---------f(5)----------|
               f(4)                    f(3)
        f(3)----------f(2)         f(2)-----f(1)
     |---------|     |-----|     |------|
  |f-(2)-|    f(1)  f(1)  f(0)   f(1)   f(0)
f (1)   f(0)

A bottom-up approach computes f(0), f(1), f(2), f(3), f(4), f(5) in the listed order.

21.2 0/1 Knapsack Problem

21.3 All Pairs Shortest Paths (Floyd-Warshall)

A dynamic programming algorithm.

FOR k=1 TO n 
  FOR i=1 TO n 
    FOR j=1 TO n 
      c(i,j,k) = min(  c(i,j,k-1), 
                       c(i,k,k-1)+c(k,j,k-1) 
                    ) 
 oao --1---boo---7---coo
7|  ----  |3 ---- |2
 oo -5-----oo----1-  oo
 d   8    e   4   f
k = a b c d e f a 1  oo 7 5  oo b 7  oo 3  oo c  oo 1 2 d 8  oo e 4 f
k = {a} a b c d e f a 1  oo 7 5  oo b 7,b-a-c  oo ,b-a-d 3,b-a-e  oo ,b-a-f c  oo ,c-a-d 1,c-a-e 2,c-a-f d 8,d-a-e  oo ,d-a-f e 4,e-a-f f
k = {a, b}
k = {a, b, c}
k = {a, b, c, d}
k = {a, b, c, d, e}
k = {a, b, c, d, e, f}

21.4 Demo Applets

21.5 Assignment #9 (due We, Dec 1)

  1. Show the tree of memory states traversed by our recursive permutation algorithm for input “a,b,c,d”.
  2. Provide a recursive backtracking algorithm for the vertex cover problem.
  3. Provide a branch-and-bound algorithm for the 0/1 knapsack problem.
  4. Provide a dynamic programming algorithm for the coin exchange problem.