Backtracking
Backtracking
Backtracking Algorithms
Jose Ortiz
[email protected]
fi
Backtracking Sum of Subsets
∑
Given the following set of items. S = {3,1,8,4} nd all its subsets s where si = m; m = 12
i=1
{}
x1 = 1 x1 = 0
3 3
x2 = 1 x2 = 0 x2 = 1 x2 = 0
1 1 1 1
x3 = 1 x3 = 0 x3 = 1 x3 = 0 x3 = 0
x3 = 1 x3 = 1 x3 = 0
8 8 8 8 8 8 8 8
x4 = 1 x4 = 1
x4 = 1 x4 = 1 x4 = 1 x4 = 1 x4 = 1 x4 = 1
x4 = 0 x4 = 0 x4 = 0 x4 = 0 x4 = 0 x4 = 0 x4 = 0 x4 = 0
4 4 4 4 4 4 4 4 4 4 4 4 4
4 4 4
∑
Given the following set of items. S = {3,1,8,4} nd all its subsets s where si = m; m = 12
{wi, pi} i=1
Backtracking :
0,16 Bounds
x1 = 1 n n
∑ ∑
wi xi ≤ m wi pi ≥ 0
i=1 i=1
x2 = 1
3,13
Operations
4,12
x3 = 1 x3 = 0 1. xi = 1 (included)
xi = 0 (not included)
12,4 4,4
x4 = 1 x4 = 0 2. Add Xi to Wi
3. Substract Xi from Pi
8,0 4,0
Copyright Jose Ortiz
fi
Sum of Subsets Backtracking
n
∑
Given the following set of items. S = {3,1,8,4} nd all its subsets s where si = m; m = 12
{wi, pi} i=1
Backtracking :
0,16 Bounds
x1 = 1 n n
∑ ∑
wi xi ≤ m wi pi ≥ 0
i=1 i=1
x2 = 1
3,13
Operations
4,12
x3 = 1 x3 = 0 1. xi = 1 (included)
xi = 0 (not included)
12,4 4,4
x4 = 1 x4 = 0 2. Add Xi to Wi
3. Substract Xi from Pi
8,0 4,0
Copyright Jose Ortiz
fi
Sum of Subsets Backtracking
n
∑
Given the following set of items. S = {3,1,8,4} nd all its subsets s where si = m; m = 12
{wi, pi} i=1
Backtracking :
0,16 Bounds
x1 = 1 n n
∑ ∑
xi+1 ≤ m wi pi ≥ 0
i=0 i=1
x2 = 1
3,13 x2 = 0
Operations
4,12 3,12
x3 = 1 x3 = 0 1. xi = 1 (included)
x3 = 1 x3 = 0
xi = 0 (not included)
12,4 4,4 11,4 3,4
x4 = 1 2. Add Xi to Wi
x4 = 0 x4 = 1 x4 = 1 x4 = 0
3. Substract Xi from Pi
8,0 4,0 15,0 7,0 3,0
Copyright Jose Ortiz
fi
Sum of Subsets Backtracking
n
∑
Given the following set of items. S = {3,1,8,4} nd all its subsets s where si = m; m = 12
{wi, pi} i=1
Backtracking :
0,16
x1 = 0
x1 = 1
3,13 x2 = 1
0,13
x2 = 1 x2 = 0
4,12 1,12
3,12 x3 = 1
x3 = 1 x3 = 0 x3 = 1 x3 = 0
∑
Given the following set of items. S = {3,1,8,4} nd all its subsets s where si = m; m = 12
{wi, pi} i=1
Backtracking :
0,16
x1 = 0
x1 = 1
3,13 x2 = 1
0,13
x2 = 1 x2 = 0
4,12 1,12
3,12 x3 = 1 x3 = 0
x3 = 1 x3 = 0 x3 = 1 x3 = 0
∑
Given the following set of items. S = {3,1,8,4} nd all its subsets s where si = m; m = 12
{wi, pi} i=1
Backtracking :
0,16
x1 = 0
x1 = 1
3,13 x2 = 1
0,13 x2 = 0
x2 = 1 x2 = 0
∑
Given the following set of items. S = {3,1,8,4} nd all its subsets s where si = m; m = 12
{wi, pi} i=1
Solutions are: {3,1,8} and {8,4}
0,16
x1 = 0
x1 = 1
3,13 x2 = 1
0,13 x2 = 0
x2 = 1 x2 = 0
8,0 4,0 7,0 3,0 13,0 8,0 4,0 12,0 4,0 0,0
15,0
Copyright Jose Ortiz
fi
Sum Of Subsets Pseudocode
Backtracking N-Queens
• Same column
• Same row
• Diagonal
1 2 3 4 1 2 3 4 1 2 3 4
1 q1 1 q1 1 q1 q2
2 q2 2 q2 2
3 q3 3 q3 3 q3
4 q4 4 q4 4 q4
1 2 3 4 1 2 3 4
1 q1 1 q1
2 q2 2 q2
3 q3 3 q3
4 q4 4 q4
fi
Backtracking N-Queens
Links Numbers Repesent {row, column}
1,1 {} 1,4 1 2 3 4
q1
q1 1 q1
q1 1,3 2 q2
2,1 q1 1,2 q1
3
q2 4
1,1 {} 1,4 1 2 3 4
q1 q1
q1 q1 1
1,3 2 q2
2,1 q1 1,2 q1
3
q2 2,2
q2 q2 4
q2
Copyright Jose Ortiz
Backtracking N-Queens
Links Numbers Repesent {row, column}
1,1 {} 1,4
q1
q1 q1
2,1 1,2 1,3
q1 q1
q2 2,2 1 2 3 4
q2 q1
q2 2,3 1
q2 q2 2 q2
q2 q 3 q3
2
3,1 4
q3
1,1 {} 1,4
q1
q1 q1
2,1 1,2 1,3
q1 q1
q2 2,2 1 2 3 4
q2 q1
q2 2,3 1
q2 q2 2 q2
q2 q 3 q3
2
3,1 4
q3 q2
3,2
q2 q2
q3 q
2
1,1 {} 1,4
q1
q1 q1
2,1 1,2 1,3
q1 q1
q2 2,2 1 2 3 4
q2 q1
q2 2,3 1
q2 q2 2 q2
q2 q 3 q3
2q
2
3,1 3,2 3,3 q3 4
q3 q2 q q3
3
q2 q2
1,1 {} 1,4
q1
q1 q1
2,1 1,2 1,3
q1 q1
q2 2,2 1 2 3 4
q2 q1
q2 2,3 1
q2 q2 2 q2
q2 q 3,4 3 q3
2q
2
3,1 3,2 q3 4
3,3
q3 q2 q q3
3
q2 q2
1,1 {} 1,4
q1 Solution Found!
q1 q1
1,2 1,3 1 2 3 4
q1 q1
2,1 1 q1
2,2 2,4 2 q2
q2 2,3 q3 3 q3
q2 q4
q2 q2q q2 4
2
3,1
q3
q2 4,1
q q3
2 q2
As we continue with the process, more solutions will be found
q4 4,2 4,3
qq24 q4
23
q q Copyright Jose Ortiz
Backtracking N-Queens
Time Complexity Analysis
• Brute Force:
• Solving this problem by brute force implies that we need to examine all the possible permutations.
For instance, {0,........., (n − 1)} is O(n!)
• Backtracking:
• One solution: the best case occurs if you nd the solution before exploiting all the possible
arrangements (rarely happens). Also the time complexity of nding the rst solution depends on
your implementation
• All the solutions: the same as brute force O(n!). However, in average time complexity hits the
lower bounds of O(n!) since there are many permutations that are abandoned during the process.
• The backtracking algorithm can be further improved using memoization for all the recursive calls.
3 4 0
Level 1 = 3 vertex
n 4+1
1
Level 1 = 3 vertices 4 3 −1
∑
3 =
2
Level 1 = 3 vertices i=1
2
3
Level 1 = 3 vertices O(3 n+1
) = O(m n+1
)
4
Level 1 = 3 vertices
Copyright Jose Ortiz
M-Graph Coloring Backtracking
{}
{}
1
1 1
1
1
1
2 2
2
No! Yes!
1 2 1 2
3 4 3 4
2 2 2 2
3 3 3
1 2 1 2
3 4 3 4
No! No!
2 2 2 2
1 2
3 3 3 3
3 3
1 2
4
3 4
We have one of the solutions!
If you keep going, Yes!
3 4
many solutions will be found
Yes!
0 1
3 2
G 0 1 2 3
0 1 1 0 1
1 1 1 1 1
2 0 1 1 1
3 1 1 1 1
0 = not connected
1 = connected
• Backtracking:
• The recursion takes n times since it needs to visit all the n nodes recursively, for
every visited node, the function needs to check is there is a valid color for that node
by visiting all its adjacent nodes. Therefore, the time complexity of solving this
n
problem with backtracking is O(nm ), which is slightly better than the one from the
brute force approach