Unit 4
Unit 4
Generalmethod(Textbook T2:7.1)
GeneralAlgorithm(Recursive)
GeneralAlgorithm(Iterative)
N-Queensproblem
The problem is to place n queens on an n × n chessboard so that no two queens attack each
other by being in the same row or in the same column or on the same diagonal.
So let us consider the four-queens problem and solve it by the backtracking technique.Since
each of the four queens has to be placed in its own row, all we need to do is to assign a
column for each queen on the board presented in figure.
We start with the empty board and then place queen 1 in the first possible position of its row,
which is in column 1 of row 1. Then we place queen 2, after trying unsuccessfully columns 1
and 2, in the first acceptable position for it, which is square (2, 3), the square in row 2 and
column 3. This proves to be a dead end because there is no acceptable position for queen 3.
So, the algorithm backtracks and puts queen 2 in the next possible position at (2, 4). Then
queen 3 is placed at (3, 2), which proves to be another dead end. The algorithm then
backtracks all the wayto queen 1 and moves it to (1, 2). Queen 2 then goes to (2, 4), queen 3
to(3, 1), and queen 4 to (4, 3), which is a solution to the problem. The state-space tree of this
search is shown in figure.
If other solutions need to be found, the algorithm can simply resume its operations at the leaf
at which it stopped. Alternatively, we can use the board’s symmetry for this purpose.
Finally, it should be pointed out that a single solution to the n-queens problem for any n ≥ 4
can be found in linear time.
Note: The algorithm NQueens() is not in the syllabus. It is given herefor interested learners.
The algorithm is referred from textbook T2.
Sumofsubsets problem
Problemdefinition:FindasubsetofagivensetA ={a1,...,an}ofnpositiveintegers whose sum is
equal to a given positive integer d.
Forexample,forA={1, 2,5,6,8}andd=9,therearetwosolutions:{1,2,6}and{1,8}. Of course,
some instances of this problem may have no solutions.
Itisconvenienttosorttheset’selementsinincreasingorder.So,wewillassume that
a1<a2<. . . <an.
Thestate-spacetreecan beconstructedasabinarytreelikethatinFigureshownbelowfor the
instance A = {3, 5, 6, 7} and d = 15.
The number inside a node is the sum of the elements already included in the subsets
representedbythenode. Theinequalitybelowaleafindicatesthereason foritstermination.
The root of the tree represents the starting point, with no decisions about the given elements
made as yet. Its left and right children represent, respectively, inclusion and exclusion of a 1 in
a set being sought.
Similarly, going to the left from a node of the first level corresponds to inclusion of a 2 while
goingto the right corresponds to its exclusion, andso on. Thus, a path from the root to a node
on the ith level of the tree indicates which of the first in numbers have been included in the
subsets represented by that node.
We record the value of s, the sum of these numbers, in the node. If s is equal to d, we have a
solution to the problem. We can either report this result and stop or, if all the solutions needto
be found, continue by backtracking to the node’s parent. If s is not equal to d, we can
terminate the node as non-promising if either of the following two inequalities holds:
Graph coloring
Analysis
Hamiltoniancycles
2. BranchandBound
Recall that the central idea of backtracking, discussed in the previous section, is to cut off a
branch of the problem’s state-space tree as soon as we can deduce that it cannot lead to a
solution. This idea can be strengthened further if we deal with an optimization problem.
An optimization problem seeks to minimize or maximize some objective function (a tour
length, the value of items selected, the cost of an assignment, and the like), usuallysubject to
some constraints. An optimal solution is a feasible solution with the best value of the
objective function (e.g., the shortest Hamiltonian circuit or the most valuable subset of items
that fit the knapsack).
Comparedtobacktracking,branch-and-boundrequirestwoadditionalitems:
1. a way to provide, for every node of a state-space tree, a bound on the best value of
the objective function on any solution that can be obtained by adding further
components to the partially constructed solution represented by the node
2. thevalueofthe bestsolutionseen sofar
In general, we terminate a search path at the current node in a state-space tree of a branch-
and-bound algorithm for any one of the following three reasons:
1. The value of the node’s bound is not better than the value of the best solution seen so
far.
2. The node represents no feasible solutions because the constraints of the problem are
alreadyviolated.
3. Thesubset offeasiblesolutions representedbythe nodeconsists ofasinglepoint (and
hence no further choices can be made)—in this case, we compare the value of the
objective function for this feasible solution with that of the best solution seen so far
and update the latter with the former if the new solution is better.
AssignmentProblem
Let us illustrate the branch-and-bound approach by applying it to the problem of assigning n
people to n jobs so that the total cost of the assignment is as small as possible.
An instance of the assignment problem is specified by an n × n cost matrix C so that we can
state the problem as follows: select one element in each row of the matrix so that no two
selected elements are in the same column and their sum is the smallest possible. We will
demonstrate how this problem can be solved using the branch-and-bound technique by
considering the small instance of the problem. Consider the data given below.
How can we find a lower bound on the cost of an optimal selection without actually solving
the problem?
We can do this by several methods. For example, it is clear that the cost of any solution,
includinganoptimalone, cannot be smaller than the sum of the smallest elements in each
of the matrix’s rows. For the instance here, this sum is 2 + 3+ 1+ 4 = 10.We can and will
apply the same thinking to partially constructed solutions. For example, for any legitimate
selection that selects 9 from the first row, the lower bound will be 9 + 3 + 1+ 4 = 17.
Ratherthangenerating asinglechild ofthelast promisingnodeas wedid in backtracking, we will
generate all the children of the most promising node among non-terminated leaves in the
current tree. (Non terminated, i.e., still promising, leaves are also called live.) How can wetell
which of the nodes is most promising? We can do this bycomparing the lower bounds of the
live nodes. It is sensible to consider a node with the best bound as most promising, although
this does not, of course, preclude the possibility that an optimal solution will ultimatelybelong
to a different branch of the state-space tree. This variation of the strategyis called the best-
first branch-and-bound.
We start with the root that corresponds to no elements selected from the cost matrix. The
lower-bound value for the root, denoted lb, is 10. The nodes on the first level of the tree
correspond to selections of an element in the first row of the matrix, i.e., a job for person a.
See the figure given below.
Figure: Levels 0 and 1 of the state-space tree for the instance of the assignment
problem being solved with the best-first branch-and-bound algorithm. The number
above a node shows the order in which the node was generated. A node’s fields
indicate the job number assigned to person a and the lower bound value, lb, for this
node.
So we have four live leaves—nodes 1 through 4—that may contain an optimal solution. The
most promising of them is node 2 because it has the smallest lower bound value. Following
our best-first search strategy, we branch out from that node first by considering the three
different ways of selecting an element from the second row and not in the second column- the
three different jobs that can be assigned to person b. See the figure given below (Fig12.7).
Of the six live leaves—nodes 1, 3, 4, 5, 6, and 7—that may contain an optimal solution, we
again choose the one with the smallest lower bound, node 5. First, we consider selecting the
third column’s element from c’s row (i.e., assigning person c to job 3); this leaves us with no
choice but to select the element from the fourth column of d’s row (assigning person d to job
4). This yields leaf 8 (Figure 12.7), which corresponds to the feasible solution {a→2, b→1,
c→3, d →4} with the total cost of 13. Its sibling, node 9, corresponds to the feasible solution
{a→2,b→1, c→4, d →3} with the total cost of 25. Since its cost is larger than the cost of the
solution represented by leaf 8, node 9 is simply terminated. (Of course, if its cost weresmaller
than 13, we would have to replace the information about the best solution seen so far with the
data provided by this node.)
Now, as we inspect each of the live leaves of the last state-space tree—nodes1, 3, 4, 6, and 7
in Figure 12.7—we discover that their lower-bound values are not smaller than 13, the value
of the best selection seen so far (leaf 8). Hence, we terminate all of them and recognize the
solution represented by leaf 8 as the optimal solution to the problem.
TravellingSalesPerson problem
We will be able to apply the branch-and-bound technique to instances of the traveling
salesman problem if we come up with a reasonable lower bound on tour lengths. One very
simple lower bound can be obtained by finding the smallest element in the intercity distance
matrix D and multiplying it by the number of cities n.
But there is a less obvious and more informative lower bound for instances with symmetric
matrix D, which does not require a lot of work to compute. We can compute a lower bound on
the length l of anytour as follows. For each cityi, 1≤ i ≤ n, find the sum s i ofthe distances from
city i to the two nearest cities; compute the sums of these n numbers, divide the resultby 2,
and, if all the distances are integers, round up the result to the nearest integer:
lb =⌈s/2⌉... (1)
Forexample,fortheinstanceinFigure2.2a,formula(1)yields
Moreover, for any subset of tours that must include particular edges of a given graph, we can
modifylower bound (formula 1) accordingly. For example, for all the Hamiltonian circuits of
the graph in Figure 2.2a that must include edge (a, d), we get the following lower bound by
summing up the lengths of the two shortest edges incident with each of the vertices, with the
required inclusion of edges (a, d)and (d, a):
We now apply the branch-and-bound algorithm, with the bounding function given byformula-
1, to find the shortest Hamiltonian circuit for the graph in Figure 2.2a.
Toreducethe amountofpotentialwork,wetakeadvantageoftwo observations.
1. First,withoutlossofgenerality,wecan consider onlytoursthatstartat a.
2. Second, because our graph is undirected, we can generate only tours in which b is
visited before c. (Refer Note at the end of section 2.2 for more details)
In addition, after visiting n−1= 4 cities, a tour has no choice but to visit the remaining
unvisited city and return to the starting one. The state-space tree tracing the algorithm’s
application is given in Figure 2.2b.
Note: An inspection of graph with 4 nodes (figure given below) reveals three pairs of tours
that differ only by their direction. Hence, we could cut the number of vertex permutations by
half. We could, for example, choose any two intermediate vertices, say, b and c, and then
consider only permutations in which b precedes c. (This trick implicitly defines a tour’s
direction.)
Figure:Solutiontoasmallinstanceofthetravelingsalesmanproblembyexhaustive search.
Figure 2.2(a)Weighted graph. (b) State-space tree of the branch-and-bound algorithm to find
a shortest Hamiltonian circuit in this graph. The list of vertices in a node specifies abeginning
part of the Hamiltonian circuits represented by the node.
Discussion
The strengths and weaknesses of backtracking are applicable to branch-and-bound as well.
The state-space tree technique enables us to solve many large instances of difficult
combinatorial problems. As a rule, however, it is virtually impossible to predict which
instances will be solvable in a realistic amount of time and which will not.
In contrast to backtracking, solving a problem by branch-and-bound has both the challenge
and opportunity of choosing the order of node generation and finding a good bounding
function. Though the best-first rule we used above is a sensible approach, it may or may not
lead to a solution faster than other strategies. (Artificial intelligence researchers are
particularly interested in different strategies for developing state-space trees.)
Finding a good bounding function is usuallynot a simple task. On the one hand, we want this
function to be easy to compute. On the other hand, it cannot be too simplistic - otherwise, it
would fail in its principal task to prune as many branches of a state-space tree as soon as
possible. Striking a proper balance between these two competing requirements may require
intensive experimentation with a wide variety of instances of the problem in question.
3. 0/1Knapsackproblem
Note:ForthistopicasperthesyllabusbothtextbooksT1 &T2aresuggested. Here
we discuss the concepts from T1 first and then that of from T2.
TopicformT1 (Levitin)
Let us now discuss how we can apply the branch-and-bound technique to solving the
knapsack problem. Given n items of known weights wi and values vi, i = 1, 2, . . . , n, and a
knapsack of capacity W, find the most valuable subset of the items that fit in the knapsack.
∑◻◻◻◻≤◻ ∑◻◻◻◻◻◻◻◻◻◻◻◻◻◻◻,◻ℎ◻◻◻◻◻=0◻◻1
◻◻◻ 1≤◻≤◻
1≤◻≤◻
It is convenient to order the items of a given instance in descending order by their value-to-
weight ratios.
Each node on the ith level of state space tree, 0 ≤ i ≤ n, represents all the subsets of n itemsthat
include a particular selection made from the first i ordered items. This particularselection is
uniquely determined by the path from the root to the node: a branch going to the left indicates
the inclusion of the next item, and a branch going to the right indicates its exclusion.
We record the total weight w and the total value v of this selection in the node, along with
some upper bound ub on the value of anysubset that can be obtained by adding zero or more
items to this selection. A simple way to compute the upper bound ub is to add to v, the total
value of the items already selected, the product of the remaining capacity of the knapsack W
–wandthebestperunitpayoffamongtheremaining items,whichisvi+1/wi+1:
ub = v+ (W− w)(vi+1/wi+1).
Example: Consider the followingproblem. Theitems are alreadyordered in descending order
of their value-to-weight ratios.
Let us apply the branch-and-bound algorithm. At the root of the state-space tree (see Figure
12.8), no items have been selected as yet. Hence, both the total weight of the items already
selected w and their total value v are equal to 0. The value of the upper bound is 100.
Node 1, the left child of the root, represents the subsets that include item 1. The total weight
and value of the items already included are 4 and 40, respectively; the value of the upper
bound is 40 + (10 − 4) * 6 = 76.
Node 2 represents the subsets that do not include item 1. Accordingly, w = 0, v = 0, and ub =
0 + (10 − 0) * 6 = 60. Since node 1 has a larger upper bound than the upper bound of node 2,
it is more promising for this maximization problem, and we branch from node 1 first. Its
children—nodes 3 and 4—represent subsets with item 1 and with and without item 2,
respectively. Since the total weight w of every subset represented by node 3 exceeds the
knapsack’s capacity, node 3 can be terminated immediately.
Node4hasthe samevaluesof wandvas itsparent; theupper boundubis equal to40 +(10
− 4) * 5 = 70. Selecting node 4 over node 2 for the next branching (Due to better ub), we get
nodes5and 6byrespectivelyincludingand excludingitem3. Thetotalweightsand values as well
as the upper bounds for these nodes are computed in the same way as for the preceding nodes.
Branching from node 5 yields node 7, which represents no feasible solutions, and node 8,
which represents just a single subset {1, 3} of value 65. The remaining live nodes 2 and 6
havesmallerupper-bound valuesthanthevalueofthesolution represented bynode8. Hence, both
can be terminated making the subset {1, 3} of node 8 the optimal solution to the problem.
ConceptsformtextbookT2 (Horowitz)
Letusunderstandsomeoftheterminologiesusedin backtracking&branchandbound.
Livenode-anodewhichhasbeengeneratedandallofwhosechildrenarenot yetbeen generated.
E-node-isalivenodewhosechildrenarecurrentlybeingexplored. In otherwords,anE- node is a
node currently being expanded.
Deadnode-anodethatiseithernottobeexpandedfurther, orforwhichallofits children have
been generated
BoundingFunction-willbeusedtokilllivenodeswithoutgeneratingalltheirchildren.
Backtracking -is depthfirst nodegenerationwith boundingfunctions.
Branch-And-Bound is amethodin whichE-noderemainsE-nodeuntil itis dead.
Breadth-First-Search:Branch-andBoundwitheachnewnodeplacedinaqueue.The front of
the queen becomes the new E-node.
Depth-Search(D-Search):Newnodesareplacedintoastack.Thelastnodeaddedisthe first to be
explored.
0/1Knapsackproblem-BranchandBoundbasedsolution
As the technique discussed here is applicable for minimization problems, let us convert the
knapsack problem (maximizing the profit) into minimization problem by negating the
objective function
LC(LeastCost)BranchandBoundsolution
FIFOBranchandBound solution
Conclusion