Week03 Backtracking
Week03 Backtracking
AND
ALGORITHSMS
CONTENT
• Sudoku puzzles
ALGORITHMS
WEEK 3: RECURSIVE BACKTRACKING
3 4
GENERAL DIAGRAM GENERAL DIAGRAM
Combination enumeration problem: List the sets x = (x[1], x[2],…, x[k], x[k+1],. . ., x[n]) with Combination enumeration problem: List the sets x = (x[1], x[2],…, x[k], x[k+1],. . ., x[n]) with
x[ i] ∈ Ai , i = 1, 2, ..., n and satisfy the given set of constraints P. x[ i] ∈ Ai , i = 1, 2, ..., n and satisfy the given set of constraints P.
Example: The call to execute the backtracking recursiveTry(k){// try values assignable to x[k]
• “The problem of listing a binary string of length n” leads to listing the sets x = (x[1], x[2],…, algorithm is: Try(1); for v in candidates(k) do {
x[k], x[k+1],. . ., x [n]) with x[i] {0, 1}, i = 1, 2, ..., n If only need to find one solution, you need to
if (check(v,k)) then {
x[k] = v;
• “The problem of listing a binary string of length n with an even number of 0 bits” leads to find a way to terminate the nested recursive
[Update the data structure D]
listing the sets x = (x[1], x[2],…, x[k], x[k +1],. . ., x[n]) with x[i] {0, 1}, i = 1, 2, ..., n and procedure calls generated by the Try(1) call
if (k == n) then solution();
satisfying the constraint: number of elements x[i] = 0 with i = 1, 2, ..., n is an even number. after the first solution is recorded.
else Try(k+1);
• The backtracking algorithm allows solving combinatorial enumeration problems. There If at the end of the algorithm we do not get [Recover the data structure D]
are two ways to implement the backtracking algorithm: recursive or non-recursive. any solution, it means the problem has no }
solution. }
}
5 6
Combination enumeration problem: List the sets x = (x[1], x[2],…, x[k], x[k+1],. . ., x[n]) with Given a positive integer n ≥ 1. List all binary strings of length n in lexicographic order.
x[ i] ∈ Ai , i = 1, 2, ..., n and satisfy the given set of constraints P. For example: n = 3, we have binary strings of length 3 that need to be listed in the following
order:
The call to execute the backtracking recursiveTry(k){// try values assignable to x[k]
• 000
algorithm is: Try(1); for v in candidates(k) do {
• 001
if (check(v,k)) then {
x[k] = v;
• 010
[Update the data structure D] • 011
if (k == n) then solution(); • 100
else Try(k+1);
• 101
[Recover the data structure D]
• 110
}
}
• 111
}
7 8
LISTING BINARY STRINGS LISTING BINARY STRINGS
Given a positive integer n ≥ 1. List all binary strings of length n in lexicographic order. void Try(int k){
Try to list binary strings of length 3
for (int v = 0; v <= 1; v++){
Solution representation: each binary string is represented by the array (x[1], x[2], . . ., x[n]) in ()
x[k] = v; Try(1)
which x[k] ∈{0,1} is the kth bit in binary string.
if (k == n) solution();
Try(k){// try values assignable to x[k] else Try(k+1);
0 1
void Try(int k){
for v in candidates(k) do { }
for (int v = 0; v <= 1; v++){ (0) (1)
if (check(v,k)) then { } Try(2) Try(2)
x[k] = v;
x[k] = v;
Determine:
candidates(k) 0 1 0 1
if (k == n) solution();
[Update the data structure D] check(v, k) (00) (01) Try(3) (10) (11)
else Try(k+1); Try(3) Try(3) Try(3)
if (k == n) then solution();
} 0 1 0 1 0 1 0 1
else Try(k+1);
[Recover the data structure D] }
}
(000) (001) (010) (011) (100) (101) (110) (111)
}
}
9 10
Given a positive integer n ≥ 1. List all permutations of n numbers 1, 2, …, n in lexicographic order. Solution representation: each permutation of n elements is represented by the array (x[1], x[2],
. . ., x[n]) where :
For example: n = 3, we have the permutations of 1, 2, 3 in lexicographic order as follows :
• x[k] {1, 2,…, n} is the kth element in the permutation
• (1, 2, 3)
• x[k] ≠ x[1], x[2],… x[k-1], x[k+1], …,x[n]
• (1, 3, 2) Try(k){// try values assignable to x[k]
void Try(int k){
• (2, 1, 3) for v in candidates(k) do {
Determine: for (int v = 1; v <= n; v++){
if (check(v,k)) then {
• (2, 3, 1) candidates(k) if (check(v, k)) {
x[k] = v; check(v, k)
• (3, 1, 2) x[k] = v;
[Update the data structure D]
if (k == n) solution();
• (3, 2, 1) if (k == n) then solution();
else Try(k+1);
else Try(k+1);
} }
} }
11 } 12
LISTING PERMUTATIONS LISTING PERMUTATIONS
void Try(int k){
#include <stdio.h> void Try(int k){ • Marking technique
for(int v = 1; v <= n; v++){
for (int v = 1; v <= n; v++){
• used[v] = 1: v appear
int n; if (used[v]==0){
• used[v] = 0: v does not appear
int x[100]; if (check(v, k)) { x[k] = v;
try(k){//try values assignable to x[k]
void solution(){ x[k] = v; used[v] = 1;
for v in candidates(k) do {
for(int k = 1; k <= n; k++) if (k == n) solution(); if (k == n) solution();
if (check(v,k)) then { else Try(k+1);
printf("%d ",x[k]); else Try(k+1);
x[k] = v; used[v] = 0;
printf("\n"); }
[Update the data structure D] }
} }
if (k == n) then solution(); }
int check(int v, int k) { }
else try(k+1); }
for (int i = 1; i <= k-1; i++) int main(){
int main(){
[Recover the data structure D]
if (x[i] == v) return 0; scanf(“%d”,&n); scanf(“%d”,&n);
}
return 1; Try(1); for(int v = 1; v <= n; v++) used[v] = 0;
}
} } Try(1);
}
}
13 14
15 16
SODOKU PUZZLES SODOKU PUZZLES
State the problem: Solution representation: X[i, j] is the numeric value filled in cell
Given a 9x9 square grid divided into 9 3x3 grids. 1 2 3 4 5 6 7 8 9 row i column j (i, j = 0, 1, 2, . . ., 8) 1 2 3 4 5 6 7 8 9
4 5 6 7 8 9 1 2 3 Marking array: 4 5 6 7 8 9 1 2 3
Rows and columns are numbered 0, 1, 2, …, 8
7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6
List all the ways to fill in the numbers 1, 2. …, 9 markR[r, v] = 1: v appears in row r and markR[r, v] = 0 for
2 1 4 3 6 5 8 9 7 2 1 4 3 6 5 8 9 7
others(r = 0, 1, …, 8 và v = 1, 2, . . ., 9)
cells in a 9x9 square grid such that: 3 6 5 8 9 7 2 1 4 3 6 5 8 9 7 2 1 4
8 9 7 2 1 4 3 6 5 markC[c, v] = 1: v appears in column c and markC[c, v] = 0, for 8 9 7 2 1 4 3 6 5
The numbers on each line are different,
5 3 1 6 4 2 9 7 8 others (c = 0, 1, 2, . . ., 8 và v = 1, 2, . . ., 9) 5 3 1 6 4 2 9 7 8
The numbers on each column are different, 6 4 2 9 7 8 5 3 1 6 4 2 9 7 8 5 3 1
markS[i, j, v] = 1: v appear in the grid 3x3 at row i and column j
The numbers on each 3x3 grid are different. 9 7 8 5 3 1 6 4 2 9 7 8 5 3 1 6 4 2
,and markS[i, j, v] = 0 for others (i, j = 0, 1, 2 và v = 1, 2, . . ., 9)
17 18
SODOKU PUZZLES
Try(r, c){
Order of browsing cells to test
for v = 1 to 9 do {
values: top to bottom and left to
if (check(v,r,c)) then {
right
X[r,c] = v;
Recursive function Try(r, c): tries markR[r,v] = 1; markC[c,v] = 1; markS[r/3,c/3,v] = 1;
the value for cell row r column c if r = 8 and c = 8 then solution();
check(v, r, c){
else {
if c = 8 then Try(r+1, 0); else Try(r, c+1);
THANK YOU !
if markR[r,v] = 1 then return 0; }
if markC[c,v] = 1 then return 0; markR[r,v] = 0; markC[c,v] = 0; markS[r/3,c/3,v] = 0;
if markS[r/3,c/3,v] = 1 then return 0; }
return 1; }
} }
19 20