Backtracking is an implementation of Artificial Intelligence.
Backtracking is a fascinating way of software engineering which can be used
for optimisations, solving puzzles (jigsaw and other) and creating computer
opponents in mind games like chess.
There are written many books about this subject, but in short it looks like this:
The computer walks through 'a tree of possibilities' (with branches and sub-branches).
Such a branch may mean: fitting one piece of a puzzle, or trying
one move in a chess game.
When the program has worked out the top of a branch, it follows its track back along
the same branch he came, and tries another branch.
Another aspect is that a branch can be cancelled at an early stage, if it comes clear
that it won't give successful result. This way you might optimize the process strongly.
Case study: the 8 queens problem
When can you use backtracking?
The applications can be catechorized:
- Common applications
- searching through a directory with subdirectories (recursive)
- Some sorting algorithms use recursion
Travelling salesman. Suppose you have a distance table of a set of destinations.
Calculate the shortest or fastest route that passes each junction.
- Creating a country-wide schedule for trains, train staffing, railroad sections and platforms.
- Optimize a school schedule for lessons, students, teachers and lesson rooms.
- Optimizing Demand and Supply e.g. with
in Operational Analysis.
- Incomplete Block Design e.g. for Marketing Research, product testing:
Suppose you need to have 8 variants been tested, but for practical reasons you can only test 4 variants
With 4 out of 8 you can create 70 combinations (8 over 4 = (8*7*6*5)/(4*3*2*1) = 70) so you would need 70 test groups.
luckily in this case it's possible to make a set of 14 groups in a
Balanced Incomplete Block Design.
- Tournament/competition scheme (canditates and contra-candidates from the same list)
- (Speed)Dating - match a list of candidates to a list of contra-candidates
(supposed you have rounds where all candidates switch simultaneously to the next)
(using a minimax algorithm
and Alpha-beta pruning)
Computers are stronger than humans in most mind games.
You'll only have chance chance against the computer when you find variants that
are cut off by the Alpha-beta algorithm,
in chess terms: find a surprising
Puzzles (and most programming contests)
There are different approaches (each with its own advantages):
- the 8 queens problem (a case-study)
- Mathematical jigsaw puzzles I have some solvers in Windows (Free download)
- Moving block puzzles (Rush Hour, the piano-mover, bishop-problem, rubiks cube)
the Peg Solitaire puzzle
and: in as few moves as possible.
- Creating a maze or labyrinth
as well as solving it!
- Solving Numbers
from TV-programme 'Letters and Numbers'
- Logiquiz puzzles (dutch)
With a number of clues you are requested to find the correct matrix of attributes.
Extra challenge for a generic solution: hoe can you make the computer understands the clue-sentences?
This is realizable when the clues follows a restricted set of transelatable structures.
- Sudoku (for creating as well as solving), introduction...
- Create a magic square with a set of domino stones
- See also the book 'Spelen met Puzzels' (dutch: Playing with Puzzles) 1978 by Pieter van Delft and Jack Botermans,
for a almost unlimited resource of mind teasers.
- The computer games The 7th Guest
and The 11th hour
contain a number of nice puzzles that can be solved (or played against) the computer.
- Recursion - You write a subroutine that calls itself,
in a way it repeats the exercise with a subset of the exercise.
Othen, your program may look amazingly simple, and still it does the full job!
see Towers of Hanoi
- One big 'while'-loop, is more flexible to control the flow and data..
- Using a recursive language like Prolog, the base program has
already been done for you.
These languages can be a bit lavish with memory or CPU,
so for the larger tasks this might not be the best way.
The N queens problem, magic squares and more
- Genetic algorithm