Lec09 BacktrackingBranchBound
Lec09 BacktrackingBranchBound
BACKTRACKING
Learning Outcomes
Assume that you are in the maze. You do not know the
exact exit path, but just walk in and find the way.
You may turn to a dead end. When that happen, you will
just back to the point where you make the turn
You will backtrack to take the next turn.
This process will repeat until you find the exit path.
The Concept
Generally:
Each object is represented by an array A[1:n]
Element of the array are from a domain D={d1,d2,
…,dm}
Commonly, D is a finite set of successive
integers
The values of array A must satisfy some
constraints C
Subset Sum Problem ~ A Knapsack
Problem
ssp(sum, k, r)
{ //left child
flag[k] :=1; //indicates the include or non-inclusion of an element
if(sum + s[k] + s[k+1] <=m)
write (flag[i:k]);
else if (sum+s[k]+s[k+1]<=m)
ssp(sum+s[k], k+1, r – s[k]);
//right child
if((sum + r + s[k] >= m) and (sum + s[k+1] <= m))
{
flag[k] := 0;
ssp(sum, k+1, r – s[k]);
}
}
That is a lot
That is what a DFS will do, check all solution
N Queens Problem
j represents the
queen
i represents the row
Application:
Is it possible to visit all cities (vertices) in a graph
exactly once and come back to the starting city
again?
A simple cycle that passes through every vertex of
graph G exactly once.
Hamiltonian Cycle
a
e
d d c
f B
A
d f
a b d e
Hamiltonian Cycle
1 1
Reach the
dead end, so
2 3 4 2 3 4 backtrack to
4.
1 3 5 4 is connected to 1 and 5. If we
choose 1, it is the end and no
5 Hamiltonian cycle. Thus, backtrack to
1 4 5
3 and select 5.
2 and 3 have visited, so choose
1 5 4.
2 3 4
2 3 4 1 5
1. Algorithm NextValue(r)
2. {
3. while(true)
4. {
5. x[r] := (x[r] + 1) mod (n+1); //next vertex
6. if (x[r] = 0)
7. { return; }
8. if (G[x[r-1], x[r]] ≠ 0)
9. { //is there an edge?
10. for j:=1 to r – 1 step 1 do
11. if(x[j] = x[r])
12. { break; }
13. if(j==r) then //if true, vertex is distinct
14. if((r < n) || ((r = n) && G[x[n], x[1]] ≠ 0))
15. return;
16. }
17. }
BRANCH & BOUND
Generally:
Set a upper bound or a lower bound as the target
to achieve
It could be the maximum or the minimum target
(local maximum or local minimum)
The most difficult part is to design the bound
function
It works well when the estimation of cost is
almost accurate
Otherwise, it consumes a lot of memory and time
Setting the bound
BEST WORST
EX IN 1
1
EX IN 2 EX IN 2
2 2
IN = include; EX = exclude
In the algorithm, 1 indicate IN, 0 indicate
EX
Example 1: Knapsack problem
Sort all items in decreasing order of ratio (the ratio enables us to
count the upper bound by using Greedy Approach)
Initialize max profit =0
Create an empty queue, Q
Create a dummy node, enqueue to queue, profit and weight are
zeros
While Q is empty:
Extract an item from Q
Compute profit of next level node. If the profit > maxProfit, update
maxProfit
Compute bound of next level node. If bound>maxProfit, add next
level node to Q
Consider the case when next level node is not considered as part
of the solution (EX case) and add node to queue with level as
next, but weight and profit without considering next level nodes
Example 1: Knapsack problem
W = 100, count the ratio, rearrange the items
according to ratio. Start from the highest
ratio.
Ite Weig Valu New Item Weight Value Ratio
m ht e Index(i) No (w) (v) (v/w)
No 1 7 30 60 2
1 40 40
2 2 50 60 6/5
2 50 60
3 1 40 40 1
3 30 10
4 4 10 10 1
4 10 10
5 6 40 20 ½
5 10 3
6 3 30 10 1/3
6 40 20
7 5 10 3 3/10
7 30 60
Objective is to maximize the value, with x = 1
or 0,
Maximize: where N = total no. of items
Example 1: Knapsack problem
EX index IN index
6 6
12 13
=60+60+10+ Cannot proceed, so -999
(3/10) (10)=133
Example 1: Knapsack problem
=60+60+10+ 12
EX index IN index
(3/10) (10)=133
7 7
=60+60+10=130 =60+60+10+
(3/10) (10)=133
Through the UB, we can stop to further explore by marking the node as -999.
When we reach the last items, then stop to branch the tree.
Final result: include indexes 1 2 and 3 where B(3) is having the maximal
value.
This is not the only branch and bound method. Next, we look at least cost
method.
Example 2 ~ Job Assignment
Job assignment
One worker performs exactly one job
A set of workers P = {P1, P2, …Pn}
One job is assigned to one worker
A set of jobs J = {J1, J2, …Jn}
Each job has a cost associated to it
Cost Cij represents the cost of assigning Pi with Jj
Total cost of the assignment is minimised:where
Xij = 1 if Pi is assigned to Jj
Xij = 0 otherwise
Example 2 ~ Job Assignment
To get the lower bound could be tedious. You can
travel along the diagonal of each column, and set it as
the lower bound (e.g.: 9+4+1+4= 18) or get the
minimum cost of each column, and use as lower
bound (2+3+1+4=10), or the minimum of each column
without repeat (2+3+5+4=14)
START
lb = 2+3+5+4 =
14
1 2 3 4
START
lb = 2+3+5+4 =
14
1 2 3 4
5 6 7
b1 b 3 b4
lb = lb = lb =
2+6+1+4=13 2+3+5+4=14 2+7+1+7=17
6 6
c 3 c 4
lb = lb =
2+6+1+4=13 2+6+8+9=25
Example 2 ~ Job Assignment
Example 3 ~ Travelling Salesman Problem (TSP)
1 ∞ 7 2 10 5
2 2 ∞ 5 10 6
3 4 7 ∞ 5 12
4 3 5 7 ∞ 2
5 12 7 3 4 ∞
1 ∞ 7 2 10 5 (-2)
2 2 ∞ 5 10 6 (-2)
3 4 7 ∞ 5 12 (-4)
4 3 5 7 ∞ 2 (-2)
5 12 7 3 4 ∞ (-3)
Step 1: reduce the matrix such that each row and each column
has a zero.
Start from row, get the minimum value of each row and subtract.
After minus the highlighted minimum element, we get the
following matrix.
∞ 5 0 8 3
0 ∞ 3 8 4
0 3 ∞ 1 8
1 3 5 ∞ 0
9 4 0 1 ∞
Example 3 ~ TSP
1 2 3 4 5
1 ∞ 5 0 8 3
2 0 ∞ 3 8 4
3 0 3 ∞ 1 8
4 1 3 5 ∞ 0
5 9 4 0 1 ∞
0 ∞ 3 7 4
0 0 ∞ 0 8
1 0 5 ∞ 0
9 1 0 0 ∞
Example 3 ~ TSP
1 2 3 4 5
1 ∞ 2 0 7 3
2 0 ∞ 3 7 4
3 0 0 ∞ 0 8
4 1 0 5 ∞ 0
5 9 1 0 0 ∞
Cost(1)=2+2+4+2+3+3+1= 17
This will set as optimal cost.
Example 3 ~ TSP 1:1
7
1 2 3 4 5
1 ∞ ∞
∞
2 ∞
0 ∞
7 ∞
3 2:19
2 5
3 4
2 ∞
0 ∞ 3 7 4
3 0 ∞
0 ∞ 0 8
4 1 ∞
0 5 ∞ 0
5 9 ∞
1 0 0 ∞
Step 2: Find the node that must be visited after the first node
Get the matrix value from 1 to n (cost of edge): M[1,n], where n is the adjacent vertex
Set row 1 and column 2 to ∞
Set M[2, 1] = ∞
Reduce the matrix if the row/column contains no zero
Cost = M[1,n] + reduction cost + cost(parent node)
Example 3 ~ TSP 1:1
7
1 2 3 4 5
1 ∞
∞ ∞
2 ∞
0 ∞
7 ∞
3 5
2:19 3:17
3
0 ∞ ∞ 4
2 3 7 4
3 ∞
0 0 ∞
∞ 0 8
4 1 0 ∞
5 ∞ 0
5 9 1 ∞
0 0 ∞
Cost(1,3) =0 + 0 + cost(1)=17
Example 3 ~ TSP 1:1
7
1 2 3 4 5
1 ∞
∞ ∞
2 ∞
0 ∞
7 ∞
3 5
2:19 3:17
0 ∞ ∞
4
4:24
2 3 7 4
3 0 0 ∞ ∞
0 8
4 ∞
1 0 5 ∞ 0
5 9 1 0 ∞
0 ∞
Cost(1,4) =7 + 0 + cost(1)=24
Example 3 ~ TSP 1:1
7
1 2 3 4 5
1 ∞ ∞
2 ∞
0 ∞
7 ∞
3 2:19 3:17 5
5:20
2 0 ∞ 3 7 ∞
4
4:24
3 0 0 ∞ 0 ∞
8
4 1 0 5 ∞ ∞
0
5 ∞
9 1 0 0 ∞
Cost(1,5) =3 + 0 + cost(1)=20
1 ∞ ∞
2 ∞
0 ∞
7 ∞
3 2:19 3:17 5:20
2 ∞
0 ∞ ∞
3 73 40 -4
4:24
3 ∞
∞
0 0
∞ ∞
∞ ∞
0 ∞
8
4 0
1 0
∞ ∞
5 ∞ 0 1-3-2:22
2 4 5
5 8
9 ∞
1 ∞
0 0 ∞
-1
Cost(3,2) =0 + 5 + cost(3)=22
Repeat: Continue from the matrix of 1:3 (with row 1, col 3 and cell [2,1] & [3,1]
are infinity.
Get the matrix value from 3 to n: M[3,n], we start from M[3,2]
Set row 3 and column n to ∞ Why [2,1] need to set
Set M[n,3] to ∞ (no revisit of similar edge) as infinity too?
Because we do not
Reduce the matrix if the row/column contains no zero want the edge 1-3-2
travel back from 2-1.
Cost = M[3,n] + reduction cost + cost(parent node)
Example 3 ~ TSP