Anda di halaman 1dari 4

BACKTRACKING

The idea of backtracking is to construct solutions one component at a time and evaluate such
partially constructed solutions. This partially constructed solution can be developed further
without violating the problem constraints.
It is convenient to implement this kind of processing by constructing a tree of choices being
made called the "State Space Tree". Its root represents an initial state before the search for the
solution begins. The nodes of the first level in the tree represent the choices for the first
component of a solution and the nodes of a second level represent the choices for the second
component and so on.
A node in the state space tree is promising if it corresponds to the partials constructed
solution that may lead to the complete solution otherwise the nodes are called non-promising
and are backtracked. Leaves of the tree represent either the non- promising dead end or
complete solution found by the algorithm.

1. Basic Idea – 0/1 Knapsack:


If we are given n objects and a knapsack or a bag in which the object I with weight w[i] is to
be placed. The knapsack has a capacity m. The profit value that can be earned is p[i]; then the
objective is to obtain filling up of knapsack with maximum profit earned, but it should not
exceed the capacity m of knapsack. To fill the knapsack we select the object as come weight
and having some profit. This selected object is put in the knapsack. Thus knapsack is filled up
with selected object.

The problem may be stated as:


MaximizeΣp[i].xi (1 ≤ i ≤ n)
Subject to Σw[i].xi ≤ m (1 ≤ i ≤ n)
p[i]> 0, w[i]> 0 and x is either 0 or 1.
Where
1≤i≤n

The profit and weight are positive numbers. Here a feasible solution is any set ( x1, x2,....., xn
)Satisfying the equation 2 and 3 and an optimal solution is a feasible solution for which
equation 1 is maximum.
Note that: -
1. Here we cannot consider any object in fraction i.e., the objects may not be broken into
smaller pieces. So we have to decide either to take an object or to leave it fully.

2. Algorithm:
Algorithm Bound(cp,cw,k)
//cp is the current profit total, cw is the current weight total, k is the index of //the last
removed item, and m is the knapsack size.
{
b≔ cp;
c≔ cw;
for i≔ k+1 to n do
{
c≔ c+w[i];
if(c<m) then b≔ b+p[i];
else return b+(1-(c-m)/w[i])*p[i];
}
return b;
}

Algorithm BKnap(k,cp,cw)
// m is the knapsack size; n is the number of weights and profits. w[ ] and p[ ] //are the
weights and profits, p[i]/w[i] ≥ p[i+1]/w[i+1]. fw is the final weight of //the knapsack, fp is
the final maximum profit, x[k]=0 if w[k] is not in the //knapsack else x[k]=1.
{
//generate left child
If(cw+w[k] ≤ m) then
{
x[k] ≔ 1;
if(k<n) then BKnap(k+1, cp+p[k], cw+w[k]);
if((cp+p[k] > fp and (k = n)) then
{
fp ≔ cp + p[k]; fw≔cw + w[k];
}
}
//generate right child.
If ( Bound(cp,cw,k) ≥ fp) then
{
x[k]≔ 0;
if(k < n) then BKnap(k+1, cp, cw);
if((cp > fp) and (k = n)) then
{
fp≔ cp;fw≔ cw;

}
}
}

3. Example:
w1, w2, w3, w4 = (2, 5, 10, 5)
p1, p2, p3, p4 = (40, 30, 50, 10)
[n = 4]
m = 16

The state space tree:


The corresponding bound, profit and weight are marked in order for each node-
Here, B stands for Backtracking and f.s. for feasible solution. As the profit value exceeds
bound value we need to backtrack the path.

Description:
Node a– since no item has been included till now , cp=0 and cw=0 and k=1 according to the
algorithm.
Node b –k=1
Cw+w[k] = 0+2 = 2 < 16
Therefore,
x[k] = x[1] = 1
Since k<n ,
BKnap(k+1, cp+p[k], cw+w[k] = BKnap(2,0+40,0+2) = BKnap(2,40,2) is called.
Node d= it is left to node a,
Cw+w[k] = 2+5 = 7 < 16
Therefore,
x[k] = x[2] = 1
Since k<n,
BKnap(k+1, cp+p[k], cw+w[k] = BKnap(3,40+30,2+5) = BKnap(3,70,7) is called.
Node f= it is left to node d,
Cw+w[k] = 7+10 = 17 < 16
The condition is false. Therefore,
X3 can’t be included in the knapsack and the solution is backtracked. We again go to node d
and find out its right child.
Node g= it is right to node d,
Bound(cp,cw,k) = bound(70,7,3) is called.
According to the algorithm of bound,
b≔ cp;
c≔ cw;
Therefore, b = 70 and c = 7.
i=k+1 to n i.e, i=4 to 4
C = c+w[4] = 7+5 = 12
C< m therefore, return b=b+p[4]= 70+10 = 80
Since 80>-1
So x3=0
K<n ; BKnap(4,70,7)
Node j= it is left to node g
Cw+w[k] = 7+5 = 12< 16
Therefore,
x[k] = x[4] = 1
Since cp+p[k] > fp (70+10=80>-1)
and k=n ,(4 = 4)
fp = cp + p[k] =80 ; fw = cw + w[k] = 12;
Therefore,
Feasible Solutions are:

x1 x2 x3 x4 Profit Weight
1. (1 , 1 , 0 , 1) 80 12
2. (1 , 1 , 0 , 0) 70 07
3. (1 , 0 , 1 , 0) 90 12
4. (1 , 0 , 0 , 1) 50 07
5. (1 , 0 , 0 , 0) 40 02

Out of these feasible solutions, (x1, x2, x3, x4) = (1, 0, 1, 0) is the optimal solution with
profit = 90.

Anda mungkin juga menyukai