CS2006 - Data Structures I Chapter 7 Queues II. 2 Topics Queue Application Simulation Comparison of List, Stack and Queue.

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

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 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.”
Data Structure Dr. Mohamed Khafagy.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 4.
CS Data Structures II Review COSC 2006 April 14, 2017
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.
Event-drive SimulationCS-2303, C-Term Project #3 – Event-driven Simulation CS-2303 System Programming Concepts (Slides include materials from The.
Simulation. Example: A Bank Simulator We are given: –The number of tellers –The arrival time of each customer –The amount of time each customer requires.
1 6.3 Binary Heap - Other Heap Operations There is no way to find any particular key without a linear scan through the entire heap. However, if we know.
Queues and Priority Queues
COSC2006 Chapter 8 Queues III
Chapter 13 Queues and Priority Queues CS Data Structures Mehmet H Gunes Modified from authors’ slides.
© 2006 Pearson Addison-Wesley. All rights reserved8-1 Chapter 8 Queues CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
Queues, Deques, and Priority Queues Chapter Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes.
Cmpt-225 Queues. A queue is a data structure that only allows items to be inserted at the end and removed from the front Queues are FIFO (First In First.
Queues, Deques, and Priority Queues Chapter Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes.
Simulating Single server queuing models. Consider the following sequence of activities that each customer undergoes: 1.Customer arrives 2.Customer waits.
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.
Simulation Waiting Line. 2 Introduction Definition (informal) A model is a simplified description of an entity (an object, a system of objects) such that.
Queues, Deques, and Priority Queues Chapter Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes.
Lab 01 Fundamentals SE 405 Discrete Event Simulation
Cmpt-225 Simulation. Application: Simulation Simulation  A technique for modeling the behavior of both natural and human-made systems  Goal Generate.
EXPANDING STACKS AND QUEUES CS16: Introduction to Data Structures & Algorithms 1 Tuesday, February 10, 2015.
Queues and Priority Queues
© 2006 Pearson Addison-Wesley. All rights reserved8 A-1 Chapter 8 Queues (slightly modified by Dan Fleck)
TK1924 Program Design & Problem Solving Session 2011/2012 L6: Queues.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
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.
1 QUEUES. 2 Definition A queue is a linear list in which data can only be inserted at one end, called the rear, and deleted from the other end, called.
Data Structures Using C++ 2E Chapter 8 Queues. Data Structures Using C++ 2E2 Objectives Learn about queues Examine various queue operations Learn how.
Waiting Lines and Queuing Models. Queuing Theory  The study of the behavior of waiting lines Importance to business There is a tradeoff between faster.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 3)
CSC 205 Programming II Lecture 22 Carwash Simulation.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
1 Simulation Implementation Using high-level languages.
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)
Data Structures Chapter 6. Data Structure A data structure is a representation of data and the operations allowed on that data. Examples: 1.Array 2.Record.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
© 2011 Pearson Addison-Wesley. All rights reserved 8 B-1 Chapter 8 (continued) Queues.
Chapter 7 Queues Introduction Queue applications Implementations.
Queue Section 3 6/12/ The Abstract Data Type Queue A queue is a list from which items are deleted from one end (front) and into which items are.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
Queues Chapter 8 (continued)
Review Array Array Elements Accessing array elements
Chapter 7 Queues.
Data Abstraction & Problem Solving with C++
Data Structures Using C++ 2E
CC 215 Data Structures Queue ADT
CENG 213 Data Structure Queue 7/2/2018.
Stacks and Queues.
Queues Queues Queues.
Chapter 13 Queues and Priority Queues
Principles of Computing – UFCFA3-30-1
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
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.
Queues Chapter 8 (continued)
Queues.
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
Discrete Event “Hand” Simulation of a GI/GI/1 Queue
Chapter 8 Queues © 2006 Pearson Addison-Wesley. All rights reserved.
Visit for more Learning Resources
Using a Queue Chapter 8 introduces the queue data type.
Using a Queue Chapter 8 introduces the queue data type.
CSCS-200 Data Structure and Algorithms
Queues.
Presentation transcript:

CS Data Structures I Chapter 7 Queues II

2 Topics Queue Application Simulation Comparison of List, Stack and Queue

3 Simulation Simulate the behavior of a system by constructing a mathematical or a physical model capturing the relevant information about the system Real-word systems are called “ Queuing Systems ” Theory used in simulation is called “ Queuing Theory ” Examples: Computer tasks Supermarket

4 Simulation Goal of simulation: Generate statistics summarizing or predicting the performance of systems, that could be used for improvement Example: Bank system Server: Teller Objects being served: Customers What should be observed: Average waiting time Events: Arrivals & departures of customers Service time: Time needed for each customer

5 Application: Bank Simulation Ms. Simpson, President of First City Bank of Springfield, has heard her customers complain about how long they have to wait for service. Because she fears that they may move their account to another bank, she is considering whether to hire a second teller. Ms. Simpson needs an approximation of the average time that customer has to wait for service from the only teller available. How can she get this information?

6 Application: Bank Simulation Questions that should be answered: How many teller does the bank employ? How often customers arrive? What is the average time does a customer have to wait before receiving service? How many employers should be hired to improve the performance of the bank?

7 Application: Bank Simulation Events: Customer arrivals External events Customer departures when completing transaction Internal events Statistics needed: Average time a customer waits for service = Total waiting time for all customers / Number of customers

8 Application: Bank Simulation Assumptions: The list of all arrival events, is already available for use in a file in the form of pairs (Arrival time, Transaction time) The events are given in Ascending order By arrival time Departure events are not included in the file since it could be calculated

9 Application: Bank Simulation Event list: Contains the unprocessed arrival & departure events Departure time: Departure time = arrival time + transaction time Waiting time: Time elapsed between arrival & start of transaction

10 Application: Bank Simulation Required operations: Add / remove customers Add /remove events Required data structures: A queue representing customers in line: will contain arrival time & duration of transaction A list representing the event list

11 Application: Bank Simulation The new customer always enters the queue, even if the queue is empty When a customer is ready for service, the following operations take place: Remove the customer from the queue Delete the arrival event for the new customer from the event list Insert the departure event into the event list The place that an event is inserted in the event list depends on the relative time of that event

12 Application: Bank Simulation Example: If the file contains the following: Arrival timeTransaction duration The result of simulation will be as follows: TimeEvent 20Cust1 enters bank & begins transaction 22Cust2 enters bank & stands at line end 23Cust3 enters bank & stands at line end 25Cust1 departs & Cust2 begins transaction 29Cust2 departs & Cust3 begins transaction 30Cust4 enters bank & stands at line end 31Cust3 departs & Cust4 begins transaction 34Cust4 departs

13 Application: Bank Simulation Arrival Events Indicating Arrival at the bank of a custmer One of the two thing happens: If the teller is idle, the customer enters the line and begins the service If the teller is busy, the customer enters the waiting line Departure Events Indicating the departure of a customer who finish server When there is another on the line, the new customer begins service

14 Application: Bank Simulation Pseudocode (First Draft) for simulation Determine the times at which the events occur, and process the events Increments the time by increments of 1 //Initialize currentTime = 0 Initialize the line to “no customers” while (currentTime <= time of final event) {if (an arrival event occurs at time currentTime) process the arrival event if (a departure event occurs at time currentTime) process the departure event // when both arrival & departure occur at the same time, // arbitrarily process the arrival event first ++currentTime } // end while

15 Application: Bank Simulation Pseudocode (Second Draft) Considers only times of relevant events (arrival & departure) //Initialize the line to “no customers” while (event remain to be processed) {currentTime = time of next event if (event is an arrival event ) process the arrival event else process the departure event // when both arrival & departure events occur at the same time, // arbitrarily process the arrival event first } // end while

16 Application: Bank Simulation Example: Observations Each arrival event generates exactly one departure event We cannot generate a departure event for a given arrival event independent of other events. If we read the whole file of events, we will need to the calculations that the simulation performs => It is better to store one arrival and one departure event at a time and let the simulation perform the calculation step-by-step The event list will contain at most one event of each kind

17 Application: Bank Simulation Example: Observations For arrival events Keep the earliest unprocessed arrival event When the event is processed, replace it with the next unprocessed event For departure events The next departure event depends on the customer currently served Evaluate time o next departure from Time service begins Length of transaction Departure time = arrival time + transaction time As soon as a customer begins service, we place the corresponding departure event corresponding to this customer in the event list

18 Application: Bank Simulation Example: Event list structure A Arrival time Transaction time D Departure time Arrival event Departure event

19 Application: Bank Simulation Possible event list configurations Initially: One arrival event A read from the input file Event list: A Generally: Two events (Arrival A & Departure D) Event list: A D (next event is arrival) or D A (next event is departure) Special cases: If Arrival event is first and, after it is processed, the file is empty (eof): Event list: D (input has been exhausted) If Departure event is first and teller line is empty: Event list: A (departure leaves teller line empty)

20 Application: Bank Simulation Events are inserted in the event list the beginning or at the end depending on their arrival times Algorithm for arrival events // Update the event list Delete the arrival event for customer C from the event list if ( new customer C begins transaction immediately) Insert a departure event for customer C into the event list (time of event = current time + transaction length) If (not at the end of the input file) Read a new arrival event & add it to the event list (time of event = time specified in file)

21 Application: Bank Simulation Algorithm for departure events // Update the line Delete customer at front of queue If (the queue is not empty) Current front customer begins transaction // Update the event list Delete the departure event from the event list If (the queue is not empty) Insert into the event list the departure event for the customer now at the front of the queue (time of event = current time + transaction length)

22 Application: Bank Simulation Final simulation algorithm +simulate ( ) // perform the simulation Create an empty queue bankQueue to represent the bank line Create an empty event list eventList Get the first arrival event from the input file & place it in eventList while ( eventList is not empty) {newEvent = first event in eventList if (newEvent is arrival event) processArrival(newEvent, arrivalFile, eventList, bankQueue) else processDeparture(newEvent, eventList, bankQueue) } // end while

23 Application: Bank Simulation Final simulation algorithm +processArrival( in arrivalEvent: Event, in arrivalFile: File, inout anEventList: EventList, inout bankQueue: Queue) // Processes an arrival event atFront = bankQueue.isEmpty() // present queue status // Update bankQueue by inserting the customer, as described in arrivalEvent, // into the queue bankQueue.enqueue ( arrivalEvent) // Update the event list Delete arrivalEvenet from anEventList If (atFront) {// The line was empty, new customer at the front of // the line begins transaction immediately Insert into anEventList a departure event corresponding to the new customer with currentime = currentTime + transaction length } // end if If (not at end of input file) {Get the next arrival event from arrivalFile Add the event – with time as specified in input file – to anEventList } // end if

24 Application: Bank Simulation Final simulation algorithm +processDeparture( in departureEvent: Event, inout anEventList: EventList, inout bankQueue: Queue) // Processes an departure event // Update the line by deleting the front customer bankQueue.dequeue ( ) // Update the event list Delete departureEvent from anEventList If (! bamkQueue.isEmpty) {// Customer at front of line begins transaction Insert into the event list the departure event corresponding to the customer now at the front of the line & has currentTime = currentTime + transaction length } // end if

25 Application: Bank Simulation ADT Event List Operations +createEventList() // Create an empty event list +destroyEventList()// Destroys an event list +isEmpty(): boolean {query} // Determines whether an event list is empty +insert(in anEvent: Event) // Inserts anEvent into an event list so that events are ordered by time. If an arrival event & departure event have the same time, the arrival event precedes the departure event +delete()// Deletes the first event from an event list +retrieve( out anEvent: Event) // Sets anEvent to the first event in an event list

26 Summary of Position-Oriented ADTs Lists Operations are defined in terms of position of data items No restrictions on the position Operations: create: Creates an empty ADT of the List type isEmpty: Determines whether an item exists in the ADT insert: Inserts a new item in any given position remove: Deletes an item from a given position retrieve: Retrieves the item in the specified position

27 Summary of Position-Oriented ADTs Stacks Operations are defined in terms of position of data items Position is restricted to the Top of the stack Operations: create: Creates an empty ADT of the Stack type isEmpty: Determines whether an item exists in the ADT push: Inserts a new item into the Top position pop: Deletes an item from the Top position getTop: Retrieves the item from the Top position

28 Summary of Position-Oriented ADTs Queues Operations are defined in terms of position of data items Position is restricted to the Front & Back of the queue Operations: Create: Creates an empty ADT of the Queue type isEmpty: Determines whether an item exists in the ADT enqueue: Inserts a new item in the Back position dequeue: Deletes an item from the Front position getFront: Retrieves the item from the Front position