1 / 36

CS2006 - Data Structures I

CS2006 - Data Structures I. Chapter 8 Queues III. Topics. Queue Application Simulation Comparison of List, Stack and Queue. Simulation. Simulate the behavior of a system by constructing a mathematical or a physical model capturing the relevant information about the system

Download Presentation

CS2006 - Data Structures I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS2006 - Data Structures I Chapter 8 Queues III

  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 tellers 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

  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 + his waiting 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 20 5 22 4 23 2 30 3 • The result of simulation will be as follows: TimeEvent 20 Cust1 enters bank & begins transaction 22 Cust2 enters bank & stands at line end 23 Cust3 enters bank & stands at line end 25 Cust1 departs & Cust2 begins transaction 29 Cust2 departs & Cust3 begins transaction 30 Cust4 enters bank & stands at line end 31 Cust3 departs & Cust4 begins transaction 34Cust4 departs

  13. Application: Bank Simulation • Arrival Events • Indicating Arrival at the bank of a customer • 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 } // end while

  16. A Arrival time Transaction time Arrival event D Departure time Departure event Application: Bank Simulation • Example: • Event list structure

  17. 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 if ( new customer C begins transaction immediately at the front) 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)

  18. 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)

  19. 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

  20. 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 arrivalEvent 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

  21. 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

  22. 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

  23. Application: Bank Simulation • Example • Input file (1,4), (4, 3), (6, 5), (7, 2), (18, 7)

  24. 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

  25. 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 • top: • Retrieves the item from the Top position

  26. 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 • peek: • Retrieves the item from the Front position

  27. Review • The ADT ______ allows you to insert into, delete from, and inspect the item at any position of the ADT. • stack • queue • list • array

  28. Review • Which of the following is an operation of the ADT stack? • enqueue • push • Add • get

  29. Review • The pop operation of the ADT stack is similar to the ______ operation of the ADT queue. • isEmpty • enqueue • Dequeue • peek

  30. Review • The enqueue operation of the ADT queue is similar to the ______ operation of the ADT stack. • isEmpty • peek • Push • pop

  31. Review • In the ADT list, items can be added ______. • only at the front of the list • only at the back of the list • either at the front or at the back of the list • at any position in the list

  32. Review • In an event-driven simulation of a bank, which of the following is an internal event? • the arrival at the bank of a new customer • the start of the transaction of a customer • the end of the transaction of a customer • the departure from the bank of a customer

  33. Review • Which of the following is true about a time-driven simulation of a bank? • arrival times are obtained from an input file • transaction times are obtained from an input file • currentTime is incremented by 1 to simulate the ticking of a clock • no action is required between events

  34. Review • An event list for an event-driven simulation of a bank contains ______. • all arrival events • all arrival events that will occur but have not occurred yet • all departure events • all departure events that will occur but have not occurred yet • all arrival and departure events that will occur but have not occurred yet

  35. Review • In an event-driven simulation of a bank, which of the following equations can be used to determine the time of a customer’s departure? • time of next departure = current time – arrival time • time of next departure = time service begins + length of transaction • time of next departure = arrival time + length of transaction • time of next departure = current time – (arrival time + time service begins)

  36. Review • The line of customers in a bank can be represented as a(n) ______. • list • queue • Stack • array

More Related