12.05.backtracking Algorithms
12.05.backtracking Algorithms
Backtracking algorithms
ece.uwaterloo.ca
dwharder@alumni.uwaterloo.ca
Outline
Backtracking
Backtracking
Suppose there are just too many partial solutions at any one time to
keep track of
– At any point in time in a game of chess or Go ( 围棋 or 碁 ), there are a
plethora of moves, each valid, but the usefulness of each will vary
Backtracking algorithms
5
Sudoku
5 4 6
6 3 1
9 2 5 4
6 8 1
8 4
2 6 1
9 5 8 1
1 2 3
8 2 7
http://www.sudokuoftheday.com/
Backtracking algorithms
6
Sudoku
At least for the first entry in the first square, only 1, 3, 7 fit
5 4 6
6 3 1
9 2 5 4
6 8 1
8 4
2 6 1
9 5 8 1
1 2 3
8 2 7
5 1 4 6 5 3 4 6 5 7 4 6
6 3 1 6 3 1 6 3 1
9 2 5 4 9 2 5 4 9 2 5 4
6 8 1 6 8 1 6 8 1
8 4 8 4 8 4
2 6 1 2 6 1 2 6 1
9 5 8 1 9 5 8 1 9 5 8 1
1 2 3 1 2 3 1 2 3
8 2 7 8 2 7 8 2 7
Backtracking algorithms
7
Sudoku
If the first entry has a 1, the 2nd entry in that square could be 7 or 8
5 1 4 6
6 3 1
9 2 5 4
6 8 1
8 4
2 6 1
9 5 8 1
1 2 3
8 2 7
5 1 4 6 5 1 4 6
7 6 3 1 8 6 3 1
9 2 5 4 9 2 5 4
6 8 1 6 8 1
8 4 8 4
2 6 1 2 6 1
9 5 8 1 9 5 8 1
1 2 3 1 2 3
8 2 7 8 2 7
Backtracking algorithms
8
Sudoku
If the first entry has a 3, the 2nd entry in that square could be 7 or 8
5 3 4 6
6 3 1
9 2 5 4
6 8 1
8 4
2 6 1
9 5 8 1
1 2 3
8 2 7
5 3 4 6 5 3 4 6
7 6 3 1 8 6 3 1
9 2 5 4 9 2 5 4
6 8 1 6 8 1
8 4 8 4
2 6 1 2 6 1
9 5 8 1 9 5 8 1
1 2 3 1 2 3
8 2 7 8 2 7
Backtracking algorithms
9
Sudoku
If the first entry has a 7, the 2nd entry in that square could be 8
5 7 4 6
6 3 1
9 2 5 4
6 8 1
8 4
2 6 1
9 5 8 1
1 2 3
8 2 7
5 7 4 6
8 6 3 1
9 2 5 4
6 8 1
8 4
2 6 1
9 5 8 1
1 2 3
8 2 7
Backtracking algorithms
10
Sudoku
5 4 6
6 3 1
9 2 5 4
6 8 1
8 4
2 6 1
In the next child, there are no 1
9 5
2
8 1
3
next entry 5 1 4
6 3 1
6 5 3 4
6 3 1
6 5 7 4
6 3 1
6
9 2 5 4 9 2 5 4 9 2 5 4
6 8 1 6 8 1 6 8 1
8 4 8 4 8 4
2 6 1 2 6 1 2 6 1
9 5 8 1 9 5 8 1 9 5 8 1
1 2 3 1 2 3 1 2 3
8 2 7 8 2 7 8 2 7
5 1 4 6 5 1 4 6 5 3 4 6 5 3 4 6 5 7 4 6 5 7 4 6
7 6 3 1 8 6 3 1 7 6 3 1 8 6 3 1 1 6 3 1 8 6 3 1
9 2 5 4 9 2 5 4 9 2 5 4 9 2 5 4 9 2 5 4 9 2 5 4
6 8 1 6 8 1 6 8 1 6 8 1 6 8 1 6 8 1
8 4 8 4 8 4 8 4 8 4 8 4
2 6 1 2 6 1 2 6 1 2 6 1 2 6 1 2 6 1
9 5 8 1 9 5 8 1 9 5 8 1 9 5 8 1 9 5 8 1 9 5 8 1
1 2 3 1 2 3 1 2 3 1 2 3 1 2 3 1 2 3
8 2 7 8 2 7 8 2 7 8 2 7 8 2 7 8 2 7
5 1 4 6
7 6 × 3 1 Any candidate solution built from this
9 2 5 4
6 8 1
partial is infeasible
8 4 – We can ignore this branch
2 6 1
9 5 8 1
1 2 3
8 2 7
Backtracking algorithms
11
Sudoku
5 4 6
6 3 1
9 2 5 4
6 8 1
8 4
2 6 1
Three other branches lead to 1
9 5
2
8 1
3
5 1 4 6 5 3 4 6 5 7 4 6
6 3 1 6 3 1 6 3 1
9 2 5 4 9 2 5 4 9 2 5 4
6 8 1 6 8 1 6 8 1
8 4 8 4 8 4
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 8 1 9 5 8 1 9 5 8 1 9 5 8 1 9 5 8 1
1 2 3 1 2 3 1 2 3 1 2 3 1 2 3
8 2 7 8 2 7 8 2 7 8 2 7 8 2 7
5 1 4 6 5 3 4 6
8 6 7 3 1 8 6 7 3 1
3 9 2 5 4 1 9 2 5 4
6 8 1 6 8 1
8 4 8 4
2 6 1 2 6 1
9 5 8 1 9 5 8 1
1 2 3 1 2 3
8 2 7 8 2 7
Backtracking algorithms
13
Sudoku
Sudoku
Implementation
The main function creates the initial matrix and calls backtrack
Backtracking algorithms
16
Implementation
// Find the next empty location in 'matrix'
// If one is found, assign 'i' and 'j' the indexes of that
entry
// Otherwise, return false
// - In this case, the values of 'i' and 'j' are undefined
bool next_location( int matrix[9][9], int &i, int &j ) {
// If 'value' already appears in
for ( int i1 = 0; i1 < 3; ++i1 ) {
// - the row 'm'
for ( int j1 = 0; j1 < 3; ++j1 ) {
// - the column 'n'
for ( int i2 = 0; i2 < 3; ++i2 ) {
// - the 3x3 square of entries it (m, n) appears in
for ( int j2 = 0; j2 < 3; ++j2 ) {
// return true, otherwise return false
i = 3*i1 + i2;
bool is_valid( int matrix[9][9], int m, int n, int value ) {
j = 3*j1 + j2;
// Check if 'value' already appears in either a row or
column
// return 'true' if we find an for ( int i = 0; i < 9; ++i ) {
// unoccupied entry
if ( (matrix[m][i] == value) || ( matrix[i][n] ==
if ( matrix[i][j] == 0 ) value ) )
return true; return false;
} }
}
} // Check if 'value' already appears in either a row or
} column
int ioff = 3*(m/3);
return false; // all the entries are occupied int joff = 3*(n/3);
}
for ( int i = 0; i < 3; ++i ) {
for ( int j = 0; j < 3; ++j ) {
if ( matrix[ioff + i][joff + j] == value )
return false; // 'value' already in the 3x3
square
}
}
Backtracking algorithms
17
Implementation
bool backtrack( int matrix[9][9] ) {
int i, j;
Implementation
int main() {
int matrix[9][9] = {
{5, 0, 4, 0, 0, 0, 0, 6, 0},
{0, 6, 0, 3, 0, 0, 1, 0, 0},
{0, 9, 2, 5, 4, 0, 0, 0, 0},
{6, 0, 8, 0, 0, 1, 0, 0, 0},
{0, 0, 0, 8, 0, 4, 0, 0, 0},
{0, 0, 0, 2, 0, 0, 6, 0, 1},
{0, 0, 0, 0, 9, 5, 8, 1, 0},
{0, 0, 1, 0, 0, 2, 0, 3, 0},
{0, 8, 0, 0, 0, 0, 2, 0, 7}
};
return 0;
}
Backtracking algorithms
19
Implementation
5 3 4 1 7 8 9 6 2
8 6 7 3 2 9 1 4 5
1 9 2 5 4 6 3 7 8
6 7 8 9 3 1 5 2 4
2 1 5 8 6 4 7 9 3
3 4 9 2 5 7 6 8 1
4 2 3 7 9 5 8 1 6
7 5 1 6 8 2 4 3 9
9 8 6 4 1 3 2 5 7
Backtracking algorithms
20
Backtracking
Classical applications
♕
Backtracking algorithms
23
Knight’s tour
♘
Backtracking algorithms
24
% Relationships
mother(M, X) :- parent(M, X), female(M).
father(F, X) :- parent(F, X), male(F).
sister(S, X) :- sibling(S, X), female(S), \+ (S = X).
brother(B, X) :- sibling(B, X), male(B), \+ (S = X).
grandparent(G, X) :- parent(G, P), parent(P, X).
% Symmetric
spouses(X, Y) :- spouses(Y, X);
sibling(X, Y) :- parent(P, X), parent(P, Y), \+ (X = Y).
cousin(X, Y) :- parent(A, X), parent(B, Y), sibling(A, B).
% Antisymmetric
uncle(U, X) :- male(U), sibling(U, Y), parent(Y, X).
uncle(U, X) :- male(U), spouse(U, Z), sibling(Z, Y),
parent(Y, X).
aunt(A, X) :- female(A), parent(Y, X), sibling(A, Y).
aunt(A, X) :- female(A), spouse(A, Z), sibling(Z, Y),
parent(Y, X).
Backtracking algorithms
26
Parsing
<nondigit> ::= A | B | … | Z | a | b | … | z | _
<identifier> ::= <nondigit> | <identifier><nondigit>
| <identifier><digit>
<nondigit> ::= A | B | … | Z | a | b | … | z | _
<identifier> ::= <nondigit> | <identifier><nondigit>
| <identifier><digit>
Note that in C/C++, 052 defines the octal number equal to 4210
Backtracking algorithms
32
Backjumping
Searching a maze
In trying to find your way through a maze, one simple rule works
quite nicely:
– The right-hand rule:
Touch a wall with your right hand, and continue forward always
keeping your right hand touching a wall until you get out.
Backtracking algorithms
34
Searching a maze
It doesn’t work if you’re trying to get into the maze or out of a maze
Backtracking algorithms
35
Searching a maze
Searching a maze
References
Wikipedia, http://en.wikipedia.org/wiki/Backtracking
[1] Cormen, Leiserson, and Rivest, Introduction to Algorithms, McGraw Hill, 1990, §11.1, p.200.
[2] Weiss, Data Structures and Algorithm Analysis in C++, 3 rd Ed., Addison Wesley, §9.2, p.342-5.
These slides are provided for the ECE 250 Algorithms and Data Structures course. The
material in it reflects Douglas W. Harder’s best judgment in light of the information available to
him at the time of preparation. Any reliance on these course slides by any party for any other
purpose are the responsibility of such parties. Douglas W. Harder accepts no responsibility for
damages, if any, suffered by any party as a result of decisions made or actions based on these
course slides for any other purpose than that for which it was intended.