0% found this document useful (0 votes)
25 views

backtracking

Uploaded by

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

backtracking

Uploaded by

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

BACKTRACKING

Introduction
• Some problems can be solved, by exhaustive search. The exhaustive-
search technique suggests generating all candidate solutions and then
identifying the one (or the ones) with a desired property
• Backtracking is a more intelligent variation of this approach.
Backtracking
• The principal idea is to construct solutions one component at
a time and evaluate such partially constructed candidates as
follows.
• If a partially constructed solution can be developed further
without violating the problem’s constraints, it is done by taking the
first remaining legitimate option for the next component.
• If there is no legitimate option for the next component, no
alternatives for any remaining component need to be considered.
• In this case, the algorithm backtracks to replace the last
component of the partially constructed solution with its next
option.
• 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 a solution
begins.
• The nodes of the first level in the tree represent the choices made
for the first component of a solution; the nodes of the second level
represent the choices for the second component, and soon.
• A node in a state-space tree is said to be promising if it corresponds
to a partially constructed solution that may still lead to a complete
solution; otherwise, it is called non-promising.
• Leaves represent either non-promising dead ends or complete
solutions found by the algorithm.
• In the majority of cases, a state space tree for a backtracking
algorithm is constructed in the manner of depth-first search.
• If the current node is promising, its child is generated by adding
the first remaining legitimate option for the next component of a
solution, and the processing moves to this child.
• If the current node turns out to be non-promising, the algorithm
backtracks to the node’s parent to consider the next possible
option for its last component; if there is no such option, it
backtracks one more level up the tree, and so on.
• Finally, if the algorithm reaches a complete solution to the
problem, it either stops (if just one solution is required) or
continues searching for other possible solutions.
N-Queens Problem
• 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.
• Consider 4 –Queens Problem
• 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
• 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 way to 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.
Subset-Sum problem
Find a subset of a given set A = {a1, . . . , an } of
n positive integers whose sum is equal to a given
positive integer d.
For example, for A = {1, 2, 5, 6, 8} and d = 9,
there are two solutions: {1, 2, 6} and {1, 8}.
• The state-space tree can be constructed as a binary tree for the
instance A = {3, 5, 6, 7} and d = 15.
• 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 a1 in a set being
sought.
• Similarly, going to the left from a node of the first level corresponds to
inclusion of a2 while going to the right corresponds to its exclusion, and
so on.
• Thus, a path from the root to a node on the ith level of the tree
indicates which of the first I 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 need to
be found, continue by backtracking to the node’s parent. If s is not
equal to d,
We can terminate the node as nonpromising if
either of the following two inequalities holds:

You might also like