Академический Документы
Профессиональный Документы
Культура Документы
Backtracking
Using Backtracking
Large instances of difficult combinatorial problems can be solved Worst case complexity of Backtracking can be exponential
If not, the path is abandoned and another path taken The process is repeated until the solution is arrived at
Kumar CSE5311
N-Queens problem
Place n-queens on an n n chess board so that no two queens attack each other.
A queen can attack another if the latter is on the same row, column or diagonal
Q1 Q2 Q3 Q4
Kumar
CSE5311
1
Q
Kumar
CSE5311
1
Q
2
Q Q
Kumar
CSE5311
1
Q
2
Q Q Q
Kumar
CSE5311
1
Q
2
Q Q Q
Kumar
CSE5311
0 5 1
Q Q
2
Q Q Q
3
Q Q
6
Q
7 4
Q Q Q Q Q Q
8
Q Q Q
Kumar
CSE5311
Start at a vertex and visit all the other vertices in the graph exactly once and return to the start vertex
Kumar
CSE5311
d e
Kumar
CSE5311
1 2 3
b f
6
d e
10
7 4 5
d e f
11 12 13
d f a Kumar CSE5311
Given a Set S ={s1,s2, Sn} and a posiitive integer d find a subset of the given set S such that the sum of the positive integers in the subset is equal to d. Let S = {3,7,9,13,26,41}; d = 51. Note the list should be sorted.
Kumar
CSE5311
Subset problem
w3 w7 w9 w13 w26
Let S = {3,7,9,13,26,41}; d = 51
w/o 3
10
19 19
w26 w/o 26
32
w/o 26
19
w41
58
32
w41
45
w41
73
w/o 41
60
19
Kumar
CSE5311
Subset problem
w3 w7 w9 w13 w26
Let S = {3,7,9,13,26,41}; d = 51
w/o 3
3 3
w9
10
19 19
w26 w/o 26
32
w/o 26
19
w41
12
w13
58
32
w41
45
w41
12
w/o 26
25 19
w26
73
w/o 41
60
51
Kumar CSE5311
With backtracking
The search space is can be very large It is an exhaustive search Worst case complexity is exponential Limits the search space Through an estimate of the
Kumar
CSE5311
Scheduling problem
The problem of assigning n people to n jobs such that the total cost is as small as possible
Job Person A B C D
J1 9 6 5 7
J2 2 4 8 6
J3 7 3 1 9
J4 8 7 8 4
Kumar
CSE5311
Find a Lower Bound on the cost of the solution The lower bound is only an estimate
Job Person A B C D
J1 9 6 5 7
J2 2 4 8 6
J3 7 3 1 9
J4 8 7 8 4
Kumar
CSE5311
1
AJ1 LB= 9+3+1+4 = 17
2
AJ2 LB= 2+3+1+4 = 10
3
AJ3 LB= 7+4+5+4 = 20
4
AJ4 LB= 8+3+1+6 = 18
5
BJ1 LB= 2+6+1+4 = 13
6
BJ3 LB= 2+3+5+4 = 14
7
BJ4 LB= 2+7+1+7 = 17 Job Person A B C J1 9 6 5 7 J2 2 4 8 6 J3 7 3 1 9 J4 8 7 8 4
8
CJ3, DJ4 2+6+1+4 = 13
9
CJ4, DJ3 2+6+8+9 = 25
Kumar
CSE5311 D
1
AJ1 LB= 9+3+1+4 = 17
2
AJ2 LB= 2+3+1+4 = 10
3
AJ3 LB= 7+4+5+4 = 20
4
AJ4 LB= 8+3+1+6 = 18
J1 9 6 5 7
J2 2 4 8 6
J3 7 3 1 9
J4 8 7 8 4
1
AJ1 LB= 9+3+1+4 = 17
2
AJ2 LB= 2+3+1+4 = 10
3
AJ3 LB= 7+4+5+4 = 20
4
AJ4 LB= 8+3+1+6 = 10
5
BJ1 LB= 2+6+1+4 = 13
6
BJ3 LB= 2+3+5+4 = 14
7
BJ4 LB= 2+7+1+7 = 17 Job Person A B C J1 9 6 5 7 J2 2 4 8 6 J3 7 3 1 9 J4 8 7 8 4
Kumar
CSE5311 D
1
AJ1 LB= 9+3+1+4 = 17
2
AJ2 LB= 2+3+1+4 = 10
3
AJ3 LB= 7+4+5+4 = 20
4
AJ4 LB= 8+3+1+6 = 10
5
BJ1 LB= 2+6+1+4 = 13
6
BJ3 LB= 2+3+5+4 = 14
7
BJ4 LB= 2+7+1+7 = 10 Job Person A B C J1 9 6 5 7 J2 2 4 8 6 J3 7 3 1 9 J4 8 7 8 4
8
CJ3, DJ4 2+6+1+4 = 13
9
CJ4, DJ3 2+6+8+9 = 25
Kumar
CSE5311 D
Knapsack Problem
We wish to maximize the profit in the knapsack Maximization Use Upper bound UB = v+ (W-wi)(vi+1/wi+1) When we start v = 0 Wi - cummulative weights
W = 10
Item Weight Value Value/ weight 10
$40
$42
$25
$12
Kumar
CSE5311
a 5 1 c 2
3 6
7 4 3 e d
Kumar
CSE5311
Kumar
CSE5311