Вы находитесь на странице: 1из 24

Backtracking and Branch and Bound

Backtracking

Using Backtracking

Large instances of difficult combinatorial problems can be solved Worst case complexity of Backtracking can be exponential

Typically, a path is taken to check if a solution can be reached

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

Hamiltonian Circuit Problem


a a c d e b f c b

Start at a vertex and visit all the other vertices in the graph exactly once and return to the start vertex

Kumar

CSE5311

Hamiltonian Circuit Problem


a a c d e b f c b

d e

Kumar

CSE5311

Hamiltonian Circuit Problem


a a c d e b f

1 2 3

b f

6
d e

10

7 4 5
d e f

11 12 13

d f a Kumar CSE5311

Subset Sum Problem

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

Branch and Bound

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

Branch and bound technique


Upper bound or Lower bound

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

Branch and Bound

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

This is only an estimate The LB may not be a legitimate solution

In this case, consider the lowest cost from each row


2 +3+1+4 =10 This is our LB

Kumar

CSE5311

Start LB= 2+3+1+4 = 10

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

Start LB= 2+3+1+4 = 10

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

Job Person A B C Kumar CSE5311 D

J1 9 6 5 7

J2 2 4 8 6

J3 7 3 1 9

J4 8 7 8 4

Start LB= 2+3+1+4 = 10

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

Start LB= 2+3+1+4 = 10

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

Traveling Salesperson Problem

LB = (distance to two nearest cities)/2 over all cities

a 5 1 c 2

3 6

7 4 3 e d

Kumar

CSE5311

Kumar

CSE5311

Вам также может понравиться