P Chapter 6 introduces the stack data type. p Several example applications of stacks are given in...

37
Chapter 6 introduces the stack data type. Several example applications of stacks are given in that chapter. This presentation shows another use called backtracking to Using a Stack Data Structures and Other Objects Using Java

Transcript of P Chapter 6 introduces the stack data type. p Several example applications of stacks are given in...

Chapter 6 introduces the stack data type.

Several example applications of stacks are given in that chapter.

This presentation shows another use called backtracking to solve the N-Queens problem.

Using a StackUsing a Stack

Data Structuresand Other ObjectsUsing Java

The N-Queens ProblemThe N-Queens Problem

Suppose you have 8 chess queens... ...and a chess board

The N-Queens ProblemThe N-Queens Problem

Can the queens be placed on the board so that no two queens are attacking each other

?

The N-Queens ProblemThe N-Queens Problem

Two queens are not allowed in the same row...

The N-Queens ProblemThe N-Queens Problem

Two queens are not allowed in the same row, or in the same column...

The N-Queens ProblemThe N-Queens Problem

Two queens are not allowed in the same row, or in the same column, or along the same diagonal.

The N-Queens ProblemThe N-Queens Problem

The number of queens, and the size of the board can vary.

N QueensN ro

ws

N columns

The N-Queens Problem: Observation The N-Queens Problem: Observation

It seems hard to generate one valid placement.

But it is easy to check whether a placement is valid or not.

The N-Queens Problem: Brutal Force Algorithm The N-Queens Problem: Brutal Force Algorithm

Generate possible placements and check for their validity

until find one solution or all of them.

Naïve brutal force: choose N out of N^2 positions -- the numbers of combinations and thus the time complexity of the algorithm grow exponentially in N^2

e.g. 64 choose 8 = 4,426,165,368

Assuming each placement takes 1 ms to check,

(64 choose 8)/(1000*3600*24)=51.23 days

The N-Queens Problem: Brutal Force Algorithm The N-Queens Problem: Brutal Force Algorithm

oNote that each row has one queen. oSo to find on valid placement for N queens in N rows, one needs to find a valid column for each queenoAn improved brutal force algorithm : consider N column positions for each of the N queens: N^N possible combinations

oe.g. 8^8= 16,777,216

Assuming each placement takes 1 ms to check,

(8^8)/(1000*3600) = 4.66 hours

The N-Queens Problem: Brutal Force Algorithm The N-Queens Problem: Brutal Force Algorithm

oNote that each row has one queen. o each column has one queen.oAn improved brutal force algorithm : consider all possible N different column positions for the N queens in N rows: N factorial (N!) possible combinations

oAssuming each placement takes 1 ms to check,oe.g. 8!= 40 320, (8!)/(1000*60) = 0.67 minuteso10!= 3 628 800, (10!)/(1000*60) = 60.48 minutes

The N-Queens Problem: Brutal Force Algorithm The N-Queens Problem: Brutal Force Algorithm

oIn general, the brutal force algorithms for N queens deal with exponential numbers of possibilities and become intractable for some N.

oNot many valid placements, compared to the numbers of possibilities

n 1 2, 3 4 5 6 7 8 9

unique 1 0 1 2 1 6 12 46

distinct 1 0 2 10 4 40 92 352

The N-Queens ProblemThe N-Queens Problem

We will write a program which tries to find a way to place N queens on an N x N chess board.

If you can run ega orvga graphics,you can double click onthis icon with the leftmouse button:

Package

How the program worksHow the program works

The program uses a stack to keep track of where each queen is placed.

How the program worksHow the program works

Each time the program decides to place a queen on the board, the position of the new queen is stored in a record which is placed in the stack.

ROW 1, COL 1

How the program worksHow the program works

We also have an integer variable to keep track of how many rows have been filled so far.

ROW 1, COL 1

1 filled

How the program worksHow the program works

Each time we try to place a new queen in the next row, we start by placing the queen in the first column...

ROW 1, COL 1

1 filled

ROW 2, COL 1

How the program worksHow the program works

...if there is a conflict with another queen, then we shift the new queen to the next column.

ROW 1, COL 1

1 filled

ROW 2, COL 2

How the program worksHow the program works

If another conflict occurs, the queen is shifted rightward again.

ROW 1, COL 1

1 filled

ROW 2, COL 3

How the program worksHow the program works

When there are no conflicts, we stop and add one to the value of filled. ROW 1, COL 1

2 filled

ROW 2, COL 3

How the program worksHow the program works

Let's look at the third row. The first position we try has a conflict... ROW 1, COL 1

2 filled

ROW 2, COL 3

ROW 3, COL 1

How the program worksHow the program works

...so we shift to column 2. But another conflict arises...

ROW 1, COL 1

2 filled

ROW 2, COL 3

ROW 3, COL 2

How the program worksHow the program works

...and we shift to the third column.

Yet another conflict arises... ROW 1, COL 1

2 filled

ROW 2, COL 3

ROW 3, COL 3

How the program worksHow the program works

...and we shift to column 4. There's still a conflict in column 4, so we try to shift rightward again...

ROW 1, COL 1

2 filled

ROW 2, COL 3

ROW 3, COL 4

How the program worksHow the program works

...but there's nowhere else to go.

ROW 1, COL 1

2 filled

ROW 2, COL 3

ROW 3, COL 4

How the program worksHow the program works

When we run out of

room in a row: pop the stack, reduce filled by

1 and continue

working on the previous row.

ROW 1, COL 1

1 filled

ROW 2, COL 3

How the program worksHow the program works

Now we continue working on row 2, shifting the queen to the right. ROW 1, COL 1

1 filled

ROW 2, COL 4

How the program worksHow the program works

This position has no conflicts, so we can increase filled by 1, and move to row 3.

ROW 1, COL 1

2 filled

ROW 2, COL 4

How the program worksHow the program works

In row 3, we start again at the first column.

ROW 1, COL 1

2 filled

ROW 2, COL 4

ROW 3, COL 1

Pseudocode for N-QueensPseudocode for N-Queens

Initialize a stack where we can keep track of our decisions.

Place the first queen, pushing its position onto the stack and setting filled to 0.

repeat these steps if there are no conflicts with the queens... else if there is a conflict and there is room to

shift the current queen rightward... else if there is a conflict and there is no room

to shift the current queen rightward...

Pseudocode for N-QueensPseudocode for N-Queens

repeat these steps if there are no conflicts with the queens...

Increase filled by 1. If filled is now N, thenthe algorithm is done. Otherwise, move to

the next row and place a queen in thefirst column.

Pseudocode for N-QueensPseudocode for N-Queens

repeat these steps if there are no conflicts with the queens... else if there is a conflict and there is room to

shift the current queen rightward...

Move the current queen rightward,adjusting the record on top of the stack

to indicate the new position.

Pseudocode for N-QueensPseudocode for N-Queens

repeat these steps if there are no conflicts with the queens... else if there is a conflict and there is room to

shift the current queen rightward... else if there is a conflict and there is no room

to shift the current queen rightward...

Backtrack!Keep popping the stack, and reducing filled by 1, until you reach a row where the queen

can be shifted rightward. Shift this queen right.

Pseudocode for N-QueensPseudocode for N-Queens

repeat these steps if there are no conflicts with the queens... else if there is a conflict and there is room to

shift the current queen rightward... else if there is a conflict and there is no room

to shift the current queen rightward...

Backtrack!Keep popping the stack, and reducing filled

by 1, until you reach a row where the queen can be shifted rightward. Shift this queen right.

Watching the program workWatching the program work

You can double click the left mouse button here to run the demonstration program a second time:

Package

Stacks have many applications. The application which we have shown is called

backtracking. The key to backtracking: Each choice is recorded

in a stack. When you run out of choices for the current

decision, you pop the stack, and continue trying different choices for the previous decision.

Summary Summary

THE ENDTHE END

Presentation copyright 2012, Pearson Education,For use with Data Structures and Other Objects Using Javaby Michael Main.

Some artwork in the presentation is used with permission from Presentation Task Force(copyright New Vision Technologies Inc) and Corel Gallery Clipart Catalog (copyrightCorel Corporation, 3G Graphics Inc, Archive Arts, Cartesia Software, Image ClubGraphics Inc, One Mile Up Inc, TechPool Studios, Totem Graphics Inc).

Students and instructors who use Data Structures and Other Objects Using Java are welcometo use this presentation however they see fit, so long as this copyright notice remainsintact.