Backtrack Intro
Backtrack Intro
g
William Fiset
What is backtracking?
Backtracking Ideas:
Incrementally place queens on board
Visit only valid partly completed states
Position queens where not threatened
Depth First Search
Problem Description:
Find a path from the start to the end
of a maze.
Backtracking Ideas:
Plunge depth first into maze
Backtrack when a dead end is encountered
Do not revisit visited paths
Depth First Search
The same concept of backtracking can be used
on a general graph to find a path from one
node to another.
S
Solving Sudoku
Problem Description:
Sudoku puzzles are solved by filling
in the empty squares with numbers.
A valid puzzle must have the distinct
numbers 1-9 in each row, column,
and box.
Backtracking Ideas:
Too many possible boards to visit
Visit only valid sudoku board states
Prune invalid states
Challenge https://projecteuler.net/problem=96
:
Graph Coloring
"Graph coloring is coloring the
vertices of a graph such that no
two adjacent vertices share the
same color"
-Wikipedia
Backtracking Ideas:
Start with uncolored graph
Assign a vertex a color if there is no conflict
Backtrack (undo) when graph is in invalid state
River Crossing Problem
Problem Description:
The river crossing problem is
a transportation problem
where one must carry
objects from one river bank
to the other side under some
constraints on the objects.
Challenge open.kattis.com/problems/safepassage
:
Key Takeaways
Common theme of visiting only valid partially
completed states and pruning illegal states.
Backtracking is useful for constraint
satisfaction or complete search problems.
Backtracking algorithms are primarily
implemented using recursion.
Key takeaways