Queues Queue Concept Queue Design Considerations

Slides:



Advertisements
Similar presentations
Ceng-112 Data Structures I Chapter 5 Queues.
Advertisements

Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
1 Queues (Continued) Applications for a queue Linked queue implementation Array queue implementation Circular array queue implementation Reading L&C 3.
Topic 9 The Queue ADT.
CHAPTER 7 Queues.
Fundamentals of Python: From First Programs Through Data Structures
Chapter 7 Queues. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine queue processing Define a queue abstract.
Queues Ellen Walker CPSC 201 Data Structures Hiram College.
CHAPTER 4 Queues MIDTERM THURSDAY, OCTOBER 17 IN LAB.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
Chapter 14 Queues. Chapter Scope Queue processing Using queues to solve problems Various queue implementations Comparing queue implementations Java Foundations,
Queues Chapter 6. Chapter Objectives  To learn how to represent a waiting line (queue) and how to use the methods in the Queue interface for insertion.
ITEC200 Week06 Queues. 2 Learning Objectives – Week06 Queues (Ch6) Students can Manage data using the queue Abstract Data Type.
CHAPTER 4 Queues. Chapter Objectives  To learn how to represent a waiting line (queue) and how to use the methods in the Queue interface for insertion.
© 2006 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 Queues CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
1 Queues Queue Concept Queue Design Considerations Queues in Java Collections APIs Queue Applications Reading L&C , 9.3.
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 Chapter 6. Chapter 6: Queues2 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in the Queue interface.
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.
TCSS 342, Winter 2005 Lecture Notes
Queues. From last time: Java's built-in java.util.Stack class Consider the javadoc for Java’s built-in java.util.Stack class (
Queues What is a Queue? Queue Implementations: Queue As Array
Stacks, Queues, and Deques
Chapter 6.6, (event-driven simulation) Queues 1CSCI 3333 Data Structures.
Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome.
Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract.
COMP 121 Week 14: Queues. Objectives Learn how to represent a queue Learn how to use the methods in the Queue interface Understand how to implement the.
Exam 1 –Monday June 25 th –open Book / Open Notes –No Electronic Devices (calculators, laptops, etc) –Room Number: W –Time: 5:30pm to 8:00pm.
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues (slightly modified by Dan Fleck)
CHAPTER 4 Queues. Chapter Objectives  To learn how to represent a waiting line (queue) and how to use the methods in the Queue interface for insertion.
Information and Computer Sciences University of Hawaii, Manoa
1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other.
COS 312 DAY 20 Tony Gauvin. Ch 1 -2 Agenda Questions? Capstone Progress reports over due Assignment 6 Posted – Due April 16 – Questions? Queues.
CSC 205 Programming II Lecture 22 Carwash Simulation.
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 Chapter 6. Chapter 6: Queues Chapter Objectives To learn how to represent a waiting line (queue) and how to use the five methods in the Queue interface:
LECTURE 26: QUEUES CSC 212 – Data Structures. Using Stack.
Data Structures Using Java1 Chapter 7 Queues. Data Structures Using Java2 Chapter Objectives Learn about queues Examine various queue operations Learn.
Lecture7: Queue Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Chapter Objectives  Learn how to represent a waiting line (queue)  Become proficient using the methods in the Queue  Understand how to implement the.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 5: Queues Java Software Structures: Designing and Using Data.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
Queues 1. Queue  a queue represents a sequence of elements where elements can be added at the back of the sequence and removed from the front of the.
1 Queues (Continued) Queue ADT Linked queue implementation Array queue implementation Circular array queue implementation Deque Reading L&C , 9.3.
Java Collections Framework The client view. The Big Picture.
The Queue ADT.
Stacks Stack Abstract Data Type (ADT) Stack ADT Interface
Queues Chapter 8 (continued)
Review Array Array Elements Accessing array elements
Week 4 - Monday CS221.
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.
Using Queues: Coded Messages
Queues Implementations 6/17/2018.
COS 312 DAY 21 Tony Gauvin.
CSE 373: Data Structures and Algorithms
Chapter 5 Queues.
THURSDAY, OCTOBER 17 IN LAB
Stacks and Queues.
Java Collections Framework
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.
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Queues What is a Queue? Queue Implementations: As Array
CE 221 Data Structures and Algorithms
Stacks, Queues, and Deques
Stacks and Queues.
Queues.
Data Structures & Programming
Presentation transcript:

Queues Queue Concept Queue Design Considerations Queues in Java Collections APIs Queue Applications Reading L&C 5.1-5.8, 9.3

Queue Abstract Data Type A queue is a linear collection where the elements are added to one end and removed from the other end The processing is first in, first out (FIFO) The first element put on the queue is the first element removed from the queue Think of a line of people waiting for a bus (The British call that “queuing up”)

A Conceptual View of a Queue Rear of Queue (or Tail) Front of Queue (or Head) Removing an Element Adding an Element

Queue Terminology We enqueue an element on a queue to add one We dequeue an element off a queue to remove one We can also examine the first element without removing it We can determine if a queue is empty or not and how many elements it contains (its size) The L&C QueueADT interface supports the above operations and some typical class operations such as toString()

Queue Design Considerations Although a queue can be empty, there is no concept for it being full. An implementation must be designed to manage storage space For first and dequeue operation on an empty queue, this implementation will throw an exception Other implementations could return a value null that is equivalent to “nothing to return”

The java.util.Queue Interface The java.util.Queue interface is in the Java Collections API (extends Collection) However, it is only an interface and you must use an implementing class LinkedList is the most commonly used implementing class For a queue of type objects: Queue<type> myQueue = new LinkedList<type>();

The java.util.Queue Interface The names of the methods are different Enqueue is done using: boolean offer(T element) // returns false if full Dequeue is done using either: T poll() // returns null value if empty T remove() // throws an exception if empty Peek is done using either: T peek() // returns null value if empty T element() // throws an exception if empty

Applications for a Queue A queue can be used as an underlying mechanism for many common applications Cycling through a set of elements in order Simulation of client-server operations Radix Sort Scheduling processes in an operating system such as printer queues

Cycling through Code Keys The Caesar cipher is simple letter shifting Each letter is treated as its number 0-25 in the alphabet and each letter is encoded as: cipher value = (letter value + constant) % 26 The message is decoded letter by letter: letter value = (cipher value – constant) % 26 if (letter value < 0) letter value += 26 Using the constant 7, the word “queue” would be coded as “xblbl” Note: the word’s “pattern” is recognizable

Cycling through Code Keys The Caesar cipher is easy to solve because there are only 26 possible “keys” to try It can be made harder by cycling through a key set of values such as 3, 1, 7, 4, 2, 5 We put that sequence of numbers in a queue As we encode each letter, we dequeue a number for the constant and re-enqueue it - cycling through the entire key set as many times as needed for the message length

Cycling through Code Keys Using that queue of numbers as the constant values, the word “queue” becomes “tvlyg” Note: the word’s “pattern” is not recognizable If we are encoding a message containing the entire Unicode character set, we can omit the modulo 26 operator as in the text book code See L&C, Listing 5.1

Ticket Counter Simulation See L&C Listing 5.2 and 5.3 The simulation in this example sets up a queue with each customer arriving at regular 15 second intervals This is not a very meaningful analysis because it doesn’t take into account the typical variations in arrival rates E.G. One customer every 15 seconds could mean 8 customers arriving at one time and then 2 minutes with no arriving customers

Ticket Counter Simulation Textbook code always gives same values: cashiers time 1 5317 2 2325 3 1332 4 840 5 547 6 355 7 219 8 120 9 120 10 120

Ticket Counter Simulation A more sophisticated simulation would use probability distributions for the arrival rate and for the processing time With an average serving time that sets a maximum capacity for handling customers based on the number of servers And an average arrival time with parameters for the distribution of arrivals over time A statistical analysis is more important for an ice cream shop next to a movie theater (during a movie versus as a movie lets out)

Ticket Counter Simulation Poisson Distribution is commonly used for estimating arrival times in simulations Lambda is the average number of arrivals per time interval P(X=k) is the probability that k is the number of arrivals during this rv this time interval

Ticket Counter Simulation Replacement for textbook code in listing 5.3: /** load customer queue improved to use random Poisson arrival times*/ Poisson myDist = new Poisson(lambda); for (int count=0; count < NUM_INTERVALS / lambda; count++) { int numberOfCustomers = myDist.getValue(); for (int i = 0; i < numberOfCustomers; i++) customerQueue.offer(new Customer(count*15*lambda)); } Introduces random arrival times based on the Poisson distribution for each time interval

Ticket Counter Simulation For a Markov/Markov/1 (M/M/1) process (one queue and one server), the expected waiting time can be calculated in closed form Waiting Time = Service Time / (1 – load/capacity) This produces a graph of waiting time versus load/capacity with infinite waiting time at load equal to 100% or more of capacity

Ticket Counter Simulation

Radix Sort - IBM Card Sorter

Radix Sort - Algorithm See L&C Listing 9.3 Like the old IBM punched card sorters Original List 1. Sort into Queues by Radix * * * Q10 Q0 Q1 2. Empty Each Queue in Order and Add to List