Professional Documents
Culture Documents
Department of cs
Jan, 2024
04/22/2024 1
Chapter 5
Backtracking
?
dead end
dead end
?
start ? ?
dead end
dead end
success!
4
Backtracking
• Backtracking is a general algorithm for finding all (or some)
solutions to some computational problems, notably constraint
satisfaction problems (CSP), that incrementally builds
candidates to the solutions, and abandons each partial
candidate ("backtracks") as soon as it determines that the
candidate cannot possibly be completed to a valid solution.
• The general method of backtracking is a systematic approach
used to solve problems by incrementally building a solution
and undoing certain choices when they are found to be
incorrect or infeasible.
• It is often used for solving combinatorial problems, such as
constraint satisfaction problems, crosswords, verbal
arithmetic, Map Coloring puzzles, and optimization
problems.
5
Backtracking
• Suppose you have to make a series of
decisions, among various choices, where
– You don’t have enough information to know
what to choose
– Each decision leads to a new set of choices
– Some sequence of choices (possibly more than
one) may be a solution to your problem
• Backtracking is a methodical way of trying
out various sequences of decisions, until
you find one that “works”.
6
Maze Problem
• The maze problem is a
classic example of a problem
that can be solved using the
backtracking algorithm. It
involves finding a path from
a starting point to a goal
point in a maze, where the
maze is represented as a grid
with walls and open paths.
• The backtracking algorithm
can be applied to explore all
possible paths in the maze
until a valid path from the
starting point to the goal
point is found.
Outline How Backtracking Algorithm Can
Solve the Maze Problem
1. Define the maze: Represent the maze as a grid, where each cell can
be either a wall or an open path. Determine the dimensions of the
maze and mark the starting point and the goal point.
2. Implement the backtracking algorithm: Use a recursive function
to explore and search for a valid path from the starting point to the
goal point. The general steps within the algorithm are as follows:
a) Check the base cases: Determine if the current position is outside
the maze boundaries or if it is a wall. If either of these conditions is
true, backtrack to the previous step.
b) Check for the goal: Determine if the current position is the goal
point. If it is, the algorithm terminates successfully, and the path is
considered the solution.
c) Mark the current position: Update the status of the current position
to indicate that it has been visited or is part of the current path.
Outline How Backtracking Algorithm Can
Solve the Maze Problem
3. Iterate and explore: Repeat steps 2a to 2d until a valid path from the
starting point to the goal point is found or all possible paths have been
explored.
4. Output the solution: Once a valid path is found, it can be stored or
displayed as the solution to the maze problem.
Solving a maze
• Given a maze, find a path from start to finish
• At each intersection, you have to decide
between three or fewer choices:
– Go straight
– Go left
– Go right
• You don’t have enough information to choose correctly
• Each choice leads to another set of choices
• One or more sequences of choices may (or may not) lead to a
solution
• Many types of maze problem can be solved with backtracking
10
Backtracking
• Backtracking is a technique used to solve problems with
a large search space, by systematically trying and
eliminating possibilities.
• A standard example of backtracking would be going
through a maze.
– At some point in a maze, you might have two options of which
direction to go:
i on
nc t
Ju
Portion A
Portion B
Backtracking
One strategy would be on
t i
to try going through unc
J
Portion A of the maze. Portion B
If you get stuck before
you find your way out,
Portion A
then you "backtrack" to
the junction.
D
Graph Coloring
B F
C E
D
Graph Coloring
B F
C E
D
Graph Coloring
B F
C E
D
Graph Coloring
B F
C E
D
Graph Coloring
B F
C E
D
Graph Coloring
B F
C E
D
Graph Coloring
B F
C E
D
Graph Coloring
B F
C E
D
Graph Coloring
B F
C E
D
Graph Coloring
B F
C E
D
A
Graph Coloring
B F
C E
D
Coloring a map
• You wish to color a map with
not more than four colors
– red, yellow, green, blue
• Adjacent countries must be in
different colors
• You don’t have enough information to choose colors
• Each choice leads to another set of choices
• One or more sequences of choices may (or may not) lead to a
solution
• Many coloring problems can be solved with backtracking
29
Edge colouring
• A -edge-coloring of a graph is a labeling .
• Edge coloring partitions into sets (some possibly
empty) .
• An edge coloring is proper if adjacent edges have
different colors. All coloring henceforth are
assumed proper.
• is -edge-colorable if it has -edge-coloring.
• The edge chromatic number is the smallest such
that has -edge-coloring. is called -edge-
chromatic.
• E.g.
Depth First Search - Method
Initial state
D
B C
E F G H I J
K L M O P Q R
Goal state
S T U Visited:
Depth-First Search
A
D
B C
E F G H I J
K L M O P Q R
Goal state
S T U
Visited: A
Depth-First Search
A
D
B C
E F G H I J
K L M O P Q R
Goal state
Visited: A, B
S T U
Depth-First Search
A
D
B C
E F G H I J
K L M O P Q R
Goal state
S T U Visited: A, B, E
Depth-First Search
A
D
B C
E F G H I J
K L M O P Q R
Goal state
S T U Visited: A, B, E, K
Depth-First Search
A
D
B C
E F G H I J
K L M O P Q R
D
B C
E F G H I J
K L M O P Q R
D
B C
E F G H I J
K L M O P Q R
E F G H I J
K L M O P Q R
Visited: A, B, E, K, S, L, T
S T U
N-Queens problem
• General techniques
In a working solution, exactly 1 queen must appear in
each row, in each column and diagonals.
Simply Finding a configuration of n queens not
attacking each other
N-Queens problem
Algorithm:
- Start with one queen at the first column first row
- Continue with second queen from proper place i.e. it
can not exist in the same row, column and diagonal.
- Go up until find a permissible situation
- Continue with next queen
- If you reach an impasse, backtrack to the previous
column
- E.g. 4 Queens problem, 8-Queens problem
Eight-Queens Problem
– Place eight queens on the chessboard so that no queen
can attack any other queen
• A recursive algorithm that places a queen in a
column
– Base case
• If there are no more columns to consider
– You are finished
– Recursive step
• If you successfully place a queen in the current
column
– Consider the next column
• If you cannot place a queen in the current column
– You need to backtrack
Example
– One solution of eight queens problem
Hamiltonian cycle:
• Given an undirected connected graph in that have to find a
path that starts at given vertex, visits each vertex in the graph
exactly once, and ends at the starting vertex.
4 3
04/22/2024 52