0% found this document useful (0 votes)
7 views10 pages

Backtrack Intro

Backtracking is a problem-solving technique used for computational problems, particularly constraint satisfaction problems, involving recursion and state pruning. Examples include the N-Queens problem, Depth First Search in mazes and graphs, solving Sudoku, graph coloring, and the River Crossing problem. The key takeaway is that backtracking focuses on visiting valid states and pruning invalid ones, making it effective for constraint satisfaction and complete search problems.

Uploaded by

Khoi Truong bao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as KEY, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views10 pages

Backtrack Intro

Backtracking is a problem-solving technique used for computational problems, particularly constraint satisfaction problems, involving recursion and state pruning. Examples include the N-Queens problem, Depth First Search in mazes and graphs, solving Sudoku, graph coloring, and the River Crossing problem. The key takeaway is that backtracking focuses on visiting valid states and pruning invalid ones, making it effective for constraint satisfaction and complete search problems.

Uploaded by

Khoi Truong bao
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as KEY, PDF, TXT or read online on Scribd
You are on page 1/ 10

Backtrackin

g
William Fiset
What is backtracking?

Backtracking is a problem solving technique and


general algorithm for solving computational
problems, especially constraint satisfaction problems.
In practice, backtracking almost always involves
using recursion and state pruning to explore a
solution space.
N-Queens Problem
Problem Description:
Given an NxN grid find out how
many valid configurations exists
where you can place N queens
without any queen being able to
threaten another.

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.

Variations of the river crossing problem:


Fox, goose and bag of beans puzzle
Jealous husbands problem
The bridge and the torch problem

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

You might also like