CS200 - Data Structures

Download Report

Transcript CS200 - Data Structures

CS2006 - Data Structures I Chapter 7 Queues II

2

Topics

Queue Application

 Simulation 

Comparison of List, Stack and Queue

Simulation

3     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

Simulation

4   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 since it could be calculated in the file

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

Application: Bank Simulation

11    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

Application: Bank Simulation

 Example:  If the file contains the following: Arrival time Transaction duration 

20 22 23 30 5 4 2 3

The result of simulation will be as follows: Time

20 22 23 25 29 30 31 34

Event

Cust1 enters bank & begins transaction Cust2 enters bank & stands at line end Cust3 enters bank & stands at line end Cust1 departs & Cust2 begins transaction Cust2 departs & Cust3 begins transaction Cust4 enters bank & stands at line end Cust3 departs & Cust4 begins transaction Cust4 departs

12

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

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

14

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

15

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

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 17

18

Application: Bank Simulation

Example:

Event list structure

Arrival event Departure event A Arrival time Transaction time D Departure time

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)

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)

21

Application: Bank Simulation

 22

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

Application: Bank Simulation

 23 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

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 } // end if currentTime = currentTime + transaction length

24

Application: Bank Simulation

 ADT Event List Operations

+createEventList() +destroyEventList() // Create an empty event list // 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

25

Summary of Position-Oriented ADTs

26  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

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 27

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 28