35
Backtracking

Backtracking. N-Queens The object is to place queens on a chess board in such a way as no queen can capture another one in a single move –Recall that

Embed Size (px)

Citation preview

Page 1: Backtracking. N-Queens The object is to place queens on a chess board in such a way as no queen can capture another one in a single move –Recall that

Backtracking

Page 2: Backtracking. N-Queens The object is to place queens on a chess board in such a way as no queen can capture another one in a single move –Recall that

N-Queens

• The object is to place queens on a chess board in such a way as no queen can capture another one in a single move– Recall that a queen can move horizontally,

vertically, or diagonally an infinite distance• This implies that no two queens can be on the same

row, column, or diagonal– We usually want to know how many different

placements there are

Page 3: Backtracking. N-Queens The object is to place queens on a chess board in such a way as no queen can capture another one in a single move –Recall that

N-Queens1 2 3 4 5 6 7 8

1 Q

2 Q

3 Q

4 Q

5 Q

6 Q

7 Q

8 Q

Page 4: Backtracking. N-Queens The object is to place queens on a chess board in such a way as no queen can capture another one in a single move –Recall that

4-Queens

• Lets take a look at the simple problem of placing 4 queens on a 4x4 board

• The brute-force solution is to place the first queen, then the second, third, and forth– After all are placed we determine if they are placed

legally• There are 16 spots for the first queen, 15 for the

second, etc.– Leading to 16*15*14*13 = 43,680 different combinations

• Obviously this isn’t a good way to solve the problem

Page 5: Backtracking. N-Queens The object is to place queens on a chess board in such a way as no queen can capture another one in a single move –Recall that

4-Queens

• First lets use the fact that no two queens can be in the same row to help us– That means we get to place a queen in each row

• So we can place the first queen into the first row, the second into the second, etc.

• This cuts down on the amount of work– Now there are 4 spots for the first queen, 4 spots for the

second, etc.• 4*4*4*4 = 256 different combinations

Page 6: Backtracking. N-Queens The object is to place queens on a chess board in such a way as no queen can capture another one in a single move –Recall that

4-Queens

• However, we can still do better because as we place each queen we can look at the previous queens we have placed to make sure our new queen is not in the same column or diagonal as a previously placed queen

• Then we could use a Greedy-like strategy to select the next valid position for each row

Page 7: Backtracking. N-Queens The object is to place queens on a chess board in such a way as no queen can capture another one in a single move –Recall that

4-QueensQ Q

QQx Q

Qx x Q

Qx x QQ

Qx x Qx Q

Page 8: Backtracking. N-Queens The object is to place queens on a chess board in such a way as no queen can capture another one in a single move –Recall that

4-QueensQx x Qx x Q

Qx x Qx x x Q

Page 9: Backtracking. N-Queens The object is to place queens on a chess board in such a way as no queen can capture another one in a single move –Recall that

4-Queens

• So now what do we do?• Well, this is very much like solving a maze

– As you walk through the maze you have to make a series of choices

– If one of your choices leads to a dead end, you need to back up to the last choice you made and take a different route• Change one of your earlier selections

– Eventually you will find your way out of the maze

Page 10: Backtracking. N-Queens The object is to place queens on a chess board in such a way as no queen can capture another one in a single move –Recall that

4-QueensQx x Qx x x Q

Qx x x Q

Qx x x QQ

Qx x x Qx Q

Qx x x Qx QQ

Qx x x Qx Qx Q

Page 11: Backtracking. N-Queens The object is to place queens on a chess board in such a way as no queen can capture another one in a single move –Recall that

4-QueensQx x x Qx Qx x Q

Qx x x Qx Qx x x Q

Qx x x Qx x Q

Qx x x Qx x x Q

Qx x x Q

x Q

Page 12: Backtracking. N-Queens The object is to place queens on a chess board in such a way as no queen can capture another one in a single move –Recall that

4-Queensx Q x Q

Qx Qx Q

x Qx x Q

x Qx x x Q

x Qx x x QQ

Page 13: Backtracking. N-Queens The object is to place queens on a chess board in such a way as no queen can capture another one in a single move –Recall that

4-Queensx Qx x x QQQ

x Qx x x QQx Q

x Qx x x QQx x Q

Page 14: Backtracking. N-Queens The object is to place queens on a chess board in such a way as no queen can capture another one in a single move –Recall that

4-Queens

• This type of problem is often viewed as a state-space tree– A tree of all the states that the problem can be in

• We start with an empty board state at the root and try to work our way down to a leaf node– Leaf nodes are completed boards

Page 15: Backtracking. N-Queens The object is to place queens on a chess board in such a way as no queen can capture another one in a single move –Recall that

4-Queens

1

1 2 3 4

2

1 2 3 4 1 2 43

1 2 43

1 2 3 4

1

1 2 3

Page 16: Backtracking. N-Queens The object is to place queens on a chess board in such a way as no queen can capture another one in a single move –Recall that

Graph Coloring

• Graph coloring is the problem of coloring each vertex in a graph such that no two adjacent vertices are the same color

• Some direct examples:– Map coloring– Register assignment

Page 17: Backtracking. N-Queens The object is to place queens on a chess board in such a way as no queen can capture another one in a single move –Recall that

Graph Coloring

• The same issues apply as in N-Queens– We don’t want to simply pick all subsets

• Way too many

– We want to prune the state-space tree as soon as we find something that won’t work• This implies that we need a sequence of vertices to color• As we color the next vertex we need to make sure it doesn’t conflict

with any of its previously colored neighbors

– We may need to backtrack

Page 18: Backtracking. N-Queens The object is to place queens on a chess board in such a way as no queen can capture another one in a single move –Recall that

Graph Coloring

C

A

FB

E

D

• As an example:– The vertices are enumerated

in order A-F– The colors are given in

order: R, G, B

Page 19: Backtracking. N-Queens The object is to place queens on a chess board in such a way as no queen can capture another one in a single move –Recall that

Graph Coloring

C

A

FB

E

D

Page 20: Backtracking. N-Queens The object is to place queens on a chess board in such a way as no queen can capture another one in a single move –Recall that

Graph Coloring

C

A

FB

E

D

Page 21: Backtracking. N-Queens The object is to place queens on a chess board in such a way as no queen can capture another one in a single move –Recall that

Graph Coloring

C

A

FB

E

D

Page 22: Backtracking. N-Queens The object is to place queens on a chess board in such a way as no queen can capture another one in a single move –Recall that

Graph Coloring

C

A

FB

E

D

Page 23: Backtracking. N-Queens The object is to place queens on a chess board in such a way as no queen can capture another one in a single move –Recall that

Graph Coloring

C

A

FB

E

D

Page 24: Backtracking. N-Queens The object is to place queens on a chess board in such a way as no queen can capture another one in a single move –Recall that

Graph Coloring

C

A

FB

E

D

Page 25: Backtracking. N-Queens The object is to place queens on a chess board in such a way as no queen can capture another one in a single move –Recall that

Graph Coloring

C

A

FB

E

D

Page 26: Backtracking. N-Queens The object is to place queens on a chess board in such a way as no queen can capture another one in a single move –Recall that

Graph Coloring

C

A

FB

E

D

Page 27: Backtracking. N-Queens The object is to place queens on a chess board in such a way as no queen can capture another one in a single move –Recall that

Graph Coloring

C

A

FB

E

D

Page 28: Backtracking. N-Queens The object is to place queens on a chess board in such a way as no queen can capture another one in a single move –Recall that

Graph Coloring

C

A

FB

E

D

Page 29: Backtracking. N-Queens The object is to place queens on a chess board in such a way as no queen can capture another one in a single move –Recall that

Graph Coloring

C

A

FB

E

D

Page 30: Backtracking. N-Queens The object is to place queens on a chess board in such a way as no queen can capture another one in a single move –Recall that

Depth-First Search

• One can view backtracking as a Depth-First Search through the State-Space– We start our search at the root– We end our search when we find a valid leaf node (or

when we explore the entire space)• A Depth-First Search expands the deepest node next

– A child will be expanded before the child’s siblings are expanded

• This type of search can get you to the leaf nodes as fast as possible

• How do you implement a Depth-First Search?

Page 31: Backtracking. N-Queens The object is to place queens on a chess board in such a way as no queen can capture another one in a single move –Recall that

Backtracking Framework

• We will try and build a generic backtracking framework that works with problems States– All it should need to work with is States

• State should be generic and not married to any specific problem

• States should be able to:– Tell us if it has more children– Produce the next child– Tell us if it is solved– Tell us if it is feasible

• (In class design of the State, NQueens problem specific, and Backtracking framework classes)

Page 32: Backtracking. N-Queens The object is to place queens on a chess board in such a way as no queen can capture another one in a single move –Recall that

Depth vs. Breadth First Search

A

B H K

C D G I J L M P

E F N O

Page 33: Backtracking. N-Queens The object is to place queens on a chess board in such a way as no queen can capture another one in a single move –Recall that

Depth vs. Breadth First Search

• A Breadth first search expands all the children at a particular level before advancing to the next level– How can a breadth first search be implemented?– Which is better: a depth or breadth first search?

• Worst case Big-O?

Page 34: Backtracking. N-Queens The object is to place queens on a chess board in such a way as no queen can capture another one in a single move –Recall that

Depth vs. Breadth First Search

A

B C D

E F G H I J K L

M N O P

Page 35: Backtracking. N-Queens The object is to place queens on a chess board in such a way as no queen can capture another one in a single move –Recall that

Practice Problems

Foundations of Algorithms

Chapter 5: 1, 3, 7, 11, 13, 16, 30, 31