Truth is not discovered by proofs but by exploration. It is always experimental. — Simone Weil, The New York Notebook, 1942
Objectives • • • • • •
To appreciate how backtracking can be used as a solution strategy. To recognize the problem domains for which backtracking strategies are appropriate. To understand how recursion applies to backtracking problems. To be able to implement recursive solutions to problems involving backtracking. To comprehend the minimax strategy as it applies to two-player games. To appreciate the importance of developing abstract solutions that can be applied to many different problem domains.
Backtracking Algorithms
– 238 –
For many real-world problems, the solution process consists of working your way through a sequence of decision points in which each choice leads you further along some path. If you make the correct set of choices, you end up at the solution. On the other hand, if you reach a dead end or otherwise discover that you have made an incorrect choice somewhere along the way, you have to backtrack to a previous decision point and try a different path. Algorithms that use this approach are called backtracking algorithms. If you think about a backtracking algorithm as the process of repeatedly exploring paths until you encounter the solution, the process appears to have an iterative character. As it happens, however, most problems of this form are easier to solve recursively. The fundamental recursive insight is simply this: a backtracking problem has a solution if and only if at least one of the smaller backtracking problems that results from making each possible initial choice has a solution. The examples in this chapter are designed to illustrate this process and demonstrate the power of recursion in this domain.
7.1 Solving a maze by recursive backtracking
Once upon a time, in the days of Greek mythology, the Mediterranean island of Crete was ruled by a tyrannical king