CS2006 - Data Structures I Chapter 7 Queues II. 2 Topics Queue Application Simulation Comparison of...
-
Upload
oscar-jackson -
Category
Documents
-
view
225 -
download
3
Transcript of CS2006 - Data Structures I Chapter 7 Queues II. 2 Topics Queue Application Simulation Comparison of...
CS2006 - Data Structures I
Chapter 7Queues 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 SimulationMs. 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 time Transaction duration
20 522 423 230 3
The result of simulation will be as follows:Time Event
20 Cust1 enters bank & begins transaction22 Cust2 enters bank & stands at line end23 Cust3 enters bank & stands at line end25 Cust1 departs & Cust2 begins transaction29 Cust2 departs & Cust3 begins transaction30 Cust4 enters bank & stands at line end31 Cust3 departs & Cust4 begins transaction34 Cust4 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
//InitializecurrentTime = 0Initialize the line to “no customers”while (currentTime <= time of final event){ if (an arrival event occurs at time currentTime)
process the arrival eventif (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
elseprocess 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 listDelete the arrival event for customer C from the event listif ( 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 lineDelete customer at front of queueIf (the queue is not empty)
Current front customer begins transaction// Update the event listDelete the departure event from the event listIf (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 simulationCreate an empty queue bankQueue to represent the bank lineCreate an empty event list eventListGet the first arrival event from the input file & place it in eventListwhile ( 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 eventatFront = bankQueue.isEmpty() // present queue status// Update bankQueue by inserting the customer, as described in arrivalEvent,// into the queuebankQueue.enqueue ( arrivalEvent)// Update the event listDelete arrivalEvenet from anEventListIf (atFront){ // The line was empty, new customer at the front of
// the line begins transaction immediatelyInsert into anEventList a departure event corresponding to the new customer with currentime = currentTime + transaction length
} // end ifIf (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 customerbankQueue.dequeue ( )
// Update the event listDelete departureEvent from anEventList
If (! bamkQueue.isEmpty){ // Customer at front of line begins transaction
Insert into the event list the departure event corresponding tothe customer now at the front of the line & hascurrentTime = 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