0% found this document useful (0 votes)
15 views5 pages

Homework 6

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views5 pages

Homework 6

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

COMP 460 ALGORITHMS AND COMPLEXITIES

HOMEWORK-6

Jiwan Pokharel

BACKTRACKING

INTRODUCTION

Backtracking is a method of determining the solution which is optimal to the given pool of
solutions by eradicating the unsatisfactory solutions. Here, the ploy of removing the unwanted
solutions is employed even before they are examined. This implies, those solutions which do not
meet the preset standards or criteria, are indiscriminately eliminated from the context, eventually
leading to a smaller and simpler solution space. This is the reason; the solution tends to be
interpreted in the form of non-cyclic graph, usually the tree, where the root tends to represent the
set of all the solutions. Thus, if any node of the tree does not satisfy the criterion, the whole sub-
tree that consists of the non-compliant node is rejected.

Backtracking is certainly a very good way of determining the compliant solutions from a set of
other possible solutions. It tends to employ the depth first technique as used in the tree structure.
For the purpose, the candidates or the objects used by this algorithm or method can be ordered,
such that the length of the tree structure can be minimized.

This method goes on to find a solution without knowing about it initially. This means, at the start
the solution set is empty, but as the search progresses, the solution sets go on accumulating. This
process is continued until a complete solution is defined and identified such that this solution
complies with the pre-defined criterion. Also, once a solution is identified, it is also possible to
seek for the other solutions which might be optimal than the previous ones. Moreover, at times it
is possible to only determine the partial solutions rather than the complete solution, due to the set
criterion or constraints. Thus, in cases when no more solution can be identified, the backtracking
algorithm stops.

The major principle governing the backtracking methodology is creation of the solutions
continually and evaluate them such that if there are partially constructed candidates which
comply with the set criterion of the problem, then they are kept or accepted else, removed.
Generally backtracking algorithms are slow or time consuming, thus various other algorithms
like pruning, etc are used in association to it for the sole purpose of speeding up the work.

APPLICATIONS OF BACKTRACKING

Backtracking is a popular technique which can be utilized in many mathematical, scientific as


well as financial fields. Basically, it can find its great usage in the areas where optimal solutions
COMP 460 ALGORITHMS AND COMPLEXITIES

are of great concern. Few of the fields for the application of backtracking are enumerated as
follows:

1. Travelling Salesman problem


2. Optimal Scheduling Problem
3. N-Queens Problem
4. Jigsaw Problem
5. Sudoku Problem
6. Solitaire Solution Problem

0/1 KNAPSACK PROBLEM

The 0/1 Knapsack problem can also make the use of back tracking algorithm. Here, we are given
a knapsack with ‘n’ number of objects such that i= 1 ,2,3,4,…,n and each of these objects are
assigned their weights ‘wi’ and values ‘vi’ respectively. The ambition of this problem is to
determine the maximum values of the objects which shall fill the knapsack such that the total
weights of those objects won’t exceed ‘W’. As this is a 0/1 problem, we are only allowed to take
either the whole/complete object or not. We shall not be allowed to take any fractional values of
the object. Thus, we can express the problem mathematically as follows:

i=1 i=1
Maximize ∑ ¿¿ i
n
) Such that: ∑ ¿¿ i)
n
<=W

Thus, this mathematical representation depicts that the solution to fill the knapsack shall contain
all those objects whose weight shall not exceed the given maximum Weight, but shall contain the
maximum sum of the values vi .

EXAMPLE

The example depicts a 0/1 Knapsack problem with 5 different objects. These objects have the
weights as 3,4,9,10,12 units respectively and their corresponding values are 4,15,18,25,30. Now,
we are provided this problem to determine a solution such that we can maximize these values
where the total weight does not exceed 15. These weights and values are represented in the
tabular form:

Weights(wi) 3 4 9 10 12
Values(vi) 4 15 18 25 30
COMP 460 ALGORITHMS AND COMPLEXITIES

Fig: Tree Structure for backtracking Knapsack objects

In this solution, we shall take the objects in their ascending order of weights. Though it is not
essential to stick to a specific ordering of the weight values, we have selected this approach as it
allows reducing the depth of the trees when considered in either ascending or the descending
order. Furthermore, each node on the tree structure depicted by the example can be interpreted as
follows:
3,9;22

This implies, the node consists of objects of weight 3 & 9, and the summation of their values is
22. We shall interpret all the other nodes similarly. Also, it is notable that the number of objects
in each node is equal to the depth of that node in the given tree structure.

INTERPRETING THE SOLUTION

The tree structure thus constructed shall be parsed in the Depth First Search technique. Thus, for
each node, the depth is parsed, rather than parsing through the breadth. Here, once we start from
root node (;0), we shall parse through the tree as follows:

( ;0)-----> (3;4)---->(3,3;8)---->(3,3,3;12)----->(3,3,3,3;16)----->(3,3,3,3,3;20)
COMP 460 ALGORITHMS AND COMPLEXITIES

From this parsing the current optimal solution shall be (3,3,3,3,3;20)

Similarly, parsing the second branch, we get,

( ;0)----->(3;4)----->(3,3;8)------->(3,3,4;23)------>(3,3,4,4;39)

As of now, the optimal solution becomes (3,3,4,4;39)

Again, let us parse through other branches:

( ;0)---->(3;4)----->(3,3;8)----->(3,3,9;26)

(;0)---(3;4)----->(3,4;19)------>(3,4,4;34)------->(3,4,4,4;49)

As, for now the new optimal solution is (3,4,4,4;49).

Again, parsing through all other branches, we come to a conclusion that the highest count of
values is in node (3,4,4,4;49). Thus, the best solution is the set of objects with weights: 3,4,4 and
4, totaling the value of 49.

ALGORITHM

The following algorithm employs back tracking mechanism to determine appropriate weighted
objects that satisfy the condition of having maximum weight W. Here, arrays w[] and v[]
represents the weights and values corresponding to each objects respectively. Also, n represents
the number of the objects and W represents the maximum weight allowed to be used in the
knapsack.

1. function backTrackSack(m,W)
2. {
3. value =0;
4. for i = m to n
5. {
6. if (w[i]<=W)
7. {
8. value =maximum(value, v[i]+backTrackSack(i,W-w[i]))
9. }
10. return Value;
11. }

The above depicted function performs the depth first search in the tree structure thus created and
determines an appropriate solution that satisfies the given condition. Line 8 in this function
performs the DFS.
COMP 460 ALGORITHMS AND COMPLEXITIES

Also, we define the maximum() function as follows:


function maximum(x,y)
{
if(x>y)
return x;
return y;
}

This function is a simple helper function which returns the largest value among the given two
parameters.

REFERENCES
[1]Gilles Brassard, et. Al., “Fundamentals of Algorithms”, 1996
[2] Dr. Mohammed Tounsi, “Design and Analysis of Algorithms”, CS311, PSU
[3] Hojjat Ghaderi,” Intro to Artificial Intelligence”,CSC384, University of Toronto
[4] “Fundamentals of Computer Science”, CS307
[5] Amritha Krishna, “http://www.scribd.com/doc/52339735/Algorith-Analysis-TSP-with-
backtracking”

You might also like