Professional Documents
Culture Documents
TREES (Contd..)
• The expected cost of the tree is
P(K)+COST(L)+COST(R)+W(0,k-1)+W(k,n)…….....(3)
• If T is optimal then (3) must be minimum over all
binary search trees containing a1,…ak-1 and
E0,E1,…..,Ek-1
• Similarly cost(R) must be minimum.
• Let C(i,j) represent the cost of the tree Tij
(containing nodes ai+1,……aj and Ei,…..,Ej).
• Then cost(l) = C(0,k-1) and cost(R) =C(k,n)
substituting for cost(l) and cost(R) in (3),
1
OPTIMAL BINARY SEARCH TREES (Contd..)
• The expected cost of the search tree is
• P(k)+C(0,k-1)+C(k,n)+W(0,k-1)+W(k,n)…..(4) is
minimum if k is chosen such that (4) is minimum cost
of the BST with nodes a1,a2,…..,an and E0,….,En
C(0,n)=min{C(0,k-1)+C(k,n)+P(k)+W(0,k-1)+W(k,n)}…..(5)
1<=k≤n
substitute i instead of 0 and j instead of n in (5)
C(i, j) = min { C(i, k-1)+ C (k, j)+ P (k) + W(i, k-1) + W (k, j)}
= min {C(i,k-1)+C(k,j)+W(i,j)}…………..(6)
i<k≤j
P(k) + W(i, k-1) + W (k, j)= P(k) +p(i+1)+…+P(k-1)+P(k+1)
+…+P(j) = P(i+1)+…+P(k)+P(k+1)+…P(j) =W(i,j)
2
OPTIMAL BINARY SEARCH
TREES (Contd..)
• Equation (6) may be solved for C(0,n) by
first computing C(i,j) such that j-i=1
(C(i,i)=0 and W(i,i)=Q(i),0≤i≤n)
• We can compute all C(i,j) such that j-i = 2,
then all C(i,j) with j-i=3 etc.
• If during these computations, the root R(i,j)
of each tree Tij is recorded, then OBST may
be constructed.
3
Optimal Binary Search Trees –
Example
Example: Let n = 4 and (a1,a2,a3,a4) = (do, if, read, while)
Let P(1:4)=(3,3,1,1) and Q(0:4)=(2,3,1,1,1) .
• P’s and Q’s have been multiplied by 16 for convenience.
• Initially ,W(i,j) = Q(i), C(i,i) = 0, R(i,i) = 0 , 0≤i≤4
J j-1
W(i,j) =Q(i) +∑ Q(l) +P(l) =P(j) +Q(j) +Q(i) + ∑ Q(l) +P(l)
l=i+1 l=i+1 ….…(7)
= P(j) +Q(j) +W(i,j-1)
4
Optimal Binary Search Trees –
Example (contd..)
C(i,j) = min{C(i,k-1)+C(k,j)} +W(i,j)……….…(8)
i<k≤j
Using (7), W(0,1) = P(1) +Q(1) +W(0,0)
=3+3+Q(0)=3+3+2=8
Using (8) C(0,1) = W(0,1) +min{C(0,0) +C(1,1)}
= 8 +min{0+0}
R(0,1) = 1 because R(i,j) is the value of k that
minimizes(8)
5
Optimal Binary Search Trees –
Example (contd..)
W(1,2) =P(2) +Q(2) +W(1,1) =P(2) +Q(2) +Q(1) =3+1+3 =7
C(1,2) =W(1,2) +min{C(1,1) +C(2,2)}=7 +min{0,0)=7
R(1,2) =2
W(2,3) =P(3) +Q(3) +W(2,2) =3 k=2
C(2,3) =W(2,3) +min{C(2,2)+C(3,3)} =3 k=3
R(2,3) =3
W(3,4) =P(4) +Q(4) +W(3,3) =1+1+Q(3) =3
C(3,4) =W(3,4) +min{C(3,3) +C(4,4)} =3 k=4
R(3,4) =4
6
Optimal Binary Search Trees –
Example (contd..)
• Knowing W(i,i+1), and C(i,i+1) 0≤i<4 , we are
computing W(i,i+2) ,C(i,i+2) R(i,i+2) 0≤i<3
• This process may be repeated until W(0,4) ,C(0,4)
and R(0,4) are obtained
• Using R(i,j) let us see how to construct an OBST.
• C(o,4)=32 is the minimum cost of BST for
{ a1, a2, a3, a4}.
do, if, read, while
7
Optimal Binary Search Trees –
Example (contd..)
• The root of tree T04 is a2
as R(0,4)=2
• The left sub-tree of T04 is T01 and right sub-tree is T24.
• R (0, 1) =1 a1 is the root of T01,
• R (2, 4) =3
a3 is root of T24,
• T01 has T00 and T11 as sub-trees , T24 has T22 and T34
• For T00,T11 and T22 the root is 0, T24 it is 4.
8
Optimal Binary Search Trees –
Example (contd..)
a2={if}
if
do Read
while
OBST
9
Optimal Binary Search Trees –
Example (contd..)
We can verify that the cost of OBST
= ∑P (i) level (ai)+∑Q(i) (level(Ei)-1)
1≤i≤n 0≤i≤n
= 2×3+1×3+2×1+3×1+2×2+2×3+2×1+3×1+3 ×1
= 6+3+2+3+4+6+2+3+3 = 32
10
COMPLEXITY OF THE PROCEDURE
FOR CONSTRUCTING OBST
C(i,j)=min{C(i,k-1)+C(k,i)}+w(i,j)
i<k≤j
• C (i,j)s are computed for j-i=1,2,…,n. If j-i=m then
there are n-m+1 C (i,j)s to compute
• (for n=4, and j-i =1,they are C(0,1), C(1,2), C(2,3),
C(3,4))
• Each C(i,j) requires to compute m quantities
• Hence each C (i,j) can be computed in o(m) time.
The total time for all c(i,j)s with j-i=m is 0(n-
m+1)(m)=0(nm-m2+m)=0(nm-m2) .
11
COMPLEXITY OF THE PROCEDURE
FOR CONSTRUCTING OBST (Contd..)
12
COMPLEXITY OF THE PROCEDURE
FOR CONSTRUCTING OBST (Contd..)
• The complexity can be reduced to 0(n2)
using D.E.Knuth’s result which limits the
search to the range
• R(i,j-1)≤k≤R(i+1,j), instead of
min{C(i,k-1)+C(k,i)}+w(i,j)
1<k≤j
• Using the values of R (i,j),the tree T0n can
be constructed in 0(n) time.
13
ALGORITHM FOR OBST
PROCEDURE OBST (P,Q,n)
// Given n distinct identifiers a1<a2<…. <an and
probability P (i) 1≤i≤n, Q (i) 0≤i≤n, the cost of
OBST C (i,j) is computed//
// R (i,j) is the root of tree //
// w (i.j) is the Weight of OBST //
Real P (n), q (0: n), C (0: n, 0: n),
W (0: n, 0: n); integer R (0: n, 0: n)
14
ALGORITHM FOR OBST (Contd..)
w(i,j)←w(i,j-1)+P(j)+Q(j)
k←a value l in the range
R (i,j-1)≤l≤R(i+1,j) that
Minimizes{C (i, l-1) +C (l,j)}
C(i,j)=w(i,j)+C(i,k-1)+C(k,j)
R (i,j)←k
Repeat
Repeat
End OBST
16
0/1 KNAPSACK PROBLEM
• The solution of 0/1 knapsack problem is a sequence
of decisions on x1,…, xn and the total profit;
x1,…..,xn may be 0 or 1.
• Let fj(X) be the value of an optimal solution to
KNAP (1, j, X).
• Since the principle of optimality holds
fn (M) = max {fn-1(M) (Corresponding to xn=0),
fn-1(M-wn) +pn} (corresponding to xn=1)
17
0/1 KNAPSACK PROBLEM (Contd..)
• In general
fi(X)=max{fi-1(X), fi-1(X-wi)+pi}….(1)
• (1) may be solved by beginning with f0(X)=0 for
all X (no elements case) and
fi(X)= -∞ for X<0 (no weight case)
f1,…,fn may be solved successively.
EXAMPLE:-
• Let n=3, (w1,w2,w3) = (2,3,4);
• (P1,P2,P3) = (1,2,3), M= 6
18
0/1 KNAPSACK PROBLEM (Contd..)
SOLUTION:
f0(X) = 0 X, fi(X) = -∞ if X<0
f0 (6) = 0
f1(6) = max{f0(6), f0(6-2)+1} = max {0,1} = 1
f2(6) = max{f1(6), f1(6-3)+2} = max{1,f1(3)+2}
f1(3) = max{f0(3), f0(3-2)+1} = max{0,1}=1
f2(6) = max {1, 3} =3
f3(6) = max{f2(6), f2(6-4)+5}=max{3, f2(2)+5}
f2(2) = max{f1(2), f1(2-3)+2}=max{1, f1(-1)+2} = 1
(as f1 (-1) = -∞)
f3(6)=max{3,f2(2)+5}=max{3,6}=6 19
0/1 KNAPSACK PROBLEM (Contd..)
20
0/1 KNAPSACK PROBLEM (Contd..)
• Also fi(X)=fi(Wj) for all X such that Wj≤X<Wj+1 0≤j<r
(as no increase in profit for X>=Wj . and
fi(X)=fi(Wr) for all X X>Wr (as only 0, 1, 2, ...r-1
objects are there.
• (fj(X) is the value of an optimal solution to
KNAP(1,j,X)
• (For such X’s as 2.5, 3.5, we do not have the profit, so
we assign the previous profit.)
21
0/1 KNAPSACK PROBLEM (Contd..)
22
0/1 KNAPSACK PROBLEM (Contd..)
• If one of Si-1 and Si1 has pair (pj,wj) and the other has a pair
(pk,wk) and pj≤pk while wj≥wk then the pair (pj,wj) is
discarded.
• This is known as dominance rule or purging rule (pk,wk)
dominates (pj,wj).
For example
S0 contains tuples when x1 = 0. p1 = 0 ; w1 = 0 S0={0,0}
S11 contains tuples when x1 =1 p1 = 1; w1 = 2 S11={(1,2)}
S1 contains tuples when x1 = 0 and x1 = 1 S1 = {(0,0), (1,2)}
23
0/1 knapsack problem example
n = 3, (w1, w2, w3) = (2,3,4), (p1,p2,p3)= (1,2,5) and M=6.
• S0 = {0, 0}; S11 is obtained by adding (p1, w1)=(1, 2) to (0, 0)
Thus S11 = {(1,2)} ; S1 is obtained by combining S0 and S11
Hence S1 = {(0, 0), (1, 2)}; S21= {(2, 3), (3, 5)} adding
(2, 3) to elements of S1
• S2 ={(0,0),(1,2), (2,3),(3,5)}; S31={(5,4),(6,6),(7,7),(8,9)}
• S3 ={(0,0),(1,2),(2,3),(5,4),(6,6),(7,7),(8,9)}
• The pair (3,5) is eliminated from S3 as 3<5 and 5>4. (Using
purging rule (3,5) is discarded.
24
0/1 knapsack problem example (contd..)
26
Informal Knapsack Algorithm
Procedure DKP (P, W, n, M)
S0= {0, 0)}
For i←1 to n do
Si1← {(P1, W1) / P1- pi,W1-wi Si-1 and W1≤M}
// Si1 is constructed by adding (pi,wi) to
tuples in Si-1 //
Si← MERGE-PURGE (Si-1, Si1)
repeat
(PX, WX)← Last tuple in Sn-1
27
Informal Knapsack Algorithm (Contd..)
29
Detailed knapsack algorithm
Procedure DKNAP(p,w,n,M,m)
Real p(n), w(n), P(m), W(m), pp, ww, M
Integer F(0:n), l, h, u, i, j, p, next // p(n), w(n) are //
F(0)← 1; P(1)← W(1)← 0 // S0 // // given arrays //
l← h← 1 // start and end of S0 // // P(m), W(m) are //
// computed pair //
F(1)← next← 2 // next free spot in P and W //
4 for i← 1 to n do
30
Detailed knapsack algorithm (Contd..)
31
Detailed knapsack algorithm (Contd..)
33
Detailed knapsack algorithm (Contd..)
Example:
• n=3, M=6, (w1,w2,w3) = (2,3,4), (p1,p2,p3)= (1,2,5)
• S0= {0,0}; S11= {(1,2)} S1={(0,0), (1,2)}; S21=
{(2,3), (3,5)}
• S2= {(0,0), (1,2), (2,3), (3,5)}; S31={ (5,4), (6,6),
(7,7), (8,9)}
• S3= {(0,0), (1,2), (2,3), (5,4), (6,6), (7,7), (8,9)}
35
Detailed knapsack algorithm (Contd..)
P 0 0 1 0 1 2 3 0 1 2 5 6
W0 0 2 0 2 3 5 0 2 3 4 6
↑ ↑ ↑ ↑
F(0) F(1) F(2) F(3)
l←4 h←7 F(2+1)= F(3)←Next= 8 (line 27)
k←l = 4
36
Detailed knapsack algorithm (Contd..)
38
Detailed knapsack algorithm (Contd..)
40
Detailed knapsack algorithm (Contd..)
41
THE TRAVELING SALESPERSON
PROBLEM
• 0/1 Knapsack problem is a subset selection
problem. Given n objects there are 2n
different subsets of objects.
• The solution is one of these 2n subsets.
• In permutation problems like Traveling
Salesperson Problem(TSP) there are n!
different permutations of n objects. (n!>2n).
42
THE TRAVELING SALESPERSON
PROBLEM (Contd..)
TSP problem is as follows:
• Let G = (V,E) be directed graph with edge costs
Cij. Cij>0 for all i and j and Cij = +∞ if <i,j> E.
• Let |V|=n and assume n>1.
• A tour of G is a directed cycle that includes every
vertex in V.
• The cost of a tour is the sum of the cost of the
edges on the tour.
• The TSP problem is to find a tour of minimum
cost.
43
THE TRAVELING SALESPERSON
PROBLEM (Contd..)
Applications of TSP:-
1. A postal van is to pick up mail from mail boxes
located at n different sites. The route taken by a
postal van is a tour and the problem is to find a
tour of minimum length.
2. A minimum cost tour of a robot arm used to
tighten the nuts on some piece of machinery on an
assembly line will minimize the time needed for
the arm to complete its task.
44
TSP AND THE PRINCIPLE
OF OPTIMALITY
• Assume a tour to be a simple path that starts and
ends at vertex 1.
• Every tour consists of an edge <1,k> for some k
V-{1} and a path from vertex k to vertex 1.
• The path from vertex k to vertex 1 goes through
each vertex in V-{1,k} exactly once.
• If the tour is optimal, then the path from k to 1
must be a shortest k to 1 path going through all
vertices in V-{1,k}.
• Hence the principle of optimality holds.
45
TSP AND THE PRINCIPLE OF
OPTIMALITY (Contd..)
• Let g(i,S) be the length of a shortest path starting
at vertex i, going through all vertices in S and
terminating at vertex 1.
• S is the set of all vertices through shortest path goes
• g(1, V-{1}) is the length of an optimal salesperson
tour.
• Form the principle of optimality, it follows that
g(1, V-{1})= min{C1k+g(k,V-{1,k})} ……(1)
2≤k≤n (forward approach)
46
TSP AND THE PRINCIPLE OF
OPTIMALITY (Contd..)
• In general, for iS,
g(i,S)= min{Cij+g(j, S-{j})}………..(2)
js
• (1) may be solved for g(1, V-{1}) if we know g(k,
V-{1,k}) for all choices of k.
• These values are obtained from (2)
• g(i,Ø) = Ci1 1≤i≤n.
• g(i,Ø) is the length of the shortest path starting at i
going through no vertex and ending at 1.
47
Solution of TSP using Dynamic programming
50
Solution of TSP using Dynamic
programming contd..
We compute g (i,S) with | S | = 3
g (1, {2, 3, 4}) = min { C12 + g (2,{3,4}), C13 + g (3, {2,4})
, C14 + g (4, {2,3})
= min { 10 + 25, 15 + 25, 20+23 }
= min { 35, 40, 43 } = 35
• Thus an optimal tour of the graph has length 35.
51
Solution of TSP using Dynamic
programming contd..
• The tour of this length may be constructed if we
retain the value of j that minimizes each g(i,S) for
| S | = 3 and | S | = 2 and | S | = 1.
• Let J (i,S) be the minimal tour.
• J (1, {2,3,4}) = 2 as C12 + g(2,{3,4}) = 35 is the
minimum.
52
Solution of TSP using Dynamic
programming contd..
53
Complexity of TSP
Complexity of TSP = (n22n)
Space needed = (n2n)
Complexity involves the computations of
g(1, V-{1}) or g(i, S)
Let N be the number of g(i, S) to be computed
For each values of /S/, there are n-1 choices of i
The number of distinct sets S of size k not including
1 and i is (n-2) C k
Hence N = Σ (n-1) (n-2) C k k= 0, 1, …, n-2
54
Complexity of TSP contd..
N = (n-1) Σ (n-2) C k k= 0, 1, …, n-2 = (n-1) 2 (n-2)
(Applying Binomial theorem below with a=1 and
x=1)
Binomial theorem is as follows
(a +x)n = a n n C 0 + a n-1 nC 1 x + a n-2 nC2 x 2 +…+nC n xn
As g(1, V-{1}) involves the computation of g(i, s) maximum
n times, the Complexity is n(n2n) =n22n
As g(i, s) are to be stored for further computations,
The storage required is (n2n)
55