CmSc 250 Intro to Algorithms
Backtracking algorithms
- State space search problems
Application of graphs in problem solving:
- to model the problem
- to model the solution process
Problem solving: search in a space of possible states.
Basic concepts:
- Initial state
- Goal/Target state
- Intermediate states
- Path from the initial to the target state
- Operators/rules to get from one state to another
We search for a path / sequence of operators to go from the
initial state
to the goal state.
Basic idea:
- Decide
on a representation of the states and the available actions in the domain
- Find a sequence of actions that will transform the initial state into the target state.

Examples:
Puzzles: Find a sequence of actions to solve the puzzle
Chess: Find the sequence of moves that will result in winning the game.
Travel: Find a route from the starting place to the destination.
Generate and test method
- Store initial state into a stack / queue
- While there are states in the stack/queue and target state is not reached
- Read a state from the stack / queue.
- Generate next states by applying relevant operators
(not all
operators 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 the stack/queue
(do not
add if the state has already been generated)
- If the stack /queue is empty and the target state is not
reached - the problem
is not solvable with the available operators.
To get the solution: Record the sequence
of applied operators.
Using a stack corresponds to depth-first search in the state space.
Using a queue corresponds to breadth-first search in the state space.
Search methods
Breadth-first vs depth-first.
Exhaustive search (brute force) vs informative (constraint-based,
heurististic) search.
Backtracking
Basic idea:
If at some state no operator is applicable or all applicable
operators
result in already generated states:
- reject the currently reached state as a dead-end state,
- go back to some previous level in the state-space tree/graph
- proceed with the exploration of other paths toward the target state
Backtracking occurs in depth-first search.
Example
LOAN
LOAN
+ LOAN
LOAN
------
DEBT
Replace the letters with distinct digits such that the addition is correct
Decide on:
- State representation
- Operators
- Constraints
- Initial state
- Target state
Back to Algorithm Design Paradigms
Created by Lydia Sinapova