You are on page 1of 52

Debark University

Department of cs

Design and Analysis of Algorithms


(cosc3094)
Target :-3rd Year computer science Students

By: Shimels T.(MSc in Software Engineering )

Jan, 2024
04/22/2024 1
Chapter 5

Backtracking

A systematic approach used to solve constraint


satisfaction problems by incrementally building
a solution and undoing certain choices when
they are found to be incorrect or infeasible.
Outline
• The general Method of Backtracking
• graph coloring
• Depth first search
• 8 queens problem,
• Hamiltonian cycles
• Travelling sales person problems
Backtracking (animation)
dead end

?
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

c). Explore adjacent cells: Recursively call the backtracking function


for all valid neighboring cells (up, down, left, and right) that have not been
visited yet. This step involves moving to the next position and repeating
steps a to d.
d). Backtrack: If none of the neighboring cells lead to a valid path,
backtrack to the previous step. This involves undoing the current position's
marking and returning to the previous position to explore alternative paths.

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.

At this point in time you


know that Portion A will
NOT lead you out of the
maze,
 so you then start searching
Backtracking
• Clearly, at a single junction
you could have even more
than 2 choices.

• The backtracking strategy


says to try each choice, one
after the other, n
ct i o
– if you ever get stuck,
Ju n
"backtrack" to the junction C
and try the next choice. B
A

• If you try all choices and


never found a way out, then
there is no solution to the
Graph colouring
• Graph Coloring Problem: Givena graph, color all the
vertices so that two adjacent vertices get different
colors(no two adjacent vertices are the same color).
• Objective: use minimum number of colors.
• E.g.

The chromatic number is the smallest such that


has proper -coloring. is called -chromatic.
Vertex colouring
• A -coloring of a graph is a labeling .
• A coloring is proper if no two vertices and
connected with an edge have same color, i.e. .
• is -colorable if it has proper -coloring.
• This problem is also called as m-coloring problem.
If the degree of given graph is d then we can color it
with d+1 colors.
Graph Coloring
• The same issues apply as in 8-Queens
– We don’t want to simply pick all subsets
• Way too many
– We want to backtrack as soon as we find
something that won’t work
• This implies that we need a sequence of vertices
to color
• As we color the next vertex we need to make
sure it doesn’t conflict with any of its previously
colored neighbors
Graph Coloring
A
• As an example:
– The vertices are enumerated B F
in order A-F
– The colors are given in
C E
order: R, G, B

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

• Expand Root Node First


• Explore one branch of the tree before
exploring another branch
• If a leaf node do not represent a goal
state, search backtracks up to the next
highest node that has an unexplored
path
Depth-First Search
A

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

Goal state Visited: A, B, E, K, S


S T U
Backtracking
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, E, K, S, L


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 Visited: A, B, E, K, S, L, T


S T U
Backtracking
Depth-First Search
A

Goal state achieved


D
B C

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.

• Then the Hamiltonian cycle is a-b-f-e-c-d-a. This problem


can be solved by using backtracking approach .The state
space tree is generated in order to find all the Hamiltonian
cycles in the graph. Only distinct cycles are output of this
algorithm.
• Are Hamiltonian Circuit/Cycle and Hamiltonian path similar?
The Traveling Salesman/Salesperson
Problem
The goal of the Traveling Salesman Problem
(TSP) is to find the most economical way to
tour of a select number of “cities” with the
following restrictions:

You must visit each city once and only once

You must return to the original starting point
Travelling Salesperson Problem
• A traveling salesman wishes to go to a certain number of destinations
in order to sell objects.
• He wants to travel to each destination exactly once and return home
taking the shortest total route.
• Let us consider a graph G = (V, E), where V is a set of cities and E is
a set of weighted edges.
• An edge e(u, v) represents that vertices u and v are connected.
• Distance between vertex u and v is d(u, v), which should be non-
negative.
For a subset of cities S Є {1, 2, 3, ... , n} that includes 1, and j Є S,
let C(S, j) be the length of the shortest path visiting each node
in S exactly once, starting at 1 and ending at j.
CONT…
• When |S| > 1, we define C(S, 1) = ∝ since the path
cannot start and end at 1.
• Now, let express C(S, j) in terms of smaller sub-
problems. We need to start at 1 and end at j. We
should select the next city in such a way that
 f( i , S) =min( i , j)+f( j , {s-j})
where j∈S, i initial vertex and i≠j
Algorithm
C ({1}, 1) = 0
for s = 2 to n do
for all subsets S Є {1, 2, 3, … , n} of size s and
containing 1
C (S, 1) = ∞
for all j Є S and j ≠ 1
f( i , S) =min( i , j)+f( j , {s-j}) where j ∈S, i initial
vertex and i≠j
Return min j C ({1, 2, 3, …, n}, j) + d(j, i)
Example
1 2

4 3

The minimum cost path is 35.


And the minimum path in the graph is 1—2-4-3-1.
Thank
Thank You
You ...
... Question?
Question?

04/22/2024 52

You might also like