This preview shows pages 1–3. Sign up to view the full content.
Appendix R
Recursive Backtracking (beta)
Copyright © 2011 by Stuart Reges and Marty Stepp
•
Q.1 Backtracking Concepts
•
Q.2 Code Example: Eight Queens
•
Q.3 Programming Project: Anagrams
Introduction
In this appendix we discuss a new topic known as
backtracking
.
It is a particular approach to
problem solving that is nicely expressed using recursion.
As a result, it is sometimes referred to
as
recursive backtracking
.
Recursive backtracking involves exploring a possibly very large space of choices and
solutions to solve an overall problem.
Backtracking is very effective when we want to try all
possible choices, possibly in some targeted order, until we find the answer(s) we are seeking.
Q.1 Backtracking Concepts
The idea behind backtracking is that there is some
solution space
of possible answers that
you want to explore.
We try to view the problem as a sequence of
choices
, which allows us to
think of the solution space as a
decision tree
.
At the top of the tree we have the first choice with
all of the possibilities underneath:
choice #1

+++++ .
..




1st possibility
2nd possibility
3rd possibility
4th possibility
Each choice we might make at the top of the tree leaves us in a different part of the solution
space. From there, we consider all possible second choices we might make. For example, if for
choice #1 we chose the 3rd possibility, then we'd have to consider the choices we could make
from there:
choice #1

+++++ .
..




1st possibility
2nd possibility
3rd possibility
4th possibility
choice #2

+++++ .
..
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document



1st possibility
2nd possibility
3rd possibility
4th possibility
It's difficult to provide a good picture of a large decision tree, because there is a fanning out
at the top of the tree into several possibilities and from each of those there is a fanning out of
possibilities, and from each of those there is a fanning out of possibilities and so on.
So the
decision tree is potentially gigantic.
R.2 Coding Example: 8 Queens
A classic example of backtracking is the problem known as "8 queens".
The challenge is to
place eight queens on a chessboard so that no two queens threaten each other.
Queens can move
horizontally, vertically or diagonally, so it is a real challenge to find a way to put 8 different
queens on the board so that no two of them are in the same row, column or diagonal.
The simplest way to think of this in terms of a decision tree is to imagine all the places you
might put a first queen.
There are 64 of them because the chess board is an 8 by 8 board.
So at
the top of the tree, there are 64 different choices you could make for placing the first queen.
Then once you've placed one queen, there are 63 squares left to choose from for the second
This is the end of the preview. Sign up
to
access the rest of the document.
 Winter '09

Click to edit the document details