Backtracking
Backtracking
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
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
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.
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