stacks

30
Chapter 7 introduces Chapter 7 introduces the the stack stack data type. data type. Several example Several example applications of stacks applications of stacks are given in that are given in that chapter. chapter. This presentation shows This presentation shows another use called another use called backtracking to solve backtracking to solve the N-Queens problem the N-Queens problem . . Using a Stack Data Structures and Other Objects Using C++

Upload: amishadalal

Post on 28-Nov-2015

8 views

Category:

Documents


1 download

DESCRIPTION

stack

TRANSCRIPT

Page 1: Stacks

Chapter 7 introduces the Chapter 7 introduces the stackstack data type.data type.

Several example Several example applications of stacks are applications of stacks are given in that chapter. given in that chapter.

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

Using a Stack

Data Structuresand Other ObjectsUsing C++

Page 2: Stacks

The N-Queens Problem

Suppose you have 8 Suppose you have 8 chess queens...chess queens...

...and a chess board...and a chess board

Page 3: Stacks

The N-Queens Problem

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

??

Page 4: Stacks

The N-Queens Problem

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

Page 5: Stacks

The N-Queens Problem

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

Page 6: Stacks

The N-Queens Problem

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

Page 7: Stacks

The N-Queens Problem

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

N QueensN ro

ws

N columns

Page 8: Stacks

The N-Queens Problem

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

Page 9: Stacks

How the program works

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

Page 10: Stacks

How the program works

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

ROW 1, COL 1

Page 11: Stacks

How the program works

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

ROW 1, COL 1

1 filled

Page 12: Stacks

How the program works

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

1 filled

ROW 2, COL 1

Page 13: Stacks

How the program works

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

ROW 1, COL 1

1 filled

ROW 2, COL 2

Page 14: Stacks

How the program works

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

ROW 1, COL 1

1 filled

ROW 2, COL 3

Page 15: Stacks

How the program works

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

ROW 1, COL 1

2 filled

ROW 2, COL 3

Page 16: Stacks

How the program works

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

ROW 1, COL 1

2 filled

ROW 2, COL 3

ROW 3, COL 1

Page 17: Stacks

How the program works

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

ROW 1, COL 1

2 filled

ROW 2, COL 3

ROW 3, COL 2

Page 18: Stacks

How the program works

...and we shift to ...and we shift to the third column.the third column.

Yet another Yet another conflict arises...conflict arises...

ROW 1, COL 1

2 filled

ROW 2, COL 3

ROW 3, COL 3

Page 19: Stacks

How the program works

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

ROW 1, COL 1

2 filled

ROW 2, COL 3

ROW 3, COL 4

Page 20: Stacks

How the program works

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

ROW 1, COL 1

2 filled

ROW 2, COL 3

ROW 3, COL 4

Page 21: Stacks

How the program works

When we run out of When we run out of

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

working working on the previous on the previous row.row.

ROW 1, COL 1

1 filled

ROW 2, COL 3

Page 22: Stacks

How the program works

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

ROW 1, COL 1

1 filled

ROW 2, COL 4

Page 23: Stacks

How the program works

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

ROW 1, COL 1

2 filled

ROW 2, COL 4

Page 24: Stacks

How the program works

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

ROW 1, COL 1

2 filled

ROW 2, COL 4

ROW 3, COL 1

Page 25: Stacks

Pseudocode for N-Queens

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

Place the first queen, pushing its position onto the Place the first queen, pushing its position onto the

stack and setting stack and setting filledfilled to 0 to 0.. repeat these stepsrepeat these steps

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

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

to shift the current queen rightward...to shift the current queen rightward...

Page 26: Stacks

Pseudocode for N-Queens

repeat these stepsrepeat these steps if there are no conflicts with the queens...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.

Page 27: Stacks

Pseudocode for N-Queens

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

shift the current queen rightward...shift the current queen rightward...

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

to indicate the new position.

Page 28: Stacks

Pseudocode for N-Queens

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

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

to shift the current queen rightward...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.

Page 29: Stacks

Pseudocode for N-Queens

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

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

to shift the current queen rightward...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.

Page 30: Stacks

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

backtrackingbacktracking.. The key to backtracking: Each choice is recorded The key to backtracking: Each choice is recorded

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

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

Summary