UNIT-III
UNIT-III
UNIT-III
Since each application of this procedure requires 0{n 2) time, the matrix A
can be obtained in 0(n3) time. We obtain an alternate o(n 3) solution to this
problem using the principle of optimality.
Clearly, A0(i, j) = cost{i, j),1 < i < n, 1 < j < n. We can obtain a recurrence
for Ak(i,j) using an argument similar to that used before. A 2(l,3)≠ min{A1(l,3),
A1 (1,2)+Al(2,3)}= 2
If we want to find the shortest path not just between sand t but between all
pairs of vertices then, one approach would be to execute our general
shortest-path algorithm from |V| times, once for each starting node. The
total running time would then be O(|V|2|E|). We'll now see a better
alternative, the O(|V|3)dynamic programming- based Floyd-War shall
algorithm.
More concretely, number the vertices in Vas {1, 2,3 ….,n}, and let
dist(i; j; k) denote the length of the shortest path from I to j in which only
nodes {1,2,…,k} can be used as intermediates. Initially, dist (i;j;0)is the
length of the directed ge between i and j, if it exist, and is α otherwise.
Let distl[u] be the length of a shortest path from the source vertex v to
vertex u under the constraint that the shortest path contains at most l
edges. Then, dist1[u] = cost[v,u], 1 ≤ u ≤ n. As noted earlier, when there are
no cycles of negative length, we can limit our search for shortest paths to
paths with at most n - 1 edges. Hence, distn-1[u] is the length of an
unrestricted shortest path from v to u.
Our goal then is to compute distn-1[u] for all u. This can be done using the
dynamic programming methodology. First, we make the following
observations:
1. If the shortest path from v to u with at most k, k > 1, edges has not
more than k - 1 edges, then distk[u] - distk-1[u].
2. If the shortest path from v to u with at most k, k > 1, edges has exactly k
edges, then it is made up of a shortest path from v to some vertex j
followed by the edge (j,u). The path from v to j has k- 1 edges, and its
length is distk-1[j]. All vertices j such that the edge (j, u) is in the graph are
candidates for j. Since we are interested in a shortest path, the i that
minimizes distk-1[i] + cost[i, u] is the correct value for j.
These observations result in the following recurrence for dist: distk[u] = min
{distk-1[u], min {distk-1[i] + cost[i,u]}}. This recurrence can be used to
compute distk from distk-1, for k= 2, 3,..., n - 1.
Algorithm BellmanFord(v, cost, dist, n)
Step 1: for i := 1 to n do Step 2:dist[i] := cost[v, i]; Step 3: end for
Step 4: for k := 2 to n - 1 do
Step 5:for each u such that u≠v and u has at least
one incoming edge
Step 6:for each <i, u> in the graph Step 7:if dist[u] >dist[i] + cost[i,u]
Step 8:dist[u] := dist[i] + cost[i,u]; Step 9:end if
Step 10:end for Step 11:end for Step 12: end for
When we know the frequency of searching each one of the keys, it is quite
easy to compute the expected cost of accessing each node in the tree. An
optimal binary search tree is a binary search tree which has minimal
expected cost of locating each node. In our problem, we are not concerned
with the frequency of searching for a missing node. For example:
Node ID 0 1 2 3 4 5
Key A B C D E F
Frequenc 4 1 1 2 8 16
y
Optimal Binary search tree ex1. [ 2*1 + (1+8)*2 + (4+1+16)*3] = 83
It's clear that the tree in fig 4.3 is not optimal. - It is easy to see that the
nodes having higher frequencies are closer to the root, and then tree
will have a lower expected cost.
Equation 4.6 can be solved for c(0,n) by first computing all c(i,j) such
that j-i=1. Next we can compute all c(i,j) such that j-i =2, then all c(i,j) with
j-i=3, etc. if during this computation we record the root r(i,j) of each tree tij,
then an optimal binary search tree can be constructed from these r(i,j).
=3+3+2 =8
=min {0+0} +8 =8
R01=1
=3+1+3 =7
=min{0+0}+7 =7
R12=2
=1+1+1 =3
=min{0+0}+3 =3
R23=3
=min{0+0}+3 =3
R23=4
=3+1+8 =12
=min{8+0}+ 12 =20
R23=1
=1+1+7 =9
R23=2
=1+1+3 =5
=min {0+ 3} +5 =8
R24=3
=1+1+12 =14
R03=1
=1+1+9 =11
R14=2
=1+1+14 =16
Algorithm
Algorithm OBST(p, q, n)
for i :=0 to n
w[i,i]:=q[i];r[i,i]:=0;c[i,i]:=0.0;
r[i,i+l]:=i+ l;
for m :=2 to n do
for i :=0 to n
j :=i +m;
k :=Find(c, r, i, j);
0/1KNAPSACK
The capacity remaining in the knapsack is m and no profit has accrued or the
capacity remaining is m- wn and a profit of pn has accrued. It is clear that the
remaining decisions Xn,..., X must be optimal with respect to the problem
state resulting from the decision on Xn
Can be solved for fn(m) by beginning with the knowledge fo(y) = 0 for ally and
fi(y) = -α, y<0. Then f1,f2,..., fn can be successively computed using.
We use the ordered set Si={(f(yj), yj)|1<j<k} to represent fi(y). Each member
of Si is a pair (P, W), where P = fi( yj) and W = yj. Notice that S0 = {(0, 0)}.
We can computeSl+1from Sl by first computing.
Consider the knapsack in stance n = 3, (W1, W2, W3) = (2, 3, 4), (P1,P2, P3) =
(1, 2, 5), and m = 6. For these data we have
S0 = {(0,0)}; S1 0={(1,2)}
S1 = {(0,0),(1,2)}; S11={(2,3),(3,5)}
S3 = {(0,0),(1,2),(2,3),(5,4),(6,6),(7,7),(8,9)
PLEFT(O)= 190,
PLEFT(l)= 90,
PLEFT(2)= 40,
PLEFT(3)= 20,
PLEFT(4)= 10,
S5= {160}
If the heuristic were not used, then the computation would have proceeded
as
S0 = {0}
S1 = {0,100}
S2 = {0,50,100,150}
S3 = {0,20,50,70,100,120,150}
S4 = {0,10,20,30,50,60,70,80,100,110,120,130,150,160}
S5 = {0,7,10,17,20,27,30,37,50,57,60,67,70,77,80,87,100,
107,110,117,120,127,130,137,150,157.160}
The value fe(165)can now be determined from S5, using the knowledge
(P6,W6)=(3,3).
and cij = α if (i,j) ∉ E. Let |V| = n and assume n > 1. A tour of G is a directed simple
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 traveling salesperson problem is to find a tour of
minimum cost.
In the following discussion, without losing the main concept, we take the
consists of an edge (1,k) for some k ∈ V - {1} and a path from vertex k to vertex
tour as a simple path that starts and ends at the starting vertex. Every tour
{1,k}. Hence, the principle of optimality holds. 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.
The function g(1, V - {1}) is the length of an optimal salesman’s tour. From the
principal of optimality it follows that
In general
The above equation can be solved for g(1, V - {1}) if we know g(k, V - {1, k}) for
all choices of k. The g values can be obtained by using this equation. Clearly, g(i,
Ø) = cj1, 1≤ i ≤ n. Hence, we can use this equation to obtain g(i, S) for all S of size
1. Then we can obtain g(i,S) for S with |S| = 2, and so on. When |S|< n - 1, the
values of i and S for which g(i, S) is needed are such that i ≠ 1, 1∉ S, and i ∉S.
Consider the directed graph of Fig 4.5(a). The edge lengths are given by matrix
c of
g(3,{2}) = 18 g(3,{4}) = 20
g(4,{2}) = 13 g(4,{3}) = 15
Finally, we obtain
g(1, {2,3,4}) = min {c12+g(2, {3,4}), c13+g(3, {2,4}), c14+g(4, {2, 3})}
= min {35,40,43}
= 35
An optimal tour of the graph of Figure has length 35. A tour of this length can be
constructed if we retain with each g(i, S) the value of j that minimizes the right-
hand side of the graph. Let J(i,S) be this value. Then, J(1,{2,3,4}) = 2. Thus the
tour starts from 1 and goes to 2. The remaining tour can be obtained from g(2,
{3, 4}). SoJ(2, {3, 4}) = 4. Thus the next edge is (2,4). The remaining tour is for
g(4, {3}). So J(4, {3}) = 3. The
TRAVELING SALESPERSON PROBLEM
One vertex represents the post office from which the postal van starts and to
which it must return. Edge (i, j) is assigned a cost equal to the distance from
site i to site j. The route taken by the postal van is a tour, and we are
interested in finding a tour of minimum length.
a b
c d
=min 35