Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Nikita D Patil Algorithmic Designing Roll No:41

Unit 4: - Backtracking and Branch and Bound


1. Knapsack Problem
Knapsack Problem algorithm is a very helpful problem in combinatorics.
In the supermarket there are n packages (n ≤ 100) the package i has weight
W[i] ≤ 100 and value V[i] ≤ 100. A thief breaks into the supermarket, the
thief cannot carry weight exceeding M (M ≤ 100). The problem to be solved
here is: which packages the thief will take away to get the highest value?

Input:

 Maximum weight M and the number of packages n.


 Array of weight W[i] and corresponding value V[i].

Output:

 Maximize value and corresponding weight in capacity.


 Which packages the thief will take away?

Knapsack algorithm can be further divided into two types:

 The 0/1 Knapsack problem using dynamic programming. In this


Knapsack algorithm type, each package can be taken or not taken.
Besides, the thief cannot take a fractional amount of a taken package
or take a package more than once. This type can be solved by
Dynamic Programming Approach.
 Fractional Knapsack problem algorithm. This type can be solved by
Greedy Strategy.

Analyze the 0/1 Knapsack Problem


When analyzing 0/1 Knapsack problem using Dynamic programming, you
can find some noticeable points. The value of the knapsack algorithm
depends on two factors:

1. How many packages are being considered


2. The remaining weight which the knapsack can store.

Therefore, you have two variable quantities.


With dynamic programming, you have useful information:

1. the objective function will depend on two variable quantities


2. the table of options will be a 2-dimensional table.

If calling B[i][j] is the maximum possible value by selecting in packages {1,


2, ..., i} with weight limit j.

 The maximum value when selected in n packages with the weight


limit M is B[n][M]. In other words: When there are i packages to
choose, B[i][j] is the optimal weight when the maximum weight of the
knapsack is j.
 The optimal weight is always less than or equal to the maximum
weight: B[i][j] ≤ j.

For example: B[4][10] = 8. It means that in the optimal case, the total
weight of the selected packages is 8, when there are 4 first packages to
choose from (1st to 4th package) and the maximum weight of the knapsack
is 10. It is not necessary that all 4 items are selected.

Formula to Calculate B[i][j]


Input, you define:

 W[i], V[i] are in turn the weight and value of package i, in which i
{1, …, n}.
 M is the maximum weight that the knapsack can carry.

In the case of simply having only 1 package to choose. You calculate B[1][j]
for every j: which means the maximum weight of the knapsack ≥ the weight
of the 1st package

B[1][j] = W[1]

With the weight limit j, the optimal selections among packages {1, 2, ..., i – 1,
i} to have the largest value will have two possibilities:

 If package i is not selected, B[i][j] is the maximum possible value by


selecting among packages {1, 2, ..., i – 1} with weight limit of j. You
have:

B[i][j] = B[i – 1][j]


 If package i is selected (of course only consider this case when W[i] ≤
j) then B[i][j] is equal to the value V[i] of package i plus the maximum
value can be obtained by selecting among packages {1, 2, ..., i – 1}
with weight limit (j – W[i]). That is, in terms of the value you have:

B[i][j] = V[i] + B[i – 1][j – W[i]]

Due to the creation of B[i][j], which is the maximum possible value, B[i][j]
will be the max of the above 2 values.

2. Traveling Salesman Problem


A traveler needs to visit all the cities from a list, where distances between
all the cities are known and each city should be visited just once. What is
the shortest possible route that he visits each city exactly once and returns
to the origin city?

Solution

Travelling salesman problem is the most notorious computational


problem. We can use brute-force approach to evaluate every possible tour
and select the best one. For n number of vertices in a graph, there are (n -
1)! number of possibilities.

Instead of brute-force using dynamic programming approach, the solution


can be obtained in lesser time, though there is no polynomial time
algorithm.

Let us consider a graph G = (V, E), where V is a set of cities and E is a set of
weighted edges. An edge e(u, v) represents that vertices u and v are
connected. Distance between vertex u and v is d(u, v), which should be
non-negative.

Suppose we have started at city 1 and after visiting some cities now we are
in city j. Hence, this is a partial tour. We certainly need to know j, since this
will determine which cities are most convenient to visit next. We also need
to know all the cities visited so far, so that we don't repeat any of them.
Hence, this is an appropriate sub-problem.
For a subset of cities S Є {1, 2, 3, ... , n} that includes 1, and j Є S, let C(S,
j) be the length of the shortest path visiting each node in S exactly once,
starting at 1 and ending at j.

When |S| > 1, we define C(S, 1) = ∝ since the path cannot start and end
at 1.

Now, let express C(S, j) in terms of smaller sub-problems. We need to start


at 1 and end at j. We should select the next city in such a way that

C(S,j)=minC(S−{j},i)+d(i,j)wherei∈Sandi≠jc(S,j)=minC(s−{j},i)
+d(i,j)wherei∈Sandi≠jC(S,j)=minC(S−{j},i)
+d(i,j)wherei∈Sandi≠jc(S,j)=minC(s−{j},i)+d(i,j)wherei∈Sandi≠j

Algorithm: Traveling-Salesman-Problem

C ({1}, 1) = 0

for s = 2 to n do

for all subsets S Є {1, 2, 3, … , n} of size s and containing 1

C (S, 1) = ∞

for all j Є S and j ≠ 1

C (S, j) = min {C (S – {j}, i) + d(i, j) for i Є S and i ≠ j}

Return minj C ({1, 2, 3, …, n}, j) + d(j, i)

You might also like