UNIT 5 Backtracking 1714885860265
UNIT 5 Backtracking 1714885860265
Backtracking
Solution Strategies
• Exhaustive Search: (brute force)
• useful only for small instances
• Dynamic Programming:
• applicable to some problems (e.g., the knapsack problem)
• Backtracking:
• eliminates some unnecessary cases from consideration
• yields solutions in reasonable time for many instances but
worst case is still exponential
• Branch-and-Bound
• further refines the backtracking idea for optimization problems
Backtracking
• Backtracking is applied to difficult combinatorial problems for which
no efficient algorithms for finding exact solutions possibly exist.
• Unlike the exhaustive search approach, which is doomed to be
extremely slow for all instances of a problem, backtracking at least
holds a hope for solving some instances of nontrivial sizes in an
acceptable amount of time.
• For optimization problems, the idea of backtracking can be further
enhanced by evaluating the quality of partially constructed solutions.
• Even if backtracking does not eliminate any elements of a problem’s
state space and ends up generating all its elements, it provides a
specific technique for doing so, which can be of value in its own right.
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 so on.
• The edges represent choices in extending partial solutions
• 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 nonpromising.
• Leaves represent either nonpromising dead ends or complete solutions found by
the algorithm.
Backtracking
• Constructs the state-space tree.
• A state space tree for a backtracking algorithm is constructed in the depth first
search manner.
• If the current node is promising, its child is generated, and the processing moves
to this child.
• If the current node turns out to be nonpromising, the algorithm backtracks to the
node’s parent to consider the next possible option; 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.
General Backtracking algorithm
N-Queens Problem:
1 2 3 4
1 queen 1
2 queen 2
3 queen 3
4 queen 4
Example: N-Queens Problem
1 2 3 4
1 queen 1
2 queen 2
3 queen 3
4 queen 4
State-Space Tree of the 4-Queens Problem
Implementation:
• If two queens are at positions (i,j) and (k,l), then they are on the same
diagonal only if,
Main diagonal: Reverse Diagonal:
(1,1)(2,2)(3,3)(4,4) (2,4) (3,3) (4,2)
(2,1) (3,2) (4,3)
Implementation
• If two queens are at positions (i,j) and (k,l), then they are on the same
diagonal only if,
1 2 3 4
Main diagonal:
1 queen 1
(1,1)(2,2)(3,3)(4,4)
2 queen 2 (1,2) (2,3) (3,4)
3 queen 3 Reverse Diagonal:
(3,1) (2,2) (1,3)
4 queen 4
(1,4) (2,3) (3,2) (4,1)
Implementation
• If two queens are at positions (i,j) and (k,l), then they are on the same
diagonal only if,
Algorithm
Algorithm
Complexity
• Single solution to the n-queens problem for any n ≥ 4 can be found in
linear time.
Hamiltonian Circuit Problem
Backtracking
Hamiltonian Circuit Problem
• Let G = ( V, E) be a connected graph with n vertices.
• A Hamiltonian cycle (suggested by Sir William Hamilton) is a round
trip path along n edges of G which visits every vertex once and returns
to its starting position.