Chapter 10: Algorithm Design Techniques Mark Allen Weiss: Data Structures and Algorithm Analysis in Java Chapter 10: Algorithm Design Techniques Backtracking Lydia Sinapova, Simpson College
Backtracking How to solve a problem by search Generate and Test What is Backtracking Example
The Idea Problem solving: search in a space of possible states. Initial state Target state2 Target state1
Generate and Test Method Take a state Generate next states by applying relevant rules (not all rules are applicable for a given state!) Test to see if the goal state is reached. If yes - stop If no - add the generated states into a stack/queue (do not add if the state has already been generated) and repeat the procedure. To get the solution: Record the paths
Search Methods Breadth-first vs depth-first search Exhaustive search (brute force) vs informative (constraint-based, heuristic) search Backtracking
Backtracking rejecting the currently reached state as a dead-end state, going back to some previous level in the state-space tree/graph proceeding with the exploration of other paths toward the target state Backtracking occurs in depth-first search
Example Replace the letters with distinct digits such that the addition is correct Decide on: State representation Operators Constraints Initial state Target state LOAN + LOAN LOAN ------- DEBT