Professional Documents
Culture Documents
2021 P3e 08 BT 1
2021 P3e 08 BT 1
2021 P3e 08 BT 1
Programming III
Ricardo Wehbe
UADE
November 4, 2021
Programme
1 Introduction to Backtracking
4 Suggested Exercises
1 Introduction to Backtracking
4 Suggested Exercises
Introduction
Introduction
There are some problems for which an efficient solution is not known. In
these cases, the only possibility is a direct exploration of all possibilities
(brute force.)
Introduction
There are some problems for which an efficient solution is not known. In
these cases, the only possibility is a direct exploration of all possibilities
(brute force.)
The backtracking technique is a method of searching for Comprehensive
solutions on acyclic graphs. This method can be optimised by “pruning”
alternatives not leading to a solution.
Introduction
There are some problems for which an efficient solution is not known. In
these cases, the only possibility is a direct exploration of all possibilities
(brute force.)
The backtracking technique is a method of searching for Comprehensive
solutions on acyclic graphs. This method can be optimised by “pruning”
alternatives not leading to a solution.
The graphs represent the possible alternatives to be evaluated.
An Overview of Backtracking
An Overview of Backtracking
All possibilities are represented in a tree (as we already know, saying that
a connected graph is acyclic is equivalent to say it is a tree.)
An Overview of Backtracking
All possibilities are represented in a tree (as we already know, saying that
a connected graph is acyclic is equivalent to say it is a tree.)
The solution is sought by walking through the tree (in a certain way
according to the chosen strategy.)
An Overview of Backtracking
All possibilities are represented in a tree (as we already know, saying that
a connected graph is acyclic is equivalent to say it is a tree.)
The solution is sought by walking through the tree (in a certain way
according to the chosen strategy.)
There are parts of the tree (sub-trees) that are avoided because they may
not contain solutions (pruning.)
An Overview of Backtracking
All possibilities are represented in a tree (as we already know, saying that
a connected graph is acyclic is equivalent to say it is a tree.)
The solution is sought by walking through the tree (in a certain way
according to the chosen strategy.)
There are parts of the tree (sub-trees) that are avoided because they may
not contain solutions (pruning.)
The solution to the problem is presented in an ordered list (x1 , x2 , . . . , xn ).
An Overview of Backtracking
All possibilities are represented in a tree (as we already know, saying that
a connected graph is acyclic is equivalent to say it is a tree.)
The solution is sought by walking through the tree (in a certain way
according to the chosen strategy.)
There are parts of the tree (sub-trees) that are avoided because they may
not contain solutions (pruning.)
The solution to the problem is presented in an ordered list (x1 , x2 , . . . , xn ).
Each element xi of the list is chosen among a set of candidates. Each list
represents a state.
An Overview of Backtracking
All possibilities are represented in a tree (as we already know, saying that
a connected graph is acyclic is equivalent to say it is a tree.)
The solution is sought by walking through the tree (in a certain way
according to the chosen strategy.)
There are parts of the tree (sub-trees) that are avoided because they may
not contain solutions (pruning.)
The solution to the problem is presented in an ordered list (x1 , x2 , . . . , xn ).
Each element xi of the list is chosen among a set of candidates. Each list
represents a state.
Sometimes we seek all solutions; sometimes we settle for one (“soution
state.”)
An Overview of Backtracking
All possibilities are represented in a tree (as we already know, saying that
a connected graph is acyclic is equivalent to say it is a tree.)
The solution is sought by walking through the tree (in a certain way
according to the chosen strategy.)
There are parts of the tree (sub-trees) that are avoided because they may
not contain solutions (pruning.)
The solution to the problem is presented in an ordered list (x1 , x2 , . . . , xn ).
Each element xi of the list is chosen among a set of candidates. Each list
represents a state.
Sometimes we seek all solutions; sometimes we settle for one (“soution
state.”)
Sometimes there is no solution.
1 algorithm backtracking(T , c)
2 if (reject(T , c)) {
3 return
4 }
5 if (accept(T , c)) {
6 return (T , c)
7 }
8 s = first(T , c)
9 while (s 6= NULL) {
10 backtracking(T , s)
11 s = next(T , c)
12 }
1 Introduction to Backtracking
4 Suggested Exercises
The queen is the most powerful piece in the game of chess. It move to
any square located in its same row, column or diagonals.
The queen is the most powerful piece in the game of chess. It move to
any square located in its same row, column or diagonals.
The problem consists of placing n queens in a chessboard of n × n
squares with n ≥ 4 such that no two queen attack each other.
The queen is the most powerful piece in the game of chess. It move to
any square located in its same row, column or diagonals.
The problem consists of placing n queens in a chessboard of n × n
squares with n ≥ 4 such that no two queen attack each other.
Therefore, all queens must be placd in different row and columns.
Q
Q
Q
7Q
Q
7Q
Q Q
7
Q Q
Q Q
7
Q Q
Q
Q
Q
Q Q
7
Q Q
Q 7Q
Ricardo Wehbe Programming III - Class 08
Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises
Q Q
7
Q Q
Q 7Q
Ricardo Wehbe Programming III - Class 08
Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises
Q Q
7
Q Q
Q 7Q
Ricardo Wehbe Programming III - Class 08
Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises
Q Q
7
Q Q
Q 7Q
Ricardo Wehbe Programming III - Class 08
Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises
Q Q
Q Q
7
Q Q
Q 7Q
Ricardo Wehbe Programming III - Class 08
Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises
Q Q
Q Q Q
7
Q Q Q
Q 7Q
Ricardo Wehbe Programming III - Class 08
Introduction to Backtracking
The n Queens Problem
The Subset Sum Problem
Suggested Exercises
Q Q
Q Q Q
7
Q Q Q
Q Q
Q 7 Q
Q
Q
Q Q
Q Q Q
7
Q Q Q
Q Q Q
Q 7 Q
Q
Q
Q
Q
Q Q
Q Q Q
7
Q Q Q
X
Q Q Q
Q 7Q Q
Q
Q
Q
Considering that there can never be more than one queen in a row or in a
column, we can express the solution with a vector of n positions where it
is indicated, for each row, in which column is the corresponding queen.
Each position will then contain a number from 1 to n.
Considering that there can never be more than one queen in a row or in a
column, we can express the solution with a vector of n positions where it
is indicated, for each row, in which column is the corresponding queen.
Each position will then contain a number from 1 to n.
The columns must be checked for repetitions. Since there cannot be
more than a queen in each position of the vector, it is not necessary to
check the rows.
Considering that there can never be more than one queen in a row or in a
column, we can express the solution with a vector of n positions where it
is indicated, for each row, in which column is the corresponding queen.
Each position will then contain a number from 1 to n.
The columns must be checked for repetitions. Since there cannot be
more than a queen in each position of the vector, it is not necessary to
check the rows.
To check the diagonals, let us observe that the squares that a queen
placed at position (i, j) “sees” diagonally are those found in rows i ± k (in
our case we are only interested in the larger values) and columns j ± k.
V = {5, 10, 6, 7}
m = 11
5 0
10
V = {5, 10, 6, 7}
m = 11
5 0
10 0
V = {5, 10, 6, 7}
m = 11
5 0
10 0
6 0
V = {5, 10, 6, 7}
m = 11
5 0
10 0
6 0
7 0
V = {5, 10, 6, 7}
m = 11
5 0
10 0
6 0
7 0
V = {5, 10, 6, 7}
m = 11
5 0
10 0
6 0
7 0
V = {5, 10, 6, 7}
m = 11
5 0
10 0
6 0
7 0
0 7
V = {5, 10, 6, 7}
m = 11
5 0
10 0
6 0
7 0
0 7
V = {5, 10, 6, 7}
m = 11
5 0
10 0
6 0
7 0 6
0 7
V = {5, 10, 6, 7}
m = 11
5 0
10 0
6 0
7 0 6
0 7 6
V = {5, 10, 6, 7}
m = 11
5 0
10 0
6 0
7 0 6
0 7 6
V = {5, 10, 6, 7}
m = 11
5 0
10 0
6 0
7 0 6
0 7 6 13
V = {5, 10, 6, 7}
m = 11
5 0
10 0
6 0
7 0 6
0 7 6 13
V = {5, 10, 6, 7}
m = 11
5 0
10 0
6 0 10
7 0 6
0 7 6 13
V = {5, 10, 6, 7}
m = 11
5 0
10 0
6 0 10
7 0 6 10
0 7 6 13
V = {5, 10, 6, 7}
m = 11
5 0
10 0
6 0 10
7 0 6 10
0 7 6 13 10
V = {5, 10, 6, 7}
m = 11
5 0
10 0
6 0 10
7 0 6 10
0 7 6 13 10
V = {5, 10, 6, 7}
m = 11
5 0
10 0
6 0 10
7 0 6 10
0 7 6 13 10 17
V = {5, 10, 6, 7}
m = 11
5 0
10 0
6 0 10
7 0 6 10
0 7 6 13 10 17
V = {5, 10, 6, 7}
m = 11
5 0
10 0
6 0 10
7 0 6 10 16
0 7 6 13 10 17
V = {5, 10, 6, 7}
m = 11
5 0
10 0
6 0 10
7 0 6 10 16
0 7 6 13 10 17 16 23
V = {5, 10, 6, 7}
m = 11
5 0
10 0 5
6 0 10
7 0 6 10 16
0 7 6 13 10 17 16 23
V = {5, 10, 6, 7}
m = 11
5 0
10 0 5
6 0 10 5
7 0 6 10 16
0 7 6 13 10 17 16 23
V = {5, 10, 6, 7}
m = 11
5 0
10 0 5
6 0 10 5
7 0 6 10 16 5
0 7 6 13 10 17 16 23
V = {5, 10, 6, 7}
m = 11
5 0
10 0 5
6 0 10 5
7 0 6 10 16 5
0 7 6 13 10 17 16 23 5
V = {5, 10, 6, 7}
m = 11
5 0
10 0 5
6 0 10 5
7 0 6 10 16 5
0 7 6 13 10 17 16 23 5
V = {5, 10, 6, 7}
m = 11
5 0
10 0 5
6 0 10 5
7 0 6 10 16 5
0 7 6 13 10 17 16 23 5 12
V = {5, 10, 6, 7}
m = 11
5 0
10 0 5
6 0 10 5
7 0 6 10 16 5
0 7 6 13 10 17 16 23 5 12
V = {5, 10, 6, 7}
m = 11
5 0
10 0 5
6 0 10 5
7 0 6 10 16 5 11
0 7 6 13 10 17 16 23 5 12
V = {5, 10, 6, 7}
m = 11
5 0
10 0 5
6 0 10 5
7 0 6 10 16 5 11
0 7 6 13 10 17 16 23 5 12 11
V = {5, 10, 6, 7}
m = 11
5 0
10 0 5
6 0 10 5
7 0 6 10 16 5 11
0 7 6 13 10 17 16 23 5 12 11
V = {5, 10, 6, 7}
m = 11
5 0
10 0 5
6 0 10 5
7 0 6 10 16 5 11
0 7 6 13 10 17 16 23 5 12 11 18
V = {5, 10, 6, 7}
m = 11
5 0
10 0 5
6 0 10 5
7 0 6 10 16 5 11
0 7 6 13 10 17 16 23 5 12 11 18
V = {5, 10, 6, 7}
m = 11
5 0
10 0 5
6 0 10 5 15
7 0 6 10 16 5 11
0 7 6 13 10 17 16 23 5 12 11 18
V = {5, 10, 6, 7}
m = 11
5 0
10 0 5
6 0 10 5 15
7 0 6 10 16 5 11 15 21
0 7 6 13 10 17 16 23 5 12 11 18 15 22 21 28
Suggested Exercises 1
Suggested Exercises 2
4 We have an n × n chessboard and a King in some arbitrary position (x0 , y0 ) (of course,
x0 , y0 ∈ {1, . . . , n}.) Each square (x , ) has a weight T (x , y ). Each tour
τ = (x0 , y0 ), (x1 , y1 ), . . . , (xk , yk ) has a value V (τ ) given by the following expression:
k
X
V (τ ) = i · T (xi , yi )
i=0
Find an algorithm to determine the minimum-weight tour such that all squares of the chessboard are visited
by the King without repetitions. Recall that a King can move to any neighbouring square in any direction.