Chapter 5 Backtracking
Chapter 5 Backtracking
Backtracking
A short list of categories
Algorithm types we will consider include:
Brute force and elementary data structures
Backtracking
Divide and conquer
Dynamic programming algorithms
Greedy algorithms
2
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”
3
Cont’d
A backtracking algorithm tries to construct a solution to
a computational problem incrementally, one small piece
at a time.
Whenever the algorithm needs to decide between
multiple alternatives to the next component of the
solution,
it recursively evaluates every alternative and then
chooses the best one.
4
N Queens
5
Cont’d
We place queens on the board one row at a time, starting
with the top row.
To place the rth queen, we methodically try all n squares
in row r from left to right in a simple for loop.
If a particular square is attacked by an earlier queen, we
ignore that square; otherwise,
we tentatively place a queen on that square and
recursively grope for consistent placements of the queens
in later rows
6
State_Space Tree
7
State space tree
It is convenient to implement this kind of processing by
constructing a tree of choices being made, called the
state-space tree.
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 non promising
8
Types of problems solved by back
tracking
N-queens
Graph coloring
Hamiltonian cycle
Sum of subset
Puzzy problems
And soon
9
Graph Coloring
10
Steps for graph coloring
1. Order nodes arbitrarily.
2. Assign the first node a color.
3. Given a partial assignment of colors (c1; c2; :::; ci-1)
to the first i – 1 nodes, try to find a color for the i-th
node in the graph.
4. If there is no possible color for the i-th node given the
previous choices, backtrack to a previous solution.
11
Cont’d
12
Hamiltonian Cycle
13
Cont’d
Given a graph G = (V, E) we have to find the
Hamiltonian Circuit using Backtracking approach.
14
Cont’d
The Hamiltonian cycle for the the figures are
V(a,b,c)
V(b,a,d,f,e) and soon
15
Solving a puzzle
In this puzzle, all holes but one
are filled with white pegs
You can jump over one peg
with another
Jumped pegs are removed
The object is to remove all
but the last peg
You don’t have enough information to jump correctly
Each choice leads to another set of choices
One or more sequences of choices may (or may not) lead to a
solution
Many kinds of puzzle can be solved with backtracking
16
Backtracking (animation)
dead end
?
dead end
dead end
?
start ? ? dead end
dead end
?
success!
17
Terminology I
18
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
19
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”
20
The End
21