Dynamic Programming

  1. I. Perspective

  2. II. Principle of Optimality

  3. III. Steps of Dynamic Programming

  4. First Application: The Matrix Chain Problem

  5. Second Application: The All-Pairs Shortest Path Problem

  6. Third Application: Optimal Binary Search Trees



I. Perspective

Back to Top

II. Principle of Optimality

Back to Top

III. Steps of Dynamic Programming

Back to Top

IV. First Application: The Matrix Chain Problem

Back to Top

V. Second Application: The All-Pairs Shortest Path Problem


Procedure APSP(input: W[1:n,1:n];A[1:n,1:n]) begin for i=1 to n do for j=1 to n do A(0)(i,j) := W[i,j]; endfor endfor for k=1 to n do for i=1 to n do for j=1 to n do A(k)(i,j)=min(A(k-1)(i,j),A(k-1)(i,k) + A(k-1)(k,j)) endfor endfor endfor end


Procedure APSP(input: W[1:n,1:n];A[1:n,1:n]) begin for i=1 to n do for j=1 to n do A(i,j) := W[i,j]; endfor endfor for k=1 to n do for i=1 to n do for j=1 to n do A(i,j)=min(A(i,j),A(i,k) + A(k,j)); endfor endfor endfor end

Back to Top

VI. Third Application: Optimal Binary Search Trees

  • Algorithm:

    
    
    This procedure computes the weights Wijs Procedure Weight(Input:p[1:n], q[0:n]; Output: W[0:n,0:n]) begin for i=1 to n do W[i,i] = q(i); endfor for l=1 to n do for i=0 to n-l do k = i+l; W[i,k]=W[i,k-1] + p[k] + q[k]; endfor endfor end

    
    
    This procedure computes the Cijs and the rijs Procedure OBST(Input:p[1:n], q[0:n], W[0:n,0:n]; Output: C[0:n,0:n], r[0:n,0:n]) begin for i=0 to n do C[i,i] := 0; endfor for l=1 to n do for i=0 to n-l do j=i+l; C[i,j] := infinity; m := i+1; --m keeps the index of the min for k=i+1 to j do if C[i,j] >= C[i,k-1] + C[k,j] then C[i,j] := C[i,k-1] + C[k,j]; m := k; endif endfor C[i,j] := C[i,j] + W[i,j]; r[i,j] := m; endfor endfor end

    
    
    This procedure creates the tree Tij Procedure create-tree(Input: r[0:n,0:n], a[1:n], i, j; Output: T) begin if (i==j) then T=null; return; endif T := new(node); -- the root of Tij k := r[i,j]; T --> data := a[k]; if (j==i+1) return; endif create-tree(r[0:n,0:n], a[1:n], i, k-1; T --> left); create-tree(r[0:n,0:n], a[1:n], k, j; T --> right); end

    
    
    This procedure is the master program that creates the whole tree T0n Procedure Final-tree(Input: a[1:n],p[1:n],q[1:n]; Output: T) begin Weight(p[1:n], q[0:n], W[0:n,0:n]); OBST(p[1:n], q[0:n], W[0:n,0:n], C[0:n,0:n], r[0:n,0:n]); create-tree(r[0:n,0:n], a[1:n], 0, n, T); end

  • Example:
    • a1 < a2 < a3 < a4

      p1=1/10, p2=2/10, p3=3/10, p4=1/10

      q0=0, q1=1/10, q2=1/20, q3=1/20, q4=1/10

      W00=0 W11=1/10 W22=1/20 W33=1/20 W44=1/10
      W01=2/10 W12=3.5/10 W23=4/10 W34=2.5/10
      W02=4.5/10 W13=7/10 W24=6/10
      W03=8/10 W14=9/10
      W04=10/10

      C00=0 C11=0 C22=0 C33=0 C44=0
      C01=2/10
      r01=1
      C12=3.5/10
      r12=2
      C23=4/10
      r23=3
      C34=2.5/10
      r34=4
      C02=6.5/10
      r02=2
      C13=10.5/10
      r13=3
      C24=8.5/10
      r24=3
      C03=14/10
      r03=2
      C14=15/10
      r14=3
      C04=19/10
      r04=3

    • The tree T04 has as root a3 because r04=3

      The left subtree is then T02 and the right subtree is T34

      T34 is a single-node tree having a4 because r34=4

      T02 has as root a2 because r02=2

      The left subtree of T02 is T01 and its right subtree is T22 (which is empty)

      T01 is a single-node tree having a1 because r01=1

      this completes the tree (to be drawn in class).

    Back to Top