# cs2006 - data structures i chapter 7 queues ii. 2 topics queue application simulation comparison of...

Post on 18-Dec-2015

218 views

Category:

## Documents

Embed Size (px)

TRANSCRIPT

• Slide 1
• CS2006 - Data Structures I Chapter 7 Queues II
• Slide 2
• 2 Topics Queue Application Simulation Comparison of List, Stack and Queue
• Slide 3
• 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
• Slide 4
• 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
• Slide 5
• 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?
• Slide 6
• 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?
• Slide 7
• 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
• Slide 8
• 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
• Slide 9
• 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
• Slide 10
• 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
• Slide 11
• 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
• Slide 12
• 12 Application: Bank Simulation Example: If the file contains the following: Arrival timeTransaction duration 205 224 232 303 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
• Slide 13
• 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
• Slide 14
• 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
• 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
• Slide 17
• 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
• Slide 18
• 18 Application: Bank Simulation Example: Event list structure A Arrival time Transaction time D Departure time Arrival event Departure event
• Slide 19
• 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)
• Slide 20
• 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)
• Slide 21
• 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)
• Slide 22
• 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
• Slide 23
• 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
• Slide 24
• 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
• Slide 25
• 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 i