Backtracking
-Prof. Jaydeep Barad
Backtracking
Suppose you have to make a series of decisions,
among various choices, where
You don’t have enough information to know what to
choose
Each decision leads to a new set of choices
Some sequence of choices (possibly more than one)
may be a solution to your problem
Backtracking is a methodical way of trying out
various sequences of decisions, until you find one
that “works”
2
Solving a maze
Given a maze, find a path from start to finish
At each intersection, you have to decide between
three or fewer choices:
Go straight
Go left
Go right
You don’t have enough information to choose correctly
Each choice leads to another set of choices
One or more sequences of choices may (or may not) lead to a
solution
Many types of maze problem can be solved with backtracking
3
Coloring a map
You wish to color a map with
not more than four colors
red, yellow, green, blue
Adjacent countries must be in
different colors
You don’t have enough information to choose colors
Each choice leads to another set of choices
One or more sequences of choices may (or may not) lead to a
solution
Many coloring problems can be solved with backtracking
4
Backtracking (animation)
dead end
?
dead end
dead end
?
start ? ? dead end
dead end
?
success!
5
Terminology I
A tree is composed of nodes
There are three kinds of
nodes:
The (one) root node State Space Tree
Internal nodes
Leaf nodes Backtracking can be thought of
as searching a tree for a
particular “goal” leaf node
6
Terminology II
Each non-leaf node in a tree is a parent of one or more
other nodes (its children)
Each node in the tree, other than the root, has exactly
one parent
parent
Usually, however,
we draw our trees
downward, with
parent the root at the top
children children
7
Real and virtual trees
There is a type of data structure called a tree
But we are not using it here
If we diagram the sequence of choices we make, the
diagram looks like a tree
In fact, we did just this a couple of slides ago
Our backtracking algorithm “sweeps out a tree” in “problem
space”
8
The backtracking algorithm
Backtracking is really quite simple--we “explore” each
node, as follows:
To “explore” node N:
1. If N is a goal node, return “success”
2. If N is a leaf node, return “failure”
3. For each child C of N,
3.1. Explore C
3.1.1. If C was successful, return “success”
4. Return “failure”
9
N-Queen Problem
N - Queens problem is to place n - queens in such a
manner on an n x n chessboard that no queens attack
each other by being in the same row, column or diagonal.
It can be seen that for n =1, the problem has a trivial
solution, and no solution exists for n =2 and n =3. So first
we will consider the 4 queens problem and then generate
it to n - queens problem.
Given a 4 x 4 chessboard and number the rows and
column of the chessboard 1 through 4.
10
N-Queen Problem
It can be seen that for n =1, the problem has a trivial
solution, and no solution exists for n =2 and n =3. So first
we will consider the 4 queens problem and then generate
it to n - queens problem.
Given a 4 x 4 chessboard and number the rows and
column of the chessboard 1 through 4.
11
12
13
14
15
16
17
State Space Tree of 4 – Queens
Problem
18
State Space Tree of 4 – Queens
Problem
19
State Space Tree of 4 – Queens
Problem
20
N-Queen Problem …
21
N-Queen Problem …
Algorithm
isValid(board, row, col)
Input: The chess board, row and the column of the board.
Output − True when placing a queen in row and place position is a valid or not.
22
N-Queen Problem …
Algorithm
solveNQueen(board, col)
Input − The chess board, the col where the queen is trying to be placed.
Output − The position matrix where queens are placed.
23
Knapsack Problem using Backtracking
No. of Items N=4 and Total Capacity W= 8
i1 i2 i3 i4
Wi 2 3 4 5
Vi 3 5 6 10
24
Knapsack Problem using Backtracking
No. of Items N=4 and Total Capacity W= 8
i1 i2 i3 i4
Wi 2 3 4 5
Vi 3 5 6 10
25
Knapsack Problem using
Backtracking
26
Branch & Bound
Branch & Bound is a technique for exploring implicit
directed graph, usually acyclic or even a tree.
Used to get optimal solution to the problem.
At each node we calculate the Bound on the possible value
of any solution.( To prune some branches or close the path)
Bound can be used to select most promising node which
can further explore to get optimal solution
27
Backtracking vs Branch & Bound
Backtracking
• It is used to find all possible solutions available to a problem.
• It traverses the state space tree by DFS(Depth First Search) manner.
• It realizes that it has made a bad choice & undoes the last choice by
backing up.
• It searches the state space tree until it has found a solution.
• It involves feasibility function.
Branch-and-Bound
• It is used to solve optimization problem.
• It may traverse the tree in any manner, DFS or BFS.
• It realizes that it already has a better optimal solution that the pre-solution
leads to so it abandons that pre-solution.
• It completely searches the state space tree to get optimal solution.
• It involves a bounding function.
28