0% found this document useful (0 votes)
16 views21 pages

Chapter 5 Backtracking

lecture note

Uploaded by

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

Chapter 5 Backtracking

lecture note

Uploaded by

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

Chapter 5

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

 The prototypical backtracking problem is the classical


n Queens Problem
 The problem is to place n queens on an n ⇥ n
chessboard,
 so that no two queens are attacking each other

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

 Using backtracking to find valid colorings


 We will use the following strategy to find all valid
colorings of a graph G = (V; E):
 Input: A (undirected) graph G = (V; E) and an integer
k.
 Output: An assignment of one of k colors to each node,
such that adjacent nodes get different colors.

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

 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.
 Determine whether a given graph contains Hamiltonian
Cycle or not.
 If it contains, then prints the path. Following are the
input and output of the required function.

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

A tree is composed of nodes

There are three kinds of


nodes:
The (one) root node
Backtracking can be thought of
Internal nodes
as searching a tree for a
Leaf nodes particular “goal” leaf node

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

You might also like