III. Steps of Dynamic Programming
IV. First Application: The Matrix Chain Problem
Clearly, Mii=0 for all i, and M1n is what we are looking for.
Cost(T') = Cost(L') + Cost(R) + Cost(BC) < Cost(L) + Cost(R) + Cost(BC) = Cost(T)
Mij=cost(T) =Cost(L)+Cost(R)+Cost(BC) =Mik + Mk+1,j + PiPk+1Pj+1.
M11=0 | M22=0 | M33=0 | M44=0 | |
M12=105 | M23=105 | M34=84 | ||
M13=150 k=1 | M24=165 k=3 | |||
M14=186 k=3 |
V. Second Application: The All-Pairs Shortest Path Problem
A(0)(i,j) = W[i,j].
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
VI. Third Application: Optimal Binary Search Trees
pi = Prob[ai is accessed], i=1,2,...,n
qi = Prob[accessing an element X, ai < X < ai+1]
q0 = Prob[accessing an element X, X < a1]
qn = Prob[accessing an element X, an < X]
cost(ai) = level(ai) + 1
cost(X, ai < X < ai+1) = level(Ei)
where
T0n is the final tree being sought
T00 is empty
Ti,i+1 is a single-node tree that has element ai+1
∑js=i+1 ps{level
Tij(as) + 1} =
∑k-1s=i+1 ps{level
Ti,k-1(as) + 1} +
∑js=k+1 ps{level
Ti,k-1(as) + 1} +
∑js=i+1 ps
Similar arithmetic will show that
∑js=i qslevel
Tij(Es) =
∑k-1s=iqslevel
Ti,k-1(Es) +
∑js=k qslevel
Tkj(Es) +
∑js=i qs
Wij = ∑js=i+1 ps +
∑js=i qs
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
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 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).