Stacks, Queues, and Deques

Slides:



Advertisements
Similar presentations
STACKS & QUEUES. Stacks Abstract data types An abstract data type (ADT) is an abstraction of a data structure An ADT specifies : –Data stored –Operations.
Advertisements

Stacks.
Queues1 Part-B2 Queues. Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
Stacks. 2 Outline and Reading The Stack ADT (§4.2.1) Applications of Stacks (§4.2.3) Array-based implementation (§4.2.2) Growable array-based stack.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 The Stack ADT (§4.2) The Stack ADT stores arbitrary objects Insertions and deletions.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Applications of Stacks Direct applications Delimiter matching Undo sequence in a text.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
Unit 11 1 Unit 11: Data Structures H We explore some simple techniques for organizing and managing information H This unit focuses on: Abstract Data Types.
1 CSCD 326 Data Structures I Stacks. 2 Data Type Stack Most basic property: last item in (most recently inserted) is first item out LIFO - last in first.
Stacks.
Queues. … frontrear dequeueenqueue Message queues in an operating system There are times that programs need to communicate with each other.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5) Java.util.Stack class Java.util.Vector.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
Part-B1 Stacks. Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Applications of Stacks (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5)
CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
Stacks1 CS2468 Data Structures and Data Management Lecturer: Lusheng Wang Office: B6422 Phone:
EXPANDING STACKS AND QUEUES CS16: Introduction to Data Structures & Algorithms 1 Tuesday, February 10, 2015.
Abstract Data Types (ADTs) and data structures: terminology and definitions A type is a collection of values. For example, the boolean type consists of.
Stacks. week 2a2 Outline and Reading The Stack ADT (§4.1) Applications of Stacks Array-based implementation (§4.1.2) Growable array-based stack Think.
Stacks. 2 What Are Stacks ? PUSHPOP 0 MAX Underflow Overflow.
Stacks © 2010 Goodrich, Tamassia1Stacks. 2 Abstract Data Types (ADTs)  An abstract data type (ADT) is an abstraction of a data structure  An ADT specifies:
Abstract Data Type (ADT) & Stacks
October 18, Algorithms and Data Structures Lecture V Simonas Šaltenis Nykredit Center for Database Research Aalborg University
SAK 3117 Data Structures Chapter 3: STACKS. Objective To introduce: Stack concepts Stack operations Stack applications CONTENT 3.1 Introduction 3.2 Stack.
Stacks 1. Stack  What is a stack? An ordered list where insertions and deletions occur at one end called the top. Also known as last-in-first-out (LIFO)
Stack. Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations on the data.
Chapter 7 Stacks. © 2004 Pearson Addison-Wesley. All rights reserved 7-2 The Abstract Data Type: Developing an ADT During the Design of a Solution Specifications.
CSC 212 Stacks & Queues. Announcement Many programs not compiled before submission  Several could not be compiled  Several others not tested with javadoc.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
ELEMENTARY DATA STRUCTURES Stacks, Queues, and Linked Lists.
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 3.
CSC 212 – Data Structures Lecture 17: Stacks. Question of the Day Move one matchstick to produce a square.
Lecture6: Stacks Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Stacks Nour El-Kadri CSI Stacks Software stacks are abstract data types (structures) similar to physical stacks, Plates Trays Books PEZ dispenser.
Queue. Avoid confusion Britain Italy 6 Applications of Queues Direct applications –Waiting lists, bureaucracy –Access to shared resources (e.g.,
Parasol Lab, Dept. CSE, Texas A&M University
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
ITI Introduction to Computing II Lab-5 Dewan Tanvir Ahmed University of Ottawa.
Data Structures Evolution of algorithms for an array-based stack, queue, and linked-list. Evolved data structures used to evolve solutions to problems.
CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Stack. ADS2 Lecture 1010 The Stack ADT (GoTa §5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in.
CS 221 Analysis of Algorithms Data Structures. Portions of the following slides are from  Goodrich and Tamassia, Algorithm Design: Foundations, Analysis.
Stacks and Queues MR. Mohammad Alqahtani.
Click to edit Master text styles Stacks Data Structure.
1 Stacks Abstract Data Types (ADTs) Stacks Application to the analysis of a time series Java implementation of a stack Interfaces and exceptions.
Stacks Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Stacks (and Queues).
Stacks II David Lillis School of Computer Science and Informatics
CSCI 3333 Data Structures Stacks.
Stacks.
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Stacks.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Stacks.
Stacks 12/7/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Stacks Abstract Data Types (ADTs) Stacks
Stacks.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Stacks.
CS210- Lecture 3 Jun 6, 2005 Announcements
A type is a collection of values
Presentation transcript:

Stacks, Queues, and Deques - Ed. 2, and 3.: Chapter 4 Ed. 4: Chapter 5

Stacks, Queues, and Deques - Stack ADT – interface - Stack implementation Queues - Queue ADT – interface - Queue implementation Deques - Deque ADT – interface - Deque implementation Sample case study application

Stacks A stack is an interesting data structure with a lot of applications. In such a data structure, we are dealing with a collection of objects. Objects can be added to or removed from the collection, one at a time, as plates in a spring-loaded, cafeteria plate dispenser.

Stacks

The Stack Abstract Data Type An array of integers is not a stack. However, if we enforce the LIFO principle, it becomes a stack. A stack S is an abstract data type (ADT) that supports following two fundamental methods: push(o): Insert object o at the top of the s tack Input : Object; Output : None. pop(): Remove from the stack and return the top object on the stack; an error occurs if the stack is empty. Input : None; Output : Object

Other supporting methods: size(): Return the number of objects in the stack. Input: None; Output: Integer isEmpty(): Return a Boolean indicating if the stack is empty. Input: None; Output: Boolean top(): Return the top object on the stack, without removing it; an error occurs if the stack is empty. Input: None; Output: Object

This table shows a series of stack operations and their effects. The stack is initially empty. 5 Operation Output S push(5) - (5) push(3) (5,3) pop() 3 push(7) (5,7) 7 top() 5 () “error” isEmpty() true push(9) (9) (9,7) (9,7,3) (9,7,3,5) size() 4 push(8) (9,7,3,8) 8 5 3 5 The stack is now empty.

A Stack Interface in Java

A Simple Array-Based Implementation To implement a stack with an array, we need: 1. An array of the given size 2. An index t for the top element (Normally, t is implemented as a variable of type int. Initially, t is set to –1.) S: 1 t N - 1 … …

Algorithms:   size(): return the number t + 1 S: 1 t N - 1 … …

Algorithms:   isEmpty(): return true if t < 0; otherwise, false. S: 1 t N - 1 … … t + 1 = 0 or t = -1 or t < 0

Algorithms:   top(): if the stack is empty throw a StackEmptyException else return element S[t] S: 1 t N - 1 … …

push(o): if stack size is N throw a StackFullException else increase the index t by 1 store the object to S[t] It can be defined in the same way for StackEmptyException. S: … … 1 t t+1 N - 1 push

pop(): if stack is empty throw a StackEmptyException else save the element S[t] to a variable e make the element S[t] a null object decrease the index t by 1 return e S: … … 1 t N - 1 pop

Stacks in the Java Virtual Machine

Sample Case Study Application We want to write a program to calculate the span of the stock’s price on a given day.   The span of the stock’s price on a given day: The maximum number of the consecutive days up to the current day (including the current day) such that the stock price on each of those days has been less than or equal to the price on the current day.

Exemplary data: Price of 7-year bonds: Spans: April 9 April 10 48.97 47.54 45.83 46.34 45.68 46.95 48.17 Spans: April 9 April 10 April 11 April 12 April 13 April 14 April 15 1 2 4 6  

Let us use some symbols to represent the problem: Stock Price:   Stock Price: April 9 April 10 April 11 April 12 April 13 April 14 April 15 p0 p1 p2 p3 p4 p5 p6 April 9 April 10 April 11 April 12 April 13 April 14 April 15 s0 s1 s2 s3 s4 s5 s6 The problem is how to calculate the span of a given day.

Since price quotes start with a given day (day 0, which is April 9 Analysis – property I: Since price quotes start with a given day (day 0, which is April 9 in this example), we have: si  i + 1 Spans: April 9 April 10 April 11 April 12 April 13 April 14 April 15 1 2 4 6 si: i + 1: s0 = 1 0 + 1 s1 = 1 1 + 1 s2 = 1 2 + 1 s3 = 2 3 + 1 s4 = 1 4 + 1 s5 = 4 5 + 1 s6 = 6 6 + 1

Analysis – property II: For a given day i, if the span is 1, it means pi-1 > pi or (The price of day pi-1 is higher than that of i = 0 day pi.) If the span is 2, it means: pi-1  pi and pi-2 > pi . If the span is 3, it means: pi-2  pi , pi-1  pi, and pi-3 > pi. If the span is 4, it means: pi-3  pi , pi-2  pi, pi-1  pi, and pi-4 > pi.

In general, if the span of pi is k, it means: pi-k-1  pi, pi-k-2  pi, … pi-1  pi, and pi-k > pi.

An Algorithm Without Using a Stack Let us consider an algorithm without using a stack.   To find out the span for a given day i: 1. Assign value 0 to a variable k 2. Compare pi-k with pi. 3. If pi-k  pi, increment the value in k and go to step 2. 4. Step 2 and 3 are repeated until pi-k > pi or k > i. The span is the value in the variable k.

To find out the spans for all the days, the procedure has to be repeated with i = 0, 1, …, n - 1. Therefore, the complete algorithm has two loops: One for the days and another is to find the span for a given day. The spans are stored in an array s.   for i = 0 to n -1 assign value 0 to a variable k repeat until pi-k > pi or k > i compare pi-k with pi. if pi-k  pi increment the value in k save the value of k to s[i] i p1 p2 … … pi … … pn-1 k for a certain i s[i]

The running time of this algorithm is proportional to the square of n, the number of days, namely,   running time = O(n2) (We say a quantity D is O(n2) if there exists a real constant c and an integer n0  1 such that D  cn2 for every integer n  n0.) Such an algorithm is called a quadratic-time algorithm. Using a stack, however, we can cut down the running time dramatically.

An Algorithm Using a Stack Note that the span si on a certain day i can be easily computed if we know the closest day preceding day i, such that the price on that day is higher than the price on day i. If such a preceding day exists for a day i, let us denote it with h(i), and otherwise let us define h(i) = -1. Then, si = i – h(i).

si = i – h(i). h(0) -1 h(1) h(2) 1 h(3) 1 h(4) 3 h(5) 1 h(6) s0 s1 s2 s3 s4 s5 s6 1 1 1 2 1 4 6

The problem is how to compute h(i) efficiently? Step 1: p0 = 48.97. h(0) = -1, s0 = 0 - h(0) = 0 – (-1) = 1 Day 0. It is possible that h(1) = 0. Step 2: p1 = 47.54. Pop days with prices less than or equal to p1. At this point of time, we have only one element in the stack. It is 0 and p0 > p1. So h(1) = 0, s1 = 1 - h(1) = 1 – 0 = 1. Day 1. It is possible that h(2) = 1. 1

p2 = 45.83. Pop days with prices less than or equal to p2. Step 3: p2 = 45.83. Pop days with prices less than or equal to p2. At this point of time, we have two elements in the stack. The top one is 1 and p1 > p2. So h(2) = 1, s2 = 2 - h(2) = 2 – 1 = 1. Day 2. It is possible that h(3) = 2. 2 1 Step 4: p3 = 46.34. Pop days with prices less than or equal to p3. The top one will be taken out since p3 > p2. The second one is 1 and p1 > p3. So h(3) = 1, s3 = 3 - h(3) = 3 – 1 = 2. Day 3. It is possible that h(4) = 3. 1 3

p4 = 45.68. Pop days with prices less than or equal to p4. Step 5: p4 = 45.68. Pop days with prices less than or equal to p4. The top one is 3 and p3 > p4. So h(4) = 3, s4 = 4 - h(3) = 4 – 3 = 1. Day 4. It is possible that h(5) = 4. 4 3 1 Step 6: p5 = 46.95. Pop days with prices less than or equal to p3. The top two will be taken out since p5 > p4 and p5 > p3. The third one is 1 and p1 > p5. So h(5) = 1, s5 = 5 - h(5) = 5 – 1 = 4. Day 5. It is possible that h(6) = 5. 1 5

p6 = 48.17. Pop days with prices less than or equal to p3. Step 7: p6 = 48.17. Pop days with prices less than or equal to p3. The top two will be taken out since p6 > p5 and p6 > p1. The third one is 0 and p0 > p6. So h(6) = 0, s6 = 6 - h(6) = 6 – 0 = 6. Day 6. The price on day 6. The process stops. 6

Java Implementation h(0) h(i)

public class DailyHighSpan { … … public static void main(String args[]) { Quote Q[] = new Quote[10]; System.out.println("Input:" + "\n"); for (int i = 0; i < 10; i++) { Q[i] = new Quote(i,(int)(Math.random()*100)); System.out.print(Q[i].getPrice(); ); System.out.print('\t'); //System.out.print("-"); } System.out.println(); try{ computeDailyHighSpan(Q); catch(EmptyStackException e) {}