Backtracking

Backtracking Backtracking - Start

2017-03-20 89K 89 0 0

Download Presentation

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.



Presentations text content in Backtracking

Slide1

Backtracking

Slide2

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

Slide3

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

Slide4

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

Slide5

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

Slide6

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

Slide7

4-Queens

Q

QQ

QxQ

Q

x

xQ

QxxQQ

QxxQxQ

Slide8

4-Queens

QxxQxxQ

QxxQxxxQ

We are stuck!

Slide9

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

Slide10

4-Queens

QxxQxxxQ

QxxxQ

QxxxQQ

Q

x

xxQxQ

QxxxQxQQ

QxxxQxQxQ

Slide11

4-Queens

QxxxQxQxxQ

QxxxQxQxxxQ

QxxxQxxQ

Q

x

xxQxxxQ

QxxxQ

xQ

Slide12

4-Queens

xQ

xQQ

xQxQ

x

Q

xxQ

xQxxxQ

xQxxxQQ

Slide13

4-Queens

xQxxxQQQ

xQxxxQQxQ

xQxxxQQxxQ

Slide14

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

Slide15

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

Slide16

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

Slide17

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

Slide18

Graph 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

Slide19

Graph Coloring

C

A

F

B

E

D

Slide20

Graph Coloring

C

A

F

B

E

D

Slide21

Graph Coloring

C

A

F

B

E

D

Slide22

Graph Coloring

C

A

F

B

E

D

Slide23

Graph Coloring

C

A

F

B

E

D

Slide24

Graph Coloring

C

A

F

B

E

D

Stuck!

Slide25

Graph Coloring

C

A

F

B

E

D

Slide26

Graph Coloring

C

A

F

B

E

D

Slide27

Graph Coloring

C

A

F

B

E

D

Slide28

Graph Coloring

C

A

F

B

E

D

Slide29

Graph Coloring

X

X

A

B

C

D

E

F

X

X

X

X

X

X

X

X

C

A

F

B

E

D

Slide30

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?

Slide31

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)

Slide32

Depth vs. Breadth First Search

A

B

H

K

C

D

G

I

J

L

M

P

E

F

N

O

Slide33

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?

Slide34

Depth vs. Breadth First Search

A

B

C

D

E

F

G

H

I

J

K

L

M

N

O

P

Slide35

Practice Problems

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


About DocSlides
DocSlides allows users to easily upload and share presentations, PDF documents, and images.Share your documents with the world , watch,share and upload any time you want. How can you benefit from using DocSlides? DocSlides consists documents from individuals and organizations on topics ranging from technology and business to travel, health, and education. Find and search for what interests you, and learn from people and more. You can also download DocSlides to read or reference later.