<s>
Backtracking	B-Algorithm
is	O
a	O
class	O
of	O
algorithms	O
for	O
finding	O
solutions	O
to	O
some	O
computational	O
problems	O
,	O
notably	O
constraint	B-Application
satisfaction	I-Application
problems	I-Application
,	O
that	O
incrementally	O
builds	O
candidates	O
to	O
the	O
solutions	O
,	O
and	O
abandons	O
a	O
candidate	O
(	O
"	O
backtracks	O
"	O
)	O
as	O
soon	O
as	O
it	O
determines	O
that	O
the	O
candidate	O
cannot	O
possibly	O
be	O
completed	O
to	O
a	O
valid	O
solution	O
.	O
</s>
<s>
The	O
classic	O
textbook	O
example	O
of	O
the	O
use	O
of	O
backtracking	B-Algorithm
is	O
the	O
eight	O
queens	O
puzzle	O
,	O
that	O
asks	O
for	O
all	O
arrangements	O
of	O
eight	O
chess	B-Application
queens	O
on	O
a	O
standard	O
chessboard	O
so	O
that	O
no	O
queen	O
attacks	O
any	O
other	O
.	O
</s>
<s>
In	O
the	O
common	O
backtracking	B-Algorithm
approach	O
,	O
the	O
partial	O
candidates	O
are	O
arrangements	O
of	O
k	O
queens	O
in	O
the	O
first	O
k	O
rows	O
of	O
the	O
board	O
,	O
all	O
in	O
different	O
rows	O
and	O
columns	O
.	O
</s>
<s>
Backtracking	B-Algorithm
can	O
be	O
applied	O
only	O
for	O
problems	O
which	O
admit	O
the	O
concept	O
of	O
a	O
"	O
partial	O
candidate	O
solution	O
"	O
and	O
a	O
relatively	O
quick	O
test	O
of	O
whether	O
it	O
can	O
possibly	O
be	O
completed	O
to	O
a	O
valid	O
solution	O
.	O
</s>
<s>
When	O
it	O
is	O
applicable	O
,	O
however	O
,	O
backtracking	B-Algorithm
is	O
often	O
much	O
faster	O
than	O
brute-force	B-Algorithm
enumeration	I-Algorithm
of	O
all	O
complete	O
candidates	O
,	O
since	O
it	O
can	O
eliminate	O
many	O
candidates	O
with	O
a	O
single	O
test	O
.	O
</s>
<s>
Backtracking	B-Algorithm
is	O
an	O
important	O
tool	O
for	O
solving	O
constraint	B-Application
satisfaction	I-Application
problems	I-Application
,	O
such	O
as	O
crosswords	B-Application
,	O
verbal	O
arithmetic	O
,	O
Sudoku	O
,	O
and	O
many	O
other	O
puzzles	O
.	O
</s>
<s>
It	O
is	O
often	O
the	O
most	O
convenient	O
technique	O
for	O
parsing	B-Language
,	O
for	O
the	O
knapsack	B-Algorithm
problem	I-Algorithm
and	O
other	O
combinatorial	O
optimization	O
problems	O
.	O
</s>
<s>
It	O
is	O
also	O
the	O
basis	O
of	O
the	O
so-called	O
logic	B-Language
programming	I-Language
languages	I-Language
such	O
as	O
Icon	B-Language
,	O
Planner	B-Application
and	O
Prolog	B-Language
.	O
</s>
<s>
Backtracking	B-Algorithm
depends	O
on	O
user-given	O
"	O
black	O
box	O
procedures	O
"	O
that	O
define	O
the	O
problem	O
to	O
be	O
solved	O
,	O
the	O
nature	O
of	O
the	O
partial	O
candidates	O
,	O
and	O
how	O
they	O
are	O
extended	O
into	O
complete	O
candidates	O
.	O
</s>
<s>
It	O
is	O
therefore	O
a	O
metaheuristic	B-Algorithm
rather	O
than	O
a	O
specific	O
algorithm	O
–	O
although	O
,	O
unlike	O
many	O
other	O
meta-heuristics	O
,	O
it	O
is	O
guaranteed	O
to	O
find	O
all	O
solutions	O
to	O
a	O
finite	O
problem	O
in	O
a	O
bounded	O
amount	O
of	O
time	O
.	O
</s>
<s>
The	O
pioneer	O
string-processing	O
language	O
SNOBOL	B-Application
(	O
1962	O
)	O
may	O
have	O
been	O
the	O
first	O
to	O
provide	O
a	O
built-in	O
general	O
backtracking	B-Algorithm
facility	O
.	O
</s>
<s>
The	O
backtracking	B-Algorithm
algorithm	I-Algorithm
enumerates	O
a	O
set	O
of	O
partial	O
candidates	O
that	O
,	O
in	O
principle	O
,	O
could	O
be	O
completed	O
in	O
various	O
ways	O
to	O
give	O
all	O
the	O
possible	O
solutions	O
to	O
the	O
given	O
problem	O
.	O
</s>
<s>
Conceptually	O
,	O
the	O
partial	O
candidates	O
are	O
represented	O
as	O
the	O
nodes	O
of	O
a	O
tree	B-Data_Structure
structure	I-Data_Structure
,	O
the	O
potential	O
search	O
tree	O
.	O
</s>
<s>
The	O
backtracking	B-Algorithm
algorithm	I-Algorithm
traverses	O
this	O
search	O
tree	O
recursively	O
,	O
from	O
the	O
root	O
down	O
,	O
in	O
depth-first	B-Algorithm
order	I-Algorithm
.	O
</s>
<s>
In	O
order	O
to	O
apply	O
backtracking	B-Algorithm
to	O
a	O
specific	O
class	O
of	O
problems	O
,	O
one	O
must	O
provide	O
the	O
data	O
P	O
for	O
the	O
particular	O
instance	O
of	O
the	O
problem	O
that	O
is	O
to	O
be	O
solved	O
,	O
and	O
six	O
procedural	O
parameters	O
,	O
root	O
,	O
reject	O
,	O
accept	O
,	O
first	O
,	O
next	O
,	O
and	O
output	O
.	O
</s>
<s>
The	O
backtracking	B-Algorithm
algorithm	I-Algorithm
reduces	O
the	O
problem	O
to	O
the	O
call	O
backtrack(root(P )	O
)	O
,	O
where	O
backtrack	O
is	O
the	O
following	O
recursive	O
procedure	O
:	O
</s>
<s>
On	O
the	O
other	O
hand	O
,	O
the	O
efficiency	O
of	O
the	O
backtracking	B-Algorithm
algorithm	I-Algorithm
depends	O
on	O
reject	O
returning	O
true	O
for	O
candidates	O
that	O
are	O
as	O
close	O
to	O
the	O
root	O
as	O
possible	O
.	O
</s>
<s>
If	O
reject	O
always	O
returns	O
false	O
,	O
the	O
algorithm	O
will	O
still	O
find	O
all	O
solutions	O
,	O
but	O
it	O
will	O
be	O
equivalent	O
to	O
a	O
brute-force	B-Algorithm
search	I-Algorithm
.	O
</s>
<s>
The	O
first	O
and	O
next	O
procedures	O
are	O
used	O
by	O
the	O
backtracking	B-Algorithm
algorithm	I-Algorithm
to	O
enumerate	O
the	O
children	O
of	O
a	O
node	O
c	O
of	O
the	O
tree	O
,	O
that	O
is	O
,	O
the	O
candidates	O
that	O
differ	O
from	O
c	O
by	O
a	O
single	O
extension	O
step	O
.	O
</s>
<s>
The	O
pseudo-code	O
above	O
will	O
call	O
output	O
for	O
all	O
candidates	O
that	O
are	O
a	O
solution	O
to	O
the	O
given	O
instance	O
P	O
.	O
The	O
algorithm	O
can	O
be	O
modified	O
to	O
stop	O
after	O
finding	O
the	O
first	O
solution	O
,	O
or	O
a	O
specified	O
number	O
of	O
solutions	O
;	O
or	O
after	O
testing	O
a	O
specified	O
number	O
of	O
partial	O
candidates	O
,	O
or	O
after	O
spending	O
a	O
given	O
amount	O
of	O
CPU	B-General_Concept
time	O
.	O
</s>
<s>
Examples	O
where	O
backtracking	B-Algorithm
can	O
be	O
used	O
to	O
solve	O
puzzles	O
or	O
problems	O
include	O
:	O
</s>
<s>
Puzzles	O
such	O
as	O
eight	O
queens	O
puzzle	O
,	O
crosswords	B-Application
,	O
verbal	O
arithmetic	O
,	O
Sudoku	O
,	O
and	O
Peg	O
Solitaire	O
.	O
</s>
<s>
Combinatorial	O
optimization	O
problems	O
such	O
as	O
parsing	B-Language
and	O
the	O
knapsack	B-Algorithm
problem	I-Algorithm
.	O
</s>
<s>
Logic	B-Language
programming	I-Language
languages	I-Language
such	O
as	O
Icon	B-Language
,	O
Planner	B-Application
and	O
Prolog	B-Language
,	O
which	O
use	O
backtracking	B-Algorithm
internally	O
to	O
generate	O
answers	O
.	O
</s>
<s>
The	O
following	O
is	O
an	O
example	O
where	O
backtracking	B-Algorithm
is	O
used	O
for	O
the	O
constraint	B-Application
satisfaction	I-Application
problem	I-Application
:	O
</s>
<s>
The	O
general	O
constraint	B-Application
satisfaction	I-Application
problem	I-Application
consists	O
in	O
finding	O
a	O
list	O
of	O
integers	O
,	O
each	O
in	O
some	O
range	O
}	O
,	O
that	O
satisfies	O
some	O
arbitrary	O
constraint	O
(	O
boolean	O
function	O
)	O
F	O
.	O
</s>
<s>
For	O
this	O
class	O
of	O
problems	O
,	O
the	O
instance	O
data	O
P	O
would	O
be	O
the	O
integers	O
m	O
and	O
n	O
,	O
and	O
the	O
predicate	O
F	O
.	O
In	O
a	O
typical	O
backtracking	B-Algorithm
solution	O
to	O
this	O
problem	O
,	O
one	O
could	O
define	O
a	O
partial	O
candidate	O
as	O
a	O
list	O
of	O
integers	O
,	O
for	O
any	O
k	O
between	O
0	O
and	O
n	O
,	O
that	O
are	O
to	O
be	O
assigned	O
to	O
the	O
first	O
k	O
variables	O
.	O
</s>
<s>
The	O
call	O
reject(P, c )	O
should	O
return	O
true	O
if	O
the	O
constraint	O
F	O
cannot	O
be	O
satisfied	O
by	O
any	O
list	O
of	O
n	O
integers	O
that	O
begins	O
with	O
the	O
k	O
elements	O
of	O
c	O
.	O
For	O
backtracking	B-Algorithm
to	O
be	O
effective	O
,	O
there	O
must	O
be	O
a	O
way	O
to	O
detect	O
this	O
situation	O
,	O
at	O
least	O
for	O
some	O
candidates	O
c	O
,	O
without	O
enumerating	O
all	O
those	O
mn	O
−	O
k	O
n-tuples	O
.	O
</s>
<s>
Further	O
improvements	O
can	O
be	O
obtained	O
by	O
the	O
technique	O
of	O
constraint	B-Application
propagation	I-Application
.	O
</s>
<s>
In	O
addition	O
to	O
retaining	O
minimal	O
recovery	O
values	O
used	O
in	O
backing	O
up	O
,	O
backtracking	B-Algorithm
implementations	O
commonly	O
keep	O
a	O
variable	O
trail	O
,	O
to	O
record	O
value	O
change	O
history	O
.	O
</s>
<s>
An	O
efficient	O
implementation	O
will	O
avoid	O
creating	O
a	O
variable	O
trail	O
entry	O
between	O
two	O
successive	O
changes	O
when	O
there	O
is	O
no	O
choice	O
point	O
,	O
as	O
the	O
backtracking	B-Algorithm
will	O
erase	O
all	O
of	O
the	O
changes	O
as	O
a	O
single	O
operation	O
.	O
</s>
