Briana B. Morrison Adapted from Alan Eugenio

Slides:



Advertisements
Similar presentations
Chapter 6 Queues and Deques.
Advertisements

© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
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.
ADT Queue 1. What is a Queue? 2. STL Queue 3. Array Implementation of Queue 4. Linked List Implementation of Queue 5. Priority Queue.
Queues Briana B. Morrison Adapted from Alan Eugenio.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 The Stack ADT (§4.2) The Stack ADT stores arbitrary objects Insertions and deletions.
Main Index Contents 11 Main Index Contents Model for a Queue Model for a Queue The Queue The Queue ADTQueue ADT (3 slides) Queue ADT Radix Sort Radix Sort.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Fall 2007CS 2251 Queues Chapter 6. Fall 2007CS 2252 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in.
Main Index Contents 11 Main Index Contents Container Types Container Types Sequence Containers Sequence Containers Associative Containers Associative Containers.
Fall 2007CS 2251 Queues Chapter 6. Fall 2007CS 2252 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in.
Implementing and Using Stacks
Priority Queues Briana B. Morrison Adapted from Alan Eugenio Sell100IBM$122 Sell300IBM$120 Buy500IBM$119 Buy400IBM$118.
Circular queue. Array-based Queue Use an array of size N in a circular fashion Three variables keep track of the front, rear, and size f index of the.
© 2004 Goodrich, Tamassia Queues1. © 2004 Goodrich, Tamassia Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions.
Introduction to Data Structure, Fall 2006 Slide- 1 California State University, Fresno Introduction to Data Structure Chapter 8 Ming Li Department 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 and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.
Fall 2007CS 2251 Queues Chapter 6. Fall 2007CS 2252 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in.
Queues by Dr. Bun Yue Professor of Computer Science CSCI 3333 Data Structures.
Data Structures Using Java1 Chapter 7 Queues. Data Structures Using Java2 Chapter Objectives Learn about queues Examine various queue operations Learn.
Adapted from Data Structures with C++ using STL: Ford, Topp CS 362: Queues Dr. Nazli Mollah Overview of Lecture  Introduction  The Queue ADT  The Radix.
Queues CSCI 3333 Data Structures. Acknowledgement  Dr. Bun Yue  Mr. Charles Moen  Dr. Wei Ding  Ms. Krishani Abeysekera  Dr. Michael Goodrich  Dr.
Computer Science Department Data Structures and Algorithms Queues Lecture 5.
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
Welcome to CSCE 221 – Data Structures and Algorithms
1 Queues Queue API Application: Radix Sort Implementation: Using Deque Using Deque Circular Array Circular Array Priority Queue Priority Queue API Implementation.
Queue. The Queue ADT Insertions and deletions follow the first-in first-out scheme Insertions are at the rear of the queue and removals are at the front.
CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow.
Queues1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich,
Elementary Data Structures
Review Array Array Elements Accessing array elements
Cpt S 122 – Data Structures Abstract Data Types
Queues 5/11/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
CC 215 Data Structures Queue ADT
Week 4 - Friday CS221.
Queues.
Queues Rem Collier Room A1.02
Queues Queues Queues.
Algorithms and Data Structures
Wednesday, February 28, 2018 Announcements… For Today…
Chapter 6 – Queues and Deques
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
CMSC 341 Lecture 5 Stacks, Queues
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Queues 11/9/2018 6:32 PM Queues.
Queues 11/16/2018 4:18 AM Queues 11/16/2018 4:18 AM Queues.
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Circular queue.
Queue.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Queues 12/3/2018 Queues © 2014 Goodrich, Tamassia, Goldwasser Queues.
Queues 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser,
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Stacks and Queues DSA 2013 Stacks n Queues.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Queues Jyh-Shing Roger Jang (張智星)
Copyright © Aiman Hanna All rights reserved
QUEUE Visit for more Learning Resources Free Powerpoint Templates.
Using a Queue Chapter 8 introduces the queue data type.
Using a Queue Chapter 8 introduces the queue data type.
Stacks, Queues, and Deques
CSCS-200 Data Structure and Algorithms
Stacks and Linked Lists
Data Structures & Programming
Presentation transcript:

Briana B. Morrison Adapted from Alan Eugenio Queues Briana B. Morrison Adapted from Alan Eugenio

Topics Define Queue APIs Applications Implementation Deques Radix Sort Simulation Implementation Array based Circular Empty, one value, full Linked list based Deques Priority Queues Queues

Queues

The Queue A Queue is a FIFO (First in First Out) Data Structure. Elements are inserted in the Rear of the queue and are removed at the Front. Queues

Queues

Queues

Return a reference to the value of the item at the font of the queue. CLASS queue Constructor <queue> queue(); Create an empty queue. CLASS queue Operations <queue> bool empty() const; Check whether the queue is empty. Return true if it is empty and false otherwise. T& front(); Return a reference to the value of the item at the font of the queue. Precondition: The queue is not empty. Queues

const T& front() const; Constant version of front(). CLASS queue Operations <queue> const T& front() const; Constant version of front(). void pop(); Remove the item from the front of the queue. Precondition: The queue is not empty. Postcondition: The element at the front of the queue is the element that was added immediately after the element just popped or the queue is empty. Queues

void push(const T& item); CLASS queue Operations <queue> void push(const T& item); Insert the argument item at the back of the queue. Postcondition: The queue has a new item at the back int size() const; Return the number of elements in the queue. Queues

Queues

DETERMINE THE OUTPUT FROM THE FOLLOWING: queue<int> my_queue; for (int i = 0; i < 10; i++) my_queue.push (i * i); while (!my_queue.empty()) { cout << my_queue.front() << endl; my_queue.pop(); } // while Queues

Queues

Queues

NOT OK: NO pop_front METHOD deque? list? vector? OK NOT OK: NO pop_front METHOD Queues

Implementing Queue: adapter of std::list This is a simple adapter class, with following mappings: Queue push maps to push_back Queue front maps front Queue pop maps to pop_front ... This is the approach taken by the C++ standard library. Any sequential container that supports push_back, front, and pop_front can be used. The list The deque Queues

Queues

Queues

Queues

Queues

Applications of Queues Direct applications Waiting lists, bureaucracy Access to shared resources (e.g., printer) Multiprogramming Indirect applications Auxiliary data structure for algorithms Component of other data structures Queues

Queues

The Radix Sort Order ten 2 digit numbers in 10 bins from smallest number to largest number. Requires 2 calls to the sort Algorithm. Initial Sequence: 91 6 85 15 92 35 30 22 39 Pass 0: Distribute the cards into bins according to the 1's digit (100). Queues

The Radix Sort After Collection: 30 91 92 22 85 15 35 6 39 Pass 1: Take the new sequence and distribute the cards into bins determined by the 10's digit (101). Final Sequence: 6 15 22 30 35 39 85 91 92 Queues

Radix Sort Use an array of queues (or vector of queues) as the “buckets” void radixSort (vector<int>& v, int d) { int i; int power = 1; queue<int> digitQueue[10]; for (i=0;i < d;i++) distribute(v, digitQueue, power); collect(digitQueue, v); power *= 10; } Queues

// support function for radixSort() // distribute vector elements into one of 10 queues // using the digit corresponding to power // power = 1 ==> 1's digit // power = 10 ==> 10's digit // power = 100 ==> 100's digit // ... void distribute(const vector<int>& v, queue<int> digitQueue[], int power) { int i; // loop through the vector, inserting each element into // the queue (v[i] / power) % 10 for (i = 0; i < v.size(); i++) digitQueue[(v[i] / power) % 10].push(v[i]); } Queues

// support function for radixSort() // gather elements from the queues and copy back to the vector void collect(queue<int> digitQueue[], vector<int>& v) { int i = 0, digit; // scan the vector of queues using indices 0, 1, 2, etc. for (digit = 0; digit < 10; digit++) // collect items until queue empty and copy items back // to the vector while (!digitQueue[digit].empty()) v[i] = digitQueue[digit].front(); digitQueue[digit].pop(); i++; } Queues

Queues

A SYSTEM IS A COLLECTION OF INTERACTING PARTS. A MODEL IS A SIMPLIFICATION OF A SYSTEM. THE PURPOSE OF BUILDING A MODEL IS TO STUDY THE UNDERLYING SYSTEM. Queues

Queues

Queues

Queues

Queues

Queues

Queues

Simulating Waiting Lines Using Queues Simulation is used to study the performance: Of a physical (“real”) system By using a physical, mathematical, or computer model of the system Simulation allows designers to estimate performance Before building a system Simulation can lead to design improvements Giving better expected performance of the system Queues

Simulating Waiting Lines Using Queues Simulation is particular useful when: Building/changing the system is expensive Changing the system later may be dangerous Often use computer models to simulate “real” systems Airline check-in counter, for example Special branch of mathematics for these problems: Queuing Theory Queues

Simulate Strategies for Airline Check-In Queues

Simulate Airline Check-In We will maintain a simulated clock Counts in integer “ticks”, from 0 At each tick, one or more events can happen: Frequent flyer (FF) passenger arrives in line Regular (R) passenger arrives in line Agent finishes, then serves next FF passenger Agent finishes, then serves next R passenger Agent is idle (both lines empty) Queues

Simulate Airline Check-In Simulation uses some parameters: Max # FF served between regular passengers Arrival rate of FF passengers Arrival rate of R passengers Service time Desired output: Statistics on waiting times, agent idle time, etc. Optionally, a detailed trace Queues

Simulate Airline Check-In Design approach: Agent data type models airline agent Passenger data type models passengers 2 queue<Passenger>, 1 for FF, 1 for R Overall Airline_Checkin_Sim class Queues

Simulate Airline Check-In Queues

Queues

Queues

Queues

Queues

Queues

Queues

Queues

Queues

Queues

Implementing a Queue Array based Where is front? Where is top? Array suffers from “rightward” drift To solve, use circular array How are elements added, removed? Using circular array, a new problem arises What does empty look like? What does single element look like? What does full look like? Queues

wrapped-around configuration Array-based Queue Use an array of size N in a circular fashion Two variables keep track of the front and rear f index of the front element r index immediately past the rear element Array location r is kept empty normal configuration Q 1 2 r f wrapped-around configuration Q 1 2 f r Queues

Implementing queue With a Circular Array Basic idea: Maintain two integer indices into an array front: index of first element in the queue rear: index of the last element in the queue Elements thus fall at front through rear Key innovation: If you hit the end of the array wrap around to slot 0 This prevents our needing to shift elements around Still have to deal with overflow of space Queues

Implementing Queue With Circular Array Queues

Implementing Queue With Circular Array Queues

Implementing Queue With Circular Array Queues

The Bounded queue Queues

Methods to Implement i = (( i + 1) == max) ? 0 : (i + 1); if (( i + 1) == max) i = 0; else i = i + 1; i = ( i + 1) % max; Queues

Queue Operations We use the modulo operator (remainder of division) Algorithm size() return (N - f + r) mod N Algorithm isEmpty() return (f = r) Q 1 2 r f Q 1 2 f r Queues

Queue Operations (cont.) Algorithm enqueue(o) if size() = N  1 then throw FullQueueException else Q[r]  o r  (r + 1) mod N Operation enqueue throws an exception if the array is full This exception is implementation-dependent Q 1 2 r f Q 1 2 f r Queues

Queue Operations (cont.) Algorithm dequeue() if isEmpty() then throw EmptyQueueException else o  Q[f] f  (f + 1) mod N return o Operation dequeue throws an exception if the queue is empty This exception is specified in the queue ADT Q 1 2 r f Q 1 2 f r Queues

Boundary Conditions Queues

Implementation Considerations The physical model: a linear array with the front always in the first position and all entries moved up the array whenever the front is deleted. A linear array with two indices always increasing. A circular array with front and rear indices and one position left vacant. A circular array with front and rear indices and a Boolean flag to indicate fullness (or emptiness). A circular array with front and rear indices and an integer counter of entries. A circular array with front and rear indices taking special values to indicate emptiness. Queues

Growable Array-based Queue In an enqueue operation, when the array is full, instead of throwing an exception, we can replace the array with a larger one Similar to what we did for an array-based stack The enqueue operation has amortized running time O(n) with the incremental strategy O(1) with the doubling strategy Queues

Implementing a Queue Linked List based Efficiency of operations Where is front? Where is back? How are elements added, removed? Efficiency of operations Queues

Queue with a Singly Linked List We can implement a queue with a singly linked list The front element is stored at the first node The rear element is stored at the last node The space used is O(n) and each operation of the Queue ADT takes O(1) time r nodes f  elements Queues

Implementing Queue: Singly-Linked List This requires front and rear Node pointers: template<typename Item_Type> class queue { . . . private: // Insert implementation-specific data fields // Insert definition of Node here #include "Node.h" // Data fields Node* front_of_queue; Node* back_of_queue; size_t num_items; }; Queues

Using a Single-Linked List to Implement a Queue (continued) Queues

Implementing Queue: Singly-Linked List Insert at tail, using back_of_queue for speed Remove using front_of_queue Adjust size when adding/removing No need to iterate through to determine size Queues

Analysis of the Space/Time Issues Time efficiency of singly- or doubly-linked list good: O(1) for all Queue operations Space cost: ~3 extra words per item vector uses 1 word per item when fully packed 2 words per item when just grown On average ~1.5 words per item, for larger lists Queues

Comparing the Three Implementations All three are comparable in time: O(1) operations Linked-lists require more storage Singly-linked list: ~3 extra words / element Doubly-linked list: ~4 extra words / element Circular array: 0-1 extra word / element On average, ~0.5 extra word / element Queues

Analysis of the Space/Time Issues vector Implementation Insertion at end of vector is O(1), on average Removal from the front is linear time: O(n) Removal from rear of vector is O(1) Insertion at the front is linear time: O(n) Queues

Queues

Queues

Queues

The deque The deque is an abstract data type that combines the features of a stack and a queue. The name deque is an abbreviation for double-ended queue. The C++ standard defines the deque as a full-fledged sequential container that supports random access. Queues

The deque class Queues

The deque class (2) Queues

The deque class (3) Queues

Queues

Queues

What’s output? Queues

Queues

Queues

The Standard Library Implementation The standard library uses a randomly accessible circular array. Each item in the circular array points to a fixed size, dynamically allocated array that contains the data. The advantage of this implementation is that when reallocation is required, only the pointers need to be copied into the new circular array. Queues

The Standard Library Implementation (2) Queues

Queues

Queues

Queues

Queues

Queues

Queues

Queues

Queues

Queues

Queues

Queues

Queues

Priority Queue A Special form of queue from which items are removed according to their designated priority and not the order in which they entered. Items entered the queue in sequential order but will be removed in the order #2, #1, #4, #3. Queues

Remove the item of highest priority from the queue. CLASS priority_queue Constructor <queue> priority_queue(); Create an empty priority queue. Type T must implement the operator <. CLASS priority_queue Operations <queue> bool empty() const; Check whether the priority queue is empty. Return true if it is empty, and false otherwise. Create void pop(); Remove the item of highest priority from the queue. Precondition: The priority queue is not empty. Postcondition: The priority queue has 1 less element Queues

void push(const T& item); CLASS priority_queue Operations <queue> void push(const T& item); Insert the argument item into the priority queue. Postcondition: The priority queue contains a new element. int size() const; Return the number of items in the priority queue. T& top(); Return a reference to the item having the highest priority. Precondition: The priority queue is not empty. const T& top(); Constant version of top(). Queues

PQ Implementation How would you implement a priority queue? Queues

§- Queue Summary Slide 1 - A first-come-first-served data structure. §- Insertion operations (push()) occur at the back of the sequence §- deletion operations (pop()) occur at the front of the sequence. 103103 Queues

§- The radix sort algorithm Summary Slide 2 §- The radix sort algorithm - Orders an integer vector by using queues (bins). - This sorting technique has running time O(n) but has only specialized applications. - The more general in-place O(n log2n) sorting algorithms are preferable in most cases. 104104 Queues

§- Implementing a queue with a fixed-size array Summary Slide 3 §- Implementing a queue with a fixed-size array - Indices qfront and qback move circularly around the array. - Gives O(1) time push() and pop() operations with no wasted space in the array. 105105 Queues

§- Priority queue Summary Slide 4 - Pop() returns the highest priority item (largest or smallest). - Normally implemented by a heap, which is discussed later in the class. - The push() and pop() operations have running time O(log2n) 106106 Queues