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

Slides:



Advertisements
Similar presentations
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Advertisements

C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title : Overview of Stack.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
 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.
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.
The N-Queens Problem lab01.
CS 206 Introduction to Computer Science II 03 / 06 / 2009 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 10 / 17 / 2008 Instructor: Michael Eckmann.
Stacks (Revised and expanded from CIT 591). What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on.
CSC212 Data Structure - Section FG Lecture 12 Stacks and Queues Instructor: Zhigang Zhu Department of Computer Science City College of New York.
Chapter 6 Stacks. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2 Chapter Objectives Examine stack processing Define a stack abstract.
Objectives of these slides:
Data Structures Using C++ 2E Chapter 7 Stacks. Data Structures Using C++ 2E2 Objectives Learn about stacks Examine various stack operations Learn how.
Data Structure Dr. Mohamed Khafagy. Stacks Stack: what is it? ADT Applications Implementation(s)
Fundamentals of Python: From First Programs Through Data Structures Chapter 14 Linear Collections: Stacks.
Stack Applications.
Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
Data Structures Week 4 Our First Data Structure The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
 STACK STACK  BASIC STACK OPERATIONS BASIC STACK OPERATIONS  PUSH ALGORITHM PUSH ALGORITHM  POP ALGORITHM POP ALGORITHM  EVALUATING A POSTFIX EXPRESSION.
Back Tracking Project Due August 11, 1999 N-queens: –A classic puzzle for chess buffs is the N- Queens problem. Simply stated: is it possible to place.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
Advanced Higher Computing Science Stacks Queues and linked lists.
CHP-3 STACKS.
Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure.
Prof. I. J. Chung Data Structure #5 Professor I. J. Chung.
CC 215 DATA STRUCTURES MORE ABOUT STACK APPLICATIONS Dr. Manal Helal - Fall 2014 Lecture 6 AASTMT Engineering and Technology College 1.
Stacks Chapter 3 Objectives Upon completion you will be able to
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
1 Data Structures CSCI 132, Spring 2016 Notes_ 5 Stacks.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
ADT Stack What Is a Stack? Standard Template Library Stack Class
Recursion Data Structure Submitted By:- Dheeraj Kataria.
Data Structures Using C++ 2E
Review Array Array Elements Accessing array elements
CSC212 Data Structure - Section AB
Using a Stack Chapter 6 introduces the stack data type.
Stacks Chapter 5.
MEMORY REPRESENTATION OF STACKS
Data Structures and Algorithms
Stacks.
Stacks Chapter 7 introduces the stack data type.
Queues Queues Queues.
STACKS.
Visit for more Learning Resources
Stack.
Stack Data Structure, Reverse Polish Notation, Homework 7
Using a Stack Chapter 6 introduces the stack data type.
CMSC 341 Lecture 5 Stacks, Queues
Recursion Copyright (c) Pearson All rights reserved.
STACK By:- Rajendra ShakyawalP.G.T. Computer Science KV-No.1, AFS, Tambaram, Chennai.
More About Stacks: Stack Applications
Using a Stack Chapter 6 introduces the stack data type.
Stacks.
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
Data Structures and Algorithms for Information Processing
Using a Stack Chapter 6 introduces the stack data type.
Stack Applications Lecture 29 Thu, Apr 5, /23/2019
Using a Stack Chapter 6 introduces the stack data type.
Stacks Data structure Elements added, removed from one end only
Stacks.
Topic 15 Implementing and Using Stacks
More About Stacks: Stack Applications
Presented by : Aman Gupta PGT CS KV No.1, Narimedu, Madurai
LINEAR DATA STRUCTURES
Lecture 9: Stack and Queue
Presentation transcript:

 Chapter 7 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 Stack Data Structures and Other Objects Using C++

Stacks  Stack – data structure of ordered entries where entries are inserted & removed at only one end (called the top). – Last In First Out (LIFO)  STL has template stack class  push  pop  top

Stack Errors  Stack underflow  Attempting to access element from empty stack  Stack overflow  Attempting to push element on full stack

Stack Applications  Reversing a word

Stack Applications  Balanced parentheses

Stack Applications  Evaluation of arithmetic expressions

Stack Applications  Reversing a word  Balanced parentheses  Evaluation of arithmetic expressions  Evaluating postfix expressions  Translating infix to postfix notation  Implementation of recursive algorithms  N-queens problem

The N-Queens Problem  Suppose you have 8 chess queens... ...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 We will write a program which tries to find a way to place N queens on an N x N chess board.

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:  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 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  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-Queens  repeat these steps  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  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-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-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.

Stack Class Implementations  Implement stack operations in terms of operations of underlying data structure  Examples  Array: insert and delete at the end  Linked list: insert and delete at the front since deleting at the end is slow

 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