The strategy of backtracking consists of dividing the
problem to be solved into a number of subproblems, so that a
decision tree is created. For reasons of time and space this tree
usually is not actually created, it functions more as an orientation
guide.
The branches are then systematically processed until the first
solution is found at which point the procedure terminates. Paths with
"dead ends" are marked as not possible.
In the worst case the calculation of the entire tree is necessary,
which leads to exponential cost.
The animation uses the 4-Queens Problem as an example, in
which four Queens have to be placed in such a way on a 4x4-field so
they can not strike each other. The 4-Queens Problem is a
smaller version of the 8-Queens Problem. It is easy to see
that there is no solution for 2 or 3 Queens on a 2x2 or 3x3 field.
Under the field with the Queens the algorithm is described
verbally. The separate steps are highlighted using colours.
Screen Shot(s):
Basic Idea
4 Queens Problem - Possible moves for a queen
Dead end reached
Step back ("backtrack") and continue with next possible setting