Backtracking
What is backtracking?
• It is a systematic search strategy of the state-space of
combinatorial problems
• It is mainly used to solve problems which ask for
finding elements of a set which satisfy some
constraints.
• Many problems which can be solved by backtracking
have the following general form:
“ Find S subset of A1 x A2 x … x An (Ak – finite sets) such
that each element s=(s1,s2,…,sn) satisfy some
constraints”
What is backtracking?
Basic ideas:
• each partial solution is evaluated in order to establish if it is
promising (a promising solution could lead to a final solution while
a non-promising one does not satisfy the partial restrictions
induced by the problem constraint)
• if all possible values for a component do not lead to a promising
partial solution then one come back to the previously component
and try another value for it
• backtracking implicitly constructs a state space tree:
- The root corresponds to an initial state (before the search for a
solution begins)
- An internal node corresponds to a promising partial solution
- An external node (leaf) corresponds to either to a non
promising partial solution or to a final solution
General Algorithm
State space tree construction according to
Depth First Search
N-Queens problem
N-queens problem: Place N queens on an N x N
grid (chess board) so that no more than 1 queen
is on any vertical, diagonal, or horizontal line
(i.e., no queens can attack each other in chess).
If a queen can attack another, it is called a
“conflict”.
Backtracking algorithm of N-Queens problem
4-Queen’s problem
Solution of 4-Queens problem
Hamiltonian circuit problem
• A Hamiltonian circuit is defined as a cycle that
passes through all the vertices of the graph
exactly once.
• It is also defined as a sequence of n+1
adjacent vertices vi0, vi1, vi2, … vin-1, vi0, where
the first vertex of the sequence is the same as
the last one while all the other n-1 vertices are
distinct.
Subset-sum problem
• Find a subset of a given set S= {s1,…sn} of n
positive integers whose sum is equal to a
given positive integer d.
• Sort the set’s elements in increasing order. So
we will assume that
s1<= s2<=… sn
• Construct a state space tree as a binary tree
Construction of State-space-tree
• 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 inclusion and
exclusion of s1 respectively in a set being sought
• Similarly, going to the left from a node of the first level
corresponds to inclusion s2, while going to the right
corresponds to its exclusion, and so on
• 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.
• If s’ is not equal to d, we can terminate the node
as nonpromising if either of the following two
inequalities holds
(1) s’ + si+1 > d ( the sum s’ is too large)
(2) S’ + ( the sum s’ is too small)

Backtracking (11111111111111111111).pptx

  • 1.
  • 2.
    What is backtracking? •It is a systematic search strategy of the state-space of combinatorial problems • It is mainly used to solve problems which ask for finding elements of a set which satisfy some constraints. • Many problems which can be solved by backtracking have the following general form: “ Find S subset of A1 x A2 x … x An (Ak – finite sets) such that each element s=(s1,s2,…,sn) satisfy some constraints”
  • 3.
    What is backtracking? Basicideas: • each partial solution is evaluated in order to establish if it is promising (a promising solution could lead to a final solution while a non-promising one does not satisfy the partial restrictions induced by the problem constraint) • if all possible values for a component do not lead to a promising partial solution then one come back to the previously component and try another value for it • backtracking implicitly constructs a state space tree: - The root corresponds to an initial state (before the search for a solution begins) - An internal node corresponds to a promising partial solution - An external node (leaf) corresponds to either to a non promising partial solution or to a final solution
  • 4.
  • 5.
    State space treeconstruction according to Depth First Search
  • 6.
    N-Queens problem N-queens problem:Place N queens on an N x N grid (chess board) so that no more than 1 queen is on any vertical, diagonal, or horizontal line (i.e., no queens can attack each other in chess). If a queen can attack another, it is called a “conflict”.
  • 7.
    Backtracking algorithm ofN-Queens problem
  • 9.
  • 10.
  • 11.
    Hamiltonian circuit problem •A Hamiltonian circuit is defined as a cycle that passes through all the vertices of the graph exactly once. • It is also defined as a sequence of n+1 adjacent vertices vi0, vi1, vi2, … vin-1, vi0, where the first vertex of the sequence is the same as the last one while all the other n-1 vertices are distinct.
  • 12.
    Subset-sum problem • Finda subset of a given set S= {s1,…sn} of n positive integers whose sum is equal to a given positive integer d. • Sort the set’s elements in increasing order. So we will assume that s1<= s2<=… sn • Construct a state space tree as a binary tree
  • 13.
    Construction of State-space-tree •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 inclusion and exclusion of s1 respectively in a set being sought • Similarly, going to the left from a node of the first level corresponds to inclusion s2, while going to the right corresponds to its exclusion, and so on • 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
  • 14.
    • We recordthe value of s’, the sum of these numbers, in the node. • If s’ is equal to d, we have a solution to the problem. • If s’ is not equal to d, we can terminate the node as nonpromising if either of the following two inequalities holds (1) s’ + si+1 > d ( the sum s’ is too large) (2) S’ + ( the sum s’ is too small)