P Chapter 6 introduces the stack data type. p Several example applications of stacks are given in that chapter. p This presentation shows another use called.

Slides:



Advertisements
Similar presentations
Chapter 5 introduces the often- used data structure of linked lists. This presentation shows how to implement the most common operations on linked lists.
Advertisements

CSC211 Data Structures Lecture 9 Linked Lists Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach to the.
Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.
Container Classes A container class is a data type that is capable of holding a collection of items. In C++, container classes can be implemented as.
L l One of the tree applications in Chapter 9 is binary search trees. l l In Chapter 9, binary search trees are used to implement bags and sets. l l This.
 One of the tree applications in Chapter 10 is binary search trees.  In Chapter 10, binary search trees are used to implement bags and sets.  This presentation.
CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.
 Chapter 11 has several programming projects, including a project that uses heaps.  This presentation shows you what a heap is, and demonstrates two.
CSC212 Data Structure - Section AB Lecture 20 Hashing Instructor: Edgardo Molina Department of Computer Science City College of New York.
 Chapter 7 introduces the stack data type.  Several example applications of stacks are given in that chapter.  This presentation shows another use called.
Stacks CS 3358 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
The N-Queens Problem lab01.
  Chapter 12 presents several common algorithms for sorting an array of integers.   Two slow but simple algorithms are Selectionsort and Insertionsort.
P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates.
CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York.
 Chapter 10 introduces trees.  This presentation illustrates the simplest kind of trees: Complete Binary Trees. Complete Binary Trees Data Structures.
Copyright © 1999, Carnegie Mellon. All Rights Reserved. Chapter 12 presents several common algorithms for sorting an array of integers. Two slow but simple.
L l Chapter 11 discusses several ways of storing information in an array, and later searching for the information. l l Hash tables are a common approach.
 An important topic: preconditions and postconditions.  They are a method of specifying what a function accomplishes. Preconditions and Postconditions.
L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked.
 Chapter 9 introduces the technique of recursive programming.  As you have seen, recursive programming involves spotting smaller occurrences of a problem.
P Chapter 2 introduces Object Oriented Programming. p OOP is a relatively new approach to programming which supports the creation of new data types and.
CSC212 Data Structure - Section FG Lecture 12 Stacks and Queues Instructor: Zhigang Zhu Department of Computer Science City College of New York.
 Chapter 8 introduces the queue data type.  Several example applications of queues are given in that chapter.  This presentation describes the queue.
Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)
P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Preconditions and Postconditions.
 A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with.
P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Illinois State University Department.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 5: Recursion as a Problem-Solving Technique Data Abstraction.
CSC211 Data Structures Lecture 20 Hashing Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
 An important topic: preconditions and postconditions.  They are a method of specifying what a method accomplishes. Preconditions and Postconditions.
Software Development p Data structure p A collection of data organized so that the data can be accessed using a set of specific techniques p Object-oriented.
CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
CSC211 Data Structures Lecture 13 Recursive Thinking Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
CSC211 Data Structures Lecture 18 Heaps and Priority Queues Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
P p Chapter 10 introduces trees. p p This presentation illustrates the simplest kind of trees: Complete Binary Trees. Complete Binary Trees Data Structures.
Container Classes  A container class is a data type that is capable of holding a collection of items.  In C++, container classes can be implemented as.
CSC212 Data Structure - Section AB Lecture 13 Recursive Thinking Instructor: Edgardo Molina Department of Computer Science City College of New York.
CSC212 Data Structure Lecture 16 Heaps and Priority Queues Instructor: George Wolberg Department of Computer Science City College of New York.
 Chapter 6 introduces templates, which are a C++ feature that easily permits the reuse of existing code for new purposes.  This presentation shows how.
 Chapter 7 introduces the stack data type.  Several example applications of stacks are given in that chapter.  This presentation shows another use called.
ADT Stack What Is a Stack? Standard Template Library Stack Class
CSC212 Data Structure - Section AB
Using a Stack Chapter 6 introduces the stack data type.
Linked Lists in Action Chapter 5 introduces the often-used data public classure of linked lists. This presentation shows how to implement the most common.
Using a Stack Chapter 6 introduces the stack data type.
Hash Tables Chapter 11 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach.
Recursion Copyright (c) Pearson All rights reserved.
Using a Stack Chapter 6 introduces the stack data type.
Quadratic Sorting Chapter 12 presents several common algorithms for sorting an array of integers. Two slow but simple algorithms are Selectionsort and.
CSC212 Data Structure - Section AB
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Stacks.
Using a Stack Chapter 6 introduces the stack data type.
Binary Search Trees One of the tree applications in Chapter 9 is binary search trees. In Chapter 9, binary search trees are used to implement bags and.
Heaps Chapter 11 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates.
Using a Stack Chapter 6 introduces the stack data type.
Hash Tables Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common.
Hash Tables Chapter 11 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach.
Hash Tables Chapter 11 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach.
Binary Search Trees One of the tree applications in Chapter 9 is binary search trees. In Chapter 9, binary search trees are used to implement bags and.
Using a Queue Chapter 8 introduces the queue data type.
Using a Queue Chapter 8 introduces the queue data type.
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Complete Binary Trees Chapter 9 introduces trees.
Hash Tables Chapter 11 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach.
Presentation transcript:

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

The N-Queens Problem p Suppose you have 8 chess queens... p...and a chess board

The N-Queens Problem Can the queens be placed on the board so that no two queens are attacking each other ?

The N-Queens Problem Two queens are not allowed in the same row...

The N-Queens Problem Two queens are not allowed in the same row, or in the same column...

The 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 Problem The number of queens, and the size of the board can vary. N Queens N rows N columns

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 Generate possible placements and check for their validity until find one solution or all of them. 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 o Note that each row has one queen. o So to find on valid placement for N queens in N rows, one needs to find a valid column for each queen o An improved brutal force algorithm : consider N column positions for each of the N queens: N^N possible combinations o e.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 o Note that each row has one queen. o each column has one queen. o An improved brutal force algorithm : consider all possible N different column positions for the N queens in N rows: N factorial (N!) possible combinations o Assuming each placement takes 1 ms to check, o e.g. 8!= , (8!)/(1000*60) = 0.67 minutes o 10!= , (10!)/(1000*60) = minutes

The N-Queens Problem: Brutal Force Algorithm o In general, the brutal force algorithms for N queens deal with exponential numbers of possibilities and become intractable for some N. o Not many valid placements, compared to the numbers of possibilities n12, unique distinct

The 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 or vga graphics, you can double click on this icon with the left mouse button:

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

How 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 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 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 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 works If another conflict occurs, the queen is shifted rightward again. ROW 1, COL 1 1 filled ROW 2, COL 3

How 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 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 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 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 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 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 works When we run out of room in a row: p pop the stack,  reduce filled by 1 p and continue working on the previous row. ROW 1, COL 1 1 filled ROW 2, COL 3

How 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 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 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-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 p if there are no conflicts with the queens... p else if there is a conflict and there is room to shift the current queen rightward... p else if there is a conflict and there is no room to shift the current queen rightward...

Pseudocode for N-Queens Žrepeat these steps p if there are no conflicts with the queens... Increase filled by 1. If filled is now N, then the algorithm is done. Otherwise, move to the next row and place a queen in the first column.

Pseudocode for N-Queens Žrepeat these steps p if there are no conflicts with the queens... p 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-Queens Žrepeat these steps p if there are no conflicts with the queens... p else if there is a conflict and there is room to shift the current queen rightward... p 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-Queens Žrepeat these steps p if there are no conflicts with the queens... p else if there is a conflict and there is room to shift the current queen rightward... p 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 work You can double click the left mouse button here to run the demonstration program a second time:

p Stacks have many applications. p The application which we have shown is called backtracking. p The key to backtracking: Each choice is recorded in a stack. p When you run out of choices for the current decision, you pop the stack, and continue trying different choices for the previous decision. Summary

T HE E ND Presentation copyright 2012, Pearson Education, For use with Data Structures and Other Objects Using Java by 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 (copyright Corel Corporation, 3G Graphics Inc, Archive Arts, Cartesia Software, Image Club Graphics Inc, One Mile Up Inc, TechPool Studios, Totem Graphics Inc). Students and instructors who use Data Structures and Other Objects Using Java are welcome to use this presentation however they see fit, so long as this copyright notice remains intact.