0% found this document useful (0 votes)
14 views27 pages

Backtracking

Backtracking is an algorithmic technique that incrementally builds solutions to computational problems by exploring all possible combinations and discarding those that fail to meet constraints. It can be applied to various problem types, including decision, optimization, and enumeration problems, and is often visualized as a state space tree. The method is particularly useful for problems requiring feasible solutions rather than optimal ones, such as solving mazes or the N-Queens problem.

Uploaded by

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

Backtracking

Backtracking is an algorithmic technique that incrementally builds solutions to computational problems by exploring all possible combinations and discarding those that fail to meet constraints. It can be applied to various problem types, including decision, optimization, and enumeration problems, and is often visualized as a state space tree. The method is particularly useful for problems requiring feasible solutions rather than optimal ones, such as solving mazes or the N-Queens problem.

Uploaded by

jodiyev273
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Backtracking

How a blind person move backward when he


finds obstacle?
Backtracking
■ As a blind person backtrack by intelligence, an algorithm
backtracks with intelligence it is backtracking.
■ Backtracking can be defined as a general algorithmic technique that
considers searching every possible combination in order to solve a
computational problem.
■ Backtracking algorithm solves a problem recursively by trying to build
a solution incrementally, one piece at a time, removing those solutions
that fail to satisfy the constraints of the problem.
■ 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
Example: Arrange ‘a’, ‘b’, ’c’ such that ‘b’ not in middle.
acb , cab, bac, bca are the solutions but abc, cba are not as per constrains.
2
Backtracking
Backtracking is a methodical way of trying out various sequences
of decisions to get a success.
dead end
?
dead end
success!

?
start ? ?
dead end
dead end
?
success!
In above diagram there are 5 decision points and we considered
all of them to get all the solutions.
3
Backtracking
■ A backtracking algorithm is a problem-solving algorithm that
uses a brute force approach for finding the desired output.
■ The Brute force approach tries out all the possible solutions and
chooses the desired/best solutions.
■ The term backtracking suggests that if the current solution is not
suitable, then backtrack and try other solutions. Thus, recursion
is used in this approach.
■ Three types of problems can be solved using backtracking:
■ Decision Problem – In this, we search for a feasible solution.
■ Optimization Problem – In this, we search for the best solution.
■ Enumeration Problem – In this, we find all feasible solutions
■ This approach is used to solve problems that have multiple
solutions and we need any one solution or all of the solutions. If
we want an optimal solution, we must go for dynamic
programming.
4
Backtracking and Tree
If we draw the sequence of choices we make, the diagram looks like
a tree. Our backtracking algorithm sweeps out a tree in problem
space, it is known as “state space tree”.
Backtracking can be thought of as searching a tree for a particular
“goal” leaf node.

A tree is composed of
nodes. There are three
kinds of nodes:
The (one) root node
Internal nodes
Leaf nodes

5
State Space Tree
❑ Consider the problem arranging a,b,c such that b is not in middle.
■ State Space Tree for the solution of the problem:

a b c

c b c a b a

b x b
x a c

6
The Backtracking Algorithm
■ Backtracking is really quite simple--we “explore” each
node, as follows:
■ To “explore” node N:
1. If N is a goal node, return “success”
2. If N is a leaf node, return “failure”
3. For each child C of N,
3.1. Explore C
3.1.1. If C was successful, return “success”
4. Return “failure”

7
DFS is Backtracking
❑ Depth-First Search can be used to attempt to visit all nodes of a
graph in a systematic manner.

■ DFS Algorithm:
■ 1. Initialize all nodes to Ready state.
■ 2. Choose first node of graph and push it on the stack, change its state to
Wait state.
■ 3. Repeat step 4 and 5 until stack is empty.
■ 4. Pop a node from stack, process it and change its state to Processed state.
■ 5. Push all the neighbours of just poped node on the stack and change their
state from ready to wait.
■ 6. Exit.

8
Walk-Through Visited Array
F C A
A B
B C
D
H D
E
G E F
G
H

Task: Conduct a depth-first search of the


graph starting with node D
Walk-Through
Visited Array
F C A
A B
B C
D
H D
E
G E F
G
D
H
The order nodes are visited:
Push D, on stack.
Walk-Through
Visited Array
F C A
A B
B C
D
H D √
E
C
G E F
E
G
F
H
The order nodes are visited:
Visit D, Push nodes adjacent to D,
D on stack (Rule: visit adjacent
nodes in alphabetical order)
Walk-Through
Visited Array
F C A
A B
B C √
D
H D √
E
G E F
E
G
F
H
The order nodes are visited:
Visit C, No adjacent node to C.
D, C
Come back take other adjacent
of D.
Walk-Through
Visited Array
F C A
A B
B C √
D
H D √
E √
G E F
G
G
F
H
The order nodes are visited:
Visit E and place its adjacent G
D, C, E on stack
Walk-Through
Visited Array
F C A
A B
B C √
D
H D √
E √
G E F
H
G √
F
H
The order nodes are visited:
Visit G and place its adjacent H
D, C, E, G on stack
Walk-Through
Visited Array
F C A
A B
B C √
D
H D √
E √
A
G E F
B
G √
F
H √
The order nodes are visited:
Visit H and place its adjacent B,
D, C, E, G, H A on stack
Walk-Through
Visited Array
F C A √
A B
B C √
D
H D √
E √
G E F
B
G √
F
H √
The order nodes are visited:
Visit A, its adjacent is already
D, C, E, G, H, A on stack.
Walk-Through
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E F
G √
F
H √
The order nodes are visited:
Visit B, there is no adjacent to
D, C, E, G, H, A, B B, go back
Walk-Through
Visited Array
F C A √
A B √
B C √
D
H D √
E √
G E F √
G √
H √
The order nodes are visited:
Visit F, Stack is empty.
D, C, E, G, H, A, B, F Depth-first traversal is done.
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

19
Solving a maze
Backtracking is used to solve problems in which a feasible solution is
needed rather than an optimal one, such as the solution to a maze.
Backtracking is a modified depth-first
Available Path Red line search of the solution-space tree. In the
case of the maze the start location is the
root of a tree, that branches at each point in
the maze where there is a choice of
direction.

Decision Point 2
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

21
Hamiltonian Cycle/Circuit
■ Some definitions
■ A Hamiltonian path or traceable path is a path that visits each vertex
exactly once.
■ A graph that contains a Hamiltonian path is called a traceable graph.
■ A graph is Hamiltonian-connected if for every pair of vertices there is a
Hamiltonian path between the two vertices.
■ A Hamiltonian cycle, Hamiltonian circuit, vertex tour or graph cycle is
a cycle that visits each vertex exactly once (except for the vertex that is
both the start and end, which is visited twice).
■ A graph that contains a Hamiltonian cycle is called a Hamiltonian
graph.
■ Hamiltonian path problem and the Hamiltonian cycle problem are
problems of determining whether a Hamiltonian path or a Hamiltonian
cycle exists in a given graph (whether directed or undirected).
22
Exploration
■ Let’s pretend that you are a city inspector but this time
you must inspect the fire hydrants that are located at
each of the street intersections.
■ To optimize your route, you must find a path that begins
at the garage, G, visits each intersection exactly once,
and returns to the garage.
Exploration

f
c d h

G
e

a b i
j
One of the solutions is path G, h, f, d, c, a, b, e, j, i, G.
State Space Tree for Hamiltonian Cycle

v v v v 1
1 2 3 4 state space
2 5 tree
v v v v
5 6 7 8
5 7 2 6
gra
ph
6 7 3

3 4 8 :
Exercise: Complete the state space :
tree for Hamiltonian cycles of 3 8 4
above graph, starting at vertex 1.
:
:
You Try
■ Try to find the Hamiltonian circuit in each of the graphs
below.
N Queens Problem
■ The problem of placing N queens on an NxN chessboard in such a way
that no two of them are "attacking" each other, is a classic problem used
to demonstrate the backtracking method.
■ A simple brute-force method would be to try placing the first queen on
the first square, followed by the second queen on the first available
square, scanning the chessboard in a row-column manner.

X[1,1]

Q1

Q2 A more efficient backtracking approach


is, check a place for next queen other
Q3 than rows and columns of placed queens
(that each queen must be in its own
X[4,2] column and row). This reduces the
Q4
search from (N2)! to N!.
27

You might also like