Clearly, M(i,i)=0 for all i, and M(1,n) is what we are looking for.
Cost(T') = Cost(L') + Cost(R) + Cost(BC) < Cost(L) + Cost(R) + Cost(BC) = Cost(T)
M_{ij}=cost(T) =Cost(L)+Cost(R)+Cost(BC) =M_{ik} + M_{k+1,j} + P_{i}P_{k+1}P_{j+1}.
M_{11}=0 | M_{22}=0 | M_{33}=0 | M_{44}=0 | |
M_{12}=105 | M_{23}=105 | M_{34}=84 | ||
M_{13}=150 k=1 | M_{24}=165 k=3 | |||
M_{14}=186 k=3 |
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
p_{i} = Prob[a_{i} is accessed], i=1,2,...,n
q_{i} = Prob[accessing an element X, a_{i} < X < a_{i+1}]
q_{0} = Prob[accessing an element X, X < a_{1}]
q_{n} = Prob[accessing an element X, a_{n} < X]
cost(a_{i}) = level(a_{i}) + 1
cost(X, a_{i} < X i+1) = level(E_{i})
where
T_{0n} is the final tree being sought
T_{00} is empty
T_{i,i+1} is a single-node tree that has element a_{i+1}
SUM^{j}_{s=i+1} p_{s}{level_{
Tij}(a_{s}) + 1} =
SUM^{k-1}_{s=i+1} p_{s}{level_{
Ti,k-1}(a_{s}) + 1} +
SUM^{j}_{s=k+1} p_{s}{level_{
Ti,k-1}(a_{s}) + 1} +
SUM^{j}_{s=i+1} p_{s}
Similar arithmetic will show that
SUM^{j}_{s=i} q_{s}level_{
Tij}(E_{s}) =
SUM^{k-1}_{s=i}q_{s}level_{
Ti,k-1}(E_{s}) +
SUM^{j}_{s=k} q_{s}level_{
Tkj}(E_{s}) +
SUM^{j}_{s=i} q_{s}
W_{ij} = SUM^{j}_{s=i+1} p_{s} +
SUM^{j}_{s=i} q_{s}
This procedure computes the weights W_{ij}s 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 C_{ij}s and the r_{ij}s 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 T_{ij} 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 T_{ij} 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 T_{0n} 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
p_{1}=1/10, p_{2}=2/10, p_{3}=3/10, p_{4}=1/10
q_{0}=0, q_{1}=1/10, q_{2}=1/20, q_{3}=1/20, q_{4}=1/10
W_{00}=0 | W_{11}=1/10 | W_{22}=1/20 | W_{33}=1/20 | W_{44}=1/10 |
W_{01}=2/10 | W_{12}=3.5/10 | W_{23}=4/10 | W_{34}=2.5/10 | |
W_{02}=4.5/10 | W_{13}=7/10 | W_{24}=6/10 | ||
W_{03}=8/10 | W_{14}=9/10 | |||
W_{04}=10/10 |
C_{00}=0 | C_{11}=0 | C_{22}=0 | C_{33}=0 | C_{44}=0 |
C_{01}=2/10 r_{01}=1 | C_{12}=3.5/10 r_{12}=2 | C_{23}=4/10 r_{23}=3 | C_{34}=2.5/10 r_{34}=4 | |
C_{02}=6.5/10 r_{02}=2 | C_{13}=10.5/10 r_{13}=3 | C_{24}=8.5/10 r_{24}=3 | ||
C_{03}=14/10 r_{03}=2 | C_{14}=15/10 r_{14}=3 | |||
C_{04}=19/10 r_{04}=3 |
The left subtree is then T_{02} and the right subtree is T_{34}
T_{34} is a single-node tree having a_{4} because r_{34}=4
T_{02} has as root a_{2} because r_{02}=2
The left subtree of T_{02} is T_{01} and its right subtree is T_{22} (which is empty)
T_{01} is a single-node tree having a_{1} because r_{01}=1
this completes the tree (to be drawn in class).