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.

Slides:



Advertisements
Similar presentations
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
Advertisements

Topic 9 The Queue ADT.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
CHAPTER 7 Queues.
Queues CS 3358 – Data Structures. What is a queue? It is an ordered group of homogeneous items of elements. Queues have two ends: – Elements are added.
Fundamentals of Python: From First Programs Through Data Structures
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Chapter 7 Queues. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine queue processing Define a queue abstract.
Data Structure Dr. Mohamed Khafagy.
Queue Overview Queue ADT Basic operations of queue
Jerry Lebowitz.  Stacks  Queues 3 C++ Programming: From Problem Analysis to Program Design, Sixth Edition.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
DATA STRUCTURE & ALGORITHMS
Data Structures Chapter 3 Queues Andreas Savva. 2 Queues A data structure modeled after a line of people waiting to be served. A data structure modeled.
Queues The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
Comp 245 Data Structures Queues. Introduction to the Queue ADT It is a FIFO (first-in, first-out) structure Access to the Queue can take place at two.
COSC2006 Chapter 8 Queues III
© 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.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Chapter 7 Queues. © 2005 Pearson Addison-Wesley. All rights reserved7-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
Chapter 18: Stacks and Queues
Chapter 18: Stacks and Queues
1 Queues (Walls & Mirrors - Chapter 7). 2 Overview The ADT Queue Linked-List Implementation of a Queue Array Implementation of a Queue.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Chapter 17: Stacks and Queues
Chapter 7 Queues. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 7-2 Chapter Objectives Examine queue processing Define a queue abstract.
CS 1031 Queues Definition of a Queue Examples of Queues Design of a Queue Class Different Implementations of the Queue Class.
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues (slightly modified by Dan Fleck)
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 2b. Simple Containers: The Queue.
TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues.
Data Structures Using C++ 2E Chapter 8 Queues. Data Structures Using C++ 2E2 Objectives Learn about queues Examine various queue operations Learn how.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 3)
Computer Science 112 Fundamentals of Programming II Modeling and Simulation.
Chapter 18: Stacks and Queues
CSC 205 Programming II Lecture 22 Carwash Simulation.
1 Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Data Structures Using Java1 Chapter 7 Queues. Data Structures Using Java2 Chapter Objectives Learn about queues Examine various queue operations Learn.
Data Structures Using C++
Chapter 8 Queue I CS Data Structures I COSC2006 April 24, 2017
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Scis.regis.edu ● CS-362: Data Structures Week 8 Dr. Jesús Borrego 1.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 5: Queues Java Software Structures: Designing and Using Data.
The Abstract Data Type Queue A queue New items enter at the back, or rear, of the queue Items leave from the front of the queue First-in, first-out (FIFO)
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 13: Queues Announcements.
Chapter 8 Queues. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 The Abstract Data Type Queue A queue –New items enter at the back, or rear, of.
Kruse/Ryba ch031 Object Oriented Data Structures Queues Implementations of Queues Circular Implementation of Queues.
Kruse/Ryba ch031 Object Oriented Data Structures Queues Implementations of Queues Circular Implementation of Queues.
CE 221 Data Structures and Algorithms
Chapter 17: Stacks and Queues. Objectives In this chapter, you will: – Learn about stacks – Examine various stack operations – Learn how to implement.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Give Eg:? Queues. Introduction DEFINITION: A Queue is an ordered collection of element in which insertions are made at one end and deletions are made.
1 Data Structures CSCI 132, Spring 2014 Lecture 7 Queues.
Circular Queues Maitrayee Mukerji. Queues First In – First Out (FIFO) The first element to be inserted is the first one to be retrieved Insertion at one.
The Queue ADT.
Review Array Array Elements Accessing array elements
Data Structures Using C++ 2E
Queues Chapter 4.
Queues Queue Concept Queue Design Considerations
Queue.
Queues Queues Queues.
Queues Chapter 4.
CSCE 3110 Data Structures & Algorithm Analysis
CE 221 Data Structures and Algorithms
Queues Definition of a Queue Examples of Queues
CSC 248 – fundamentals of Data structure
CSCS-200 Data Structure and Algorithms
Data Structures Using C++ 2E
Getting queues right … finally (?)
Presentation transcript:

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 end, called the front or first First In First Out (FIFO) Operations common to previous data structures: –initializeQueue: initializes the queue to an empty state –destroyQueue: removes all the elements from the queue, leaving the queue empty –isEmptyQueue: checks whether the queue is empty--if the queue is empty, it returns the value true; otherwise, it returns the value false –isFullQueue: checks whether the queue is full--if the queue is full, it returns the value true; otherwise, it returns the value false

3 New Operations front: returns the front (first) element of the queue back: returns the rear (last) element of the queue addQueue: adds a new element to the rear of the queue; the queue must exist and must not be full deleteQueue: removes the front element of the queue

4 Array Implementation list: the array of elements queueFront: the first (front) element queueRear: the last (rear) element Dequeue: –retrieve the first element via front –remove it via deleteQueue Enqueue (traditional term): –addQueue to the rear

5 A Queue ABCDE 4 What are the elements in the queue? What is returned by front()? What is returned by back()? What is the effect of addQueue('X')? What is the effect of deleteQueue()? 3

6 An Empty Queue Note: when the first element is added, it will be put in slot 0. To add an element: –increment queueRear –put the new element in list[queueRear]

7 Adding to a Queue

8 Deleting from a Queue What now?

9 Conceptually: a Circular Queue Idea: reuse slots that have been vacated by deleteQueue What do we have that allows us to wraparound, going from 99+1 to 0?

10

11 Queues as Circular Arrays What happens if we add an element 'z'?

12 Queues as Circular Arrays It looks the same as an empty queue Solutions: 1. maintain a count of the number of elements 2. let queueFront be the position preceding the first element and leave it empty Choose option #1

13 private: { int maxQueueSize; int count; int queueFront; int queueRear; Type *list } [maxQueueSize-1] Implementation

14 Initialize Queue template void queueType ::initializeQueue() { queueFront = 0; queueRear = maxQueueSize - 1; count = 0; } Note: queueRear does not point to an element when the first element is added, it will be put in slot 0

15 What about a linkedQueue? queueFront queueRear private: { nodeType queueFront; nodeType queueRear; } How would addQueue(50) work? deleteQueue?

16 Application of Queues: Simulation Major application of queues Build a computer model and "test" different policies Text example: the manager of a local movie theater wants to know whether to go from 1 cashier to 2 What are the considerations? –the cost of the cashier –how much an extra cashier reduces average wait time What happens when a customer arrives? –she gets in line –waits for the next available server –is served –goes into the theater

17 One Server

18 Two Servers with Two Queues

19 Three Servers with a Single Queue

20 Movie Theater Simulation What do we want to track? how many customers were served the sum of their waiting times Assumptions when the first customer arrives, all servers are free when a customer arrives, if there is a free server, she moves to it, otherwise gets in the wait queue when a customer is done, the first person in line moves to the free server What do we need to know # servers transaction time for each customer average time between customers (arrival rate) (assume customers arrive according to a Poisson distribution)

21 Movie Theater Simulation Time-driven simulation run for a number of clock periods (e.g minute periods) track the number of customers served and their cumulative waiting time When a customer starts being served: the time she waited is added to the cumulative waiting time the server initializes its remaining transaction time Each period the simulation increments the clock decrements the remaining transaction time for each server updates the waiting time for each customer in line checks to see if anyone arrived checks to see if there is a free server

22 Procedural Bones //set simulationTime, numberOfServers, transactionTime, timeBetweenCustomerArrival //process simulationTime periods for(clock = 1; clock <= simulationTime; clock++) { //update remaining server transaction time //update the waiting time for each customer //check to see if a customer arrives //check for a free server } //print results How would we think of this from an object-oriented perspective?

23 Object-Oriented Approach: what pieces (objects) do we need to build?

24 Object-Oriented Approach Objects (classes) –customer –server –server list –customer queue What are their properties and methods? Customer properties –id –when she arrived –how long her transaction will take –how long she has waited in line

25 Object-Oriented Approach serverType properties –whether free or busy –the current customer –remaining transaction time serverTypeList properties –how many servers –the list

26 Movie Theater Simulation

27 Movie Theater Simulation

28 waitingCustomerQueueType A queue of customers (suppose clock= 12) queueRear queueFront What happens with a clock tick? cust#: 5: arrT: 9 transT: 6 waitT: 3 cust#: 4 arrT: 7 transT: 5 waitT: 5 cust#: 3 arrT: 5 transT: 5 waitT: 7 queueRear queueFront Is it different from the normal queue? Its type is customertype Additional operation: update the waiting time cust#: 5: arrT: 9 transT: 6 waitT: 3 4 cust#: 4 arrT: 7 transT: 5 waitT: 5 6 cust#: 3 arrT: 5 transT: 5 waitT: 7 8

29 waitingCustomerQueueType Is it different from the normal queue? Its type is customerType Additional operation: update the waiting time (How) can we use the existing queueType? Inheritance!! What functions will need to be added? updateWaitingQueue to add one to each person's waiting time What functions will be different? the constructor

30 waitingCustomerQueueType

31 updateWaitingQueue() How can we add one to the waiting time for each customer? Clumsy: –dequeue each person and add one to her waiting time –add the updated customer to the end of the queue Problem: how do we know when to stop –add a dummy customer to the end before you start –stop when you reach her

32 updateWaitingQueue() Before: queueRear queueFront cust#: 5: arrT: 9 transT: 6 waitT: 3 cust#: 4 arrT: 7 transT: 5 waitT: 5 cust#: 3 arrT: 5 transT: 5 waitT: 7 cust#: 5: arrT: 9 transT: 6 waitT: 3 cust#: 4 arrT: 7 transT: 5 waitT: 5 cust#: 3 arrT: 5 transT: 5 waitT: 7 cust#: -1 arrT: 0 transT: 0 waitT: 0 (add one to waitT)

33 updateWaitingQueue() void waitingCustomerQueueType::updateWaitingQueue() { customerType cust; cust.setWaitingTime(-1); int wTime = 0; addQueue(cust); while(wTime != -1) { cust = front(); deleteQueue(); wTime = cust.getWaitingTime(); if(wTime == -1) break; cust.incrementWaitingTime(); addQueue(cust); } } Can you think of a better way?

34 Poisson Distribution A good way to model events that occur "on the average" E.g. suppose a bus arrives once every 10 minutes The probability of a bus arrives in 1 minute is ~1/10 If λ is the probability an event occurs in an interval, the probability that it occurs y times is P(y) = λ y e - λ /y! Busses Probability 0 (0.1) 0 e -0.1 /0! =.9 1 (0.1) 1 e -0.1 /1! =.09 2 (0.1) 2 e -0.1 /2! =.0045

35 Poisson Distribution Using this to generate arrivals for the movie theater –generate a number between 0 and 1 –see if it is > e - λ (the probability of 0 arrivals) no arrival arrival |_________________________| 0 e - λ 1 bool isCustomerArrived(double arvTimeDiff) { double value; value = static_cast (rand()) / static_cast (RAND_MAX); return (value > exp(- 1.0/arvTimeDiff)); } static_cast ? converts the expression to double