Lecture 13 - Backtracking
Lecture 13 - Backtracking
Department, GCETTS
•
bounding function.
• Backtracking abandons the formed partial vector as soon as it found this vector
cannot lead to a solution, unlike Brute force approach.
• One solution vs. All solutions.
2
•
Introduction
Si = { 0, 1 }
Si = { All non-negative real numbers }
Department, GCETTS
Illustration of Backtracking design technique – Part 2
n-Queens Problem
Department, GCETTS
8 Can there be any other solution?
1 2 3 4 5 6 7 8
Representing the Problem
Since all the queens has to be in different row, we’ll assume the i-th queen is placed at i-th row.
Then the solution can be represent as 8-tuple as (X1, X2, …, X8), Xi denoting the column i-th
queen being placed.
5
4/26/2023
Analyzing Problem
• What is the Explicit Constraints for 8-Queens Problem?
Department, GCETTS
● Total solution space is now reduced to 8! from 88 solution vectors.
6
Solving 4-queens Problem
Department, GCETTS
How backtracking works – Part 3
The Mechanism
Problem
State Space
State
Solution
Problemstates
states
that
formeets
whichthe
path
implicit
from root
constraints
to the node
of the
defines
problem
a tuple
Solution Answer
States States
Department, GCETTS
Solution Space for 4-Queen Problem
9
4/26/2023
Generating Problem states
• Terminologies
Department, GCETTS
● Depth first way, known as Backtracking.
• Second Method :
● An E-node remains E-node until it becomes dead.
● Breadth first way, known as Branch and Bound.
10
4/26/2023
Generalized Backtracking
• Finding just one answer state, not all.
Department, GCETTS
• With the help of these, we will define a generalized backtracking algorithm
• The initial call should be Backtrack(1)
11
4/26/2023
Generalized Backtracking Algorithm
Algorithm : Backtrack(x, k, n)
Department, GCETTS
then
return Backtrack(x, k+1, n)
else
return x
endif
endif
endfor
End 12
The algorithms – Part 4
Solving n-Queens
Department, GCETTS
return false
endif
endfor
return true
End
14
4/26/2023
Algorithm : N-queens
Algortihm : NQueens(X[], k, n)
Department, GCETTS
if k = n then return X endif
y ← NQueens(X, k+1, n)
if y ≠ NIL then break endif
endif
endfor
return y
End
15
Problem
Graph Coloring
Illustration of Backtracking designing techniques – Part 5
• Example
● Is it 2-colorable?
● Is it 3-colorable?
18
4/26/2023
Graph Theory
• In computer science, graph can be represented in two ways –
● Adjacency List Representation.
● Adjacency Matrix Representation
19
The algorithms – Part 6
Coloring
Solving Graph
• So, the state space tree has degree m and height n+1.
● Each node at level i has m children corresponding to m possible assignment at Xi.
● Nodes at level n+1 are the leaf nodes representing the solution states of the
problem.
21
4/26/2023
Bounding Function: NextValue
Algorithm : NextValue(G, X, k, m)
Input : Adjacency Matrix G. Array X, X[i] = color of i-th vertex. Finding a
color for vertex k in range [0,m]. Highest m colors can be used.
Assumption : X[1…k-1] is populated with valid values. X[k…n] are all 0.
Output : X[k] will be assigned a valid color, if found. 0, if not found.
23
Connecting the Dots…