# Backtracking

### Presentations text content in Backtracking

Backtracking

Slide2N-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

Slide3N-Queens

1

2

3

4

5

6

7

8

1

Q

2

Q

3

Q

4

Q

5

Q

6

Q

7

Q

8

Q

Slide44-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

Slide54-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

Slide64-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

Slide74-Queens

Q

QxQ

Q

x

xQ

QxxQQ

QxxQxQ

Slide84-Queens

QxxQxxQ

QxxQxxxQ

We are stuck!

Slide94-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

Slide104-Queens

QxxQxxxQ

QxxxQ

QxxxQQ

Q

x

xxQxQ

QxxxQxQQ

QxxxQxQxQ

Slide114-Queens

QxxxQxQxxQ

QxxxQxQxxxQ

QxxxQxxQ

Q

x

xxQxxxQ

QxxxQ

xQ

Slide124-Queens

xQ

xQQ

xQxQ

x

Q

xxQ

xQxxxQ

xQxxxQQ

Slide134-Queens

xQxxxQQQ

xQxxxQQxQ

xQxxxQQxxQ

Slide144-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

Slide154-Queens

1

1

2

3

4

2

X

X

1

2

3

4

X

X

X

X

1

2

4

3

X

X

1

2

4

3

X

X

X

X

X

1

2

3

4

X

X

X

1

1

2

3

X

X

Slide16Graph 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

Slide17Graph 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

Slide18Graph Coloring

C

A

F

B

E

D

As an example:

The vertices are enumerated in order A-F

The colors are given in order: R, G, B

Slide19Graph Coloring

C

A

F

B

E

D

Slide20Graph Coloring

C

A

F

B

E

D

Slide21Graph Coloring

C

A

F

B

E

D

Slide22Graph Coloring

C

A

F

B

E

D

Slide23Graph Coloring

C

A

F

B

E

D

Slide24Graph Coloring

C

A

F

B

E

D

Stuck!

Slide25Graph Coloring

C

A

F

B

E

D

Slide26Graph Coloring

C

A

F

B

E

D

Slide27Graph Coloring

C

A

F

B

E

D

Slide28Graph Coloring

C

A

F

B

E

D

Slide29Graph Coloring

X

X

A

B

C

D

E

F

X

X

X

X

X

X

X

X

C

A

F

B

E

D

Slide30Depth-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?

Slide31Backtracking 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)

Slide32Depth vs. Breadth First Search

A

B

H

K

C

D

G

I

J

L

M

P

E

F

N

O

Slide33Depth 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?

Slide34Depth vs. Breadth First Search

A

B

C

D

E

F

G

H

I

J

K

L

M

N

O

P

Slide35Practice Problems

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

## Backtracking

Download Presentation - The PPT/PDF document "Backtracking" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.