0% found this document useful (0 votes)
30 views46 pages

UNIT 5 Backtracking 1714885860265

Uploaded by

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

UNIT 5 Backtracking 1714885860265

Uploaded by

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

N Queens Problem

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:

▪ The n-queens problem is a generalization of the 8-Queens (or)


4-Queens problem.
▪ Objective is to place n-Queens on a chessboard of dimension
n x n such that no two queens attack each other.
▪ Queens attack each other if they are in the same row, same
column or same diagonal to each other.
Example: N-Queens Problem

Place n queens on an n-by-n chess board so that no two


of them are in the same row, column, or diagonal.

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.

• In other words, if a Hamiltonian cycle begins at some vertex v1 ϵ G


and the vertices of G are visited in the order v1, v2, ••• , Vn+ 1 then
the edges (v1, …. Vn+1) are in E, are distinct except for v1 and Vn+1
which are equal.
Example
Example

G1: 1-3-4-5-6-7-8-2-1 G2: No Hamiltonian circuit


1-2-8-7-6-5-4-3-1
State space tree
Algorithm
Algorithm
Efficiency:
Sum of Subset- Problem
Backtracking
Sum of Subset- Problem:
Backtracking:
• Find all feasible solutions.
• Once a decision is made, it can be
revoked if it does not lead to feasible
solution but in greedy technique
cannot be revoked.
• To solve any problem using
backtracking, we need to construct a
tree is termed as “State Space Tree”.
• Tree is constructed using DFS traversal.
Sum of Subset- Problem:
• Subset-sum problem: find a subset of a given set w = {w1, . . . , wn} of
n positive integers whose sum is equal to a given positive integer m.

• For example, for w = {1, 2, 5, 6, 8} and m = 9, there are two solutions:


{1, 2, 6} and {1, 8}.
• Some instances of this problem may have no solutions.
Sum of Subset- Problem:
Sum of Subset- Problem:
Sum of Subset- Problem:
Sort the set’s elements
in increasing order
The bounding functions used are:
Algorithm

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy