0% found this document useful (0 votes)
8 views23 pages

Backtracking Algorithms Explained

Uploaded by

shubh.6328.pw
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)
8 views23 pages

Backtracking Algorithms Explained

Uploaded by

shubh.6328.pw
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

Design & Analysis of Algorithms

(KCS 503)

UNIT-3
Backtracking

From:
Asif Khan
Department of CSE
GNIOT, Greater Noida

Reference:
Thomas H. Corman et al.
The MIT Press
Backtracking
Backtracking is one of the most general techniques.
Many problems which deal with searching for a set
of solutions or which ask for an optimal solution
satisfying some constraints can be solved using the
backtracking technique.
A backtracking algorithm is a problem-solving
algorithm that uses a brute force approach for
finding the desired output.
The Brute force approach tries out all the possible
solutions and chooses the desired/best solutions.
Backtracking
The term backtracking suggests that if the current
solution is not suitable, then backtrack and try other
solutions. Thus, recursion is used in this approach.
This approach is used to solve problems that have
multiple solutions. If you want an optimal solution,
you must go for dynamic programming.
In backtracking problem, the algorithm tries to find
a sequential path to the solution which has some
small checkpoints from where the problem can
backtrack if no feasible solution is found for the
problem.
Backtracking
State Space Tree:
A state space tree is a tree representing all the
possible states (solution or nonsolution) of the
problem from the root as an initial state to the leaf as
a terminal state.
Example of Backtracking Approach
Problem: We want to find all the possible ways of
arranging 2 boys and 1 girl on 3 benches.
Constraint: Girl should not be on the middle bench.

Solution: There are a total of 3! = 6 possibilities.


We will try all the possibilities and get the possible
solutions. We recursively try all the possibilities.
All the possibilities are:
Example of Backtracking Approach
The following state space tree shows the possible
solutions.
N-Queens Problem
The N Queen problem is the problem of placing N
chess queens on an N×N chessboard so that no two
queens attack each other.
Two queens will attack each other if they are placed
in same row or same column or same diagonal.
Backtracking Algorithm: The idea is to place
queens one by one in different rows, starting from
the topmost row. When we place a queen in a row,
we check for clashes with already placed queens. In
the current row, if we find a column for which there
is no clash, we mark this row and column as part of
the solution.
N-Queens Problem
If we do not find such a row due to clashes then we
backtrack and return false.
N-Queens Problem
N-Queens Problem (State Space Tree)

C1= 1 2 3 4

C2=2 3 4 1 3 4 1 2 4

C3= 2 4 2 3 1 3 2 4

C4= 3 3 2
√ √
Hamiltonian Cycle
Hamiltonian Path in an undirected graph is a path
that visits each vertex exactly once.
A Hamiltonian cycle (or Hamiltonian circuit) is a
Hamiltonian Path such that there is an edge (in the
graph) from the last vertex to the first vertex of the
Hamiltonian Path.
We can determine Hamiltonian Cycle in a given
graph by using backtracking technique.
Hamiltonian Cycle
Example: The Hamiltonian Circuit for the
following given graph is (0,1,2,4,3,0)

State Space Tree for the given example is as


follows:
Hamiltonian Cycle (State Space Tree)
0

3 1

1 4

2 4 1 2

4 2 2 1

0
Graph Coloring or m Coloring Problem
In this problem we are given an undirected graph
and a number m. We need to determine if the graph
can be colored with at most m colors such that no
two adjacent vertices of the graph are colored with
the same color. Here coloring of a graph means the
assignment of colors to all vertices.

Following is an example of a
graph that can be colored
with 3 different colors.
Graph Coloring or m Coloring Problem
Backtracking Approach: The idea is to assign
colors one by one to different vertices, starting
from the vertex 0. Before assigning a color, check
for safety by considering already assigned colors
to the adjacent vertices i.e check if the adjacent
vertices have the same color or not. If there is any
color assignment that does not violate the
conditions, mark the color assignment as part of
the solution. If no assignment of color is possible
then backtrack and return false.
Graph Coloring or m Coloring Problem
Example: Let we are given the following graph
and m=3. We can assign colors to the vertices of
the graph as given in the next slide.

B C

D E
(State Space Tree)

CA= 1 2 3

CB=1 2 3 A

CC=1 2 3 B C

D E
CD= 1 2 3

CE= 1 2 3
Subset Sum or Sum of Subset Problem
Subset sum problem is to find subset of elements
that are selected from a given set whose sum adds
up to a given number K. We are considering the set
contains non-negative values. It is assumed that
the input set is unique (no duplicates are
presented).
Example: Let the Set is {10, 7, 5, 18, 12, 20, 15}
and the sum value is 35.
All possible subsets of the given set, where sum of
all element for every subsets is 35 are as follows:
{10, 7, 18}, {10, 5, 20}, {5, 18, 12}, {20, 15}
Subset Sum or Sum of Subset Problem
Backtracking Algorithm: Using exhaustive search
we consider all subsets irrespective of whether
they satisfy given constraints or not. Backtracking
can be used to make a systematic consideration of
the elements to be selected.
If we are given a set of elements S=(s1,s2….sn), we
will create an array x=(x1,x2,……xn), where xi will
be 1, if element i will be included in the sum
otherwise it will be zero.
So in state space tree we will consider elements
one by one and check whether it is feasible to
include it in the solution or not.
Subset Sum or Sum of Subset Problem
To solve the problem first we will arrange the
elements in increasing order of their values and
then try to include them in solution one by one.
At any point if the sum is more than the given
value M then we will backtrack as the solution is
not feasible.
Example: Let the Set is {5, 10, 12, 13, 15, 18} and
the sum value M is 30.
State Space tree for the above problem is as
follows:
Subset Sum Problem State Space Tree)
{5, 10, 12, 13, 15, 18}
0
5 0
15 5 0

27 15 17 5 12

27 28 15 30 17 18 5 12
27 30 15 17 20 5
12
30
Subset Sum Problem State Space Tree)
{5, 10, 12, 13, 15, 18}
0
5 0
15 5 0

27 15 17 5 12

27 28 15 30 17 18 5 12
27 30 15 17 20 5
12
30
THANK YOU

You might also like