1 / 35

CS 132 Spring 2008 Chapter 8

CS 132 Spring 2008 Chapter 8. Queues. Queue. A data structure in which the elements are added at one end, called the rear , and deleted from the other end, called the front or first First In First Out (FIFO) Operations common to previous data structures:

hisoki
Download Presentation

CS 132 Spring 2008 Chapter 8

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. CS 132 Spring 2008Chapter 8 Queues

  2. Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other end, called the front or first First In First Out (FIFO) Operations common to previous data structures: • initializeQueue: initializes the queue to an empty state • destroyQueue: removes all the elements from the queue, leaving the queue empty • isEmptyQueue: checks whether the queue is empty--if the queue is empty, it returns the value true; otherwise, it returns the value false • isFullQueue: checks whether the queue is full--if the queue is full, it returns the value true; otherwise, it returns the value false

  3. New Operations front: returns the front (first) element of the queue back: returns the rear (last) element of the queue addQueue: adds a new element to the rear of the queue; the queue must exist and must not be full deleteQueue: removes the front element of the queue

  4. Array Implementation list: the array of elements queueFront: the first (front) element queueRear: the last (rear) element Dequeue: • retrieve the first element via front • remove it via deleteQueue Enqueue (traditional term): • addQueue to the rear

  5. A Queue A B C D E 4 3 What are the elements in the queue? What is returned by front()? What is returned by back()? What is the effect of addQueue('X')? What is the effect of deleteQueue()?

  6. An Empty Queue Note: when the first element is added, it will be put in slot 0. To add an element: • increment queueRear • put the new element in list[queueRear]

  7. Adding to a Queue

  8. Deleting from a Queue What now?

  9. Conceptually: a Circular Queue Idea: reuse slots that have been vacated by deleteQueue What do we have that allows us to wraparound, going from 99+1 to 0?

  10. Queues as Circular Arrays What happens if we add an element 'z'?

  11. Queues as Circular Arrays It looks the same as an empty queue Solutions: 1. maintain a count of the number of elements 2. let queueFront be the position preceding the first element and leave it empty Choose option #1

  12. Implementation private: { int maxQueueSize; int count; int queueFront; int queueRear; Type *list } [maxQueueSize-1]

  13. Initialize Queue template<class Type> void queueType<Type>::initializeQueue() { queueFront = 0; queueRear = maxQueueSize - 1; count = 0; } Note: queueRear does not point to an element when the first element is added, it will be put in slot 0

  14. What about a linkedQueue? queueFront queueRear private: { nodeType<Type> queueFront; nodeType<Type> queueRear; } How would addQueue(50) work? deleteQueue?

  15. Application of Queues: Simulation Major application of queues Build a computer model and "test" different policies Text example: the manager of a local movie theater wants to know whether to go from 1 cashier to 2 What are the considerations? • the cost of the cashier • how much an extra cashier reduces average wait time What happens when a customer arrives? • she gets in line • waits for the next available server • is served • goes into the theater

  16. One Server

  17. Two Servers with Two Queues

  18. Three Servers with a Single Queue

  19. Movie Theater Simulation What do we want to track? how many customers were served the sum of their waiting times Assumptions when the first customer arrives, all servers are free when a customer arrives, if there is a free server, she moves to it, otherwise gets in the wait queue when a customer is done, the first person in line moves to the free server What do we need to know # servers transaction time for each customer average time between customers (arrival rate) (assume customers arrive according to a Poisson distribution)

  20. Movie Theater Simulation Time-driven simulation run for a number of clock periods (e.g. 6000 1 minute periods) track the number of customers served and their cumulative waiting time When a customer starts being served: the time she waited is added to the cumulative waiting time the server initializes its remaining transaction time Each period the simulation increments the clock decrements the remaining transaction time for each server updates the waiting time for each customer in line checks to see if anyone arrived checks to see if there is a free server

  21. Procedural Bones //set simulationTime, numberOfServers, transactionTime, timeBetweenCustomerArrival //process simulationTime periods for(clock = 1; clock <= simulationTime; clock++) { //update remaining server transaction time //update the waiting time for each customer //check to see if a customer arrives //check for a free server } //print results How would we think of this from an object-oriented perspective?

  22. Object-Oriented Approach: what pieces (objects) do we need to build?

  23. Object-Oriented Approach Objects (classes) • customer • server • server list • customer queue What are their properties and methods? Customer properties • id • when she arrived • how long her transaction will take • how long she has waited in line

  24. Object-Oriented Approach serverType properties • whether free or busy • the current customer • remaining transaction time serverTypeList properties • how many servers • the list

  25. Movie Theater Simulation

  26. Movie Theater Simulation

  27. waitingCustomerQueueType A queue of customers (suppose clock= 12) queueRear queueFront What happens with a clock tick? cust#: 4 arrT: 7 transT: 5 waitT: 5 cust#: 3 arrT: 5 transT: 5 waitT: 7 cust#: 5: arrT: 9 transT: 6 waitT: 3 • queueRear queueFront • Is it different from the normal queue? • Its type is customertype • Additional operation: update the waiting time cust#: 4 arrT: 7 transT: 5 waitT: 5 6 cust#: 3 arrT: 5 transT: 5 waitT: 7 8 cust#: 5: arrT: 9 transT: 6 waitT: 3 4

  28. waitingCustomerQueueType Is it different from the normal queue? Its type is customerType Additional operation: update the waiting time (How) can we use the existing queueType? Inheritance!! What functions will need to be added? updateWaitingQueue to add one to each person's waiting time What functions will be different? the constructor

  29. waitingCustomerQueueType

  30. updateWaitingQueue() How can we add one to the waiting time for each customer? Clumsy: • dequeue each person and add one to her waiting time • add the updated customer to the end of the queue Problem: how do we know when to stop • add a dummy customer to the end before you start • stop when you reach her

  31. updateWaitingQueue() • Before: • queueRear queueFront cust#: 4 arrT: 7 transT: 5 waitT: 5 cust#: 3 arrT: 5 transT: 5 waitT: 7 cust#: 5: arrT: 9 transT: 6 waitT: 3 (add one to waitT) cust#: -1 arrT: 0 transT: 0 waitT: 0 cust#: 4 arrT: 7 transT: 5 waitT: 5 cust#: 3 arrT: 5 transT: 5 waitT: 7 cust#: 5: arrT: 9 transT: 6 waitT: 3

  32. updateWaitingQueue() void waitingCustomerQueueType::updateWaitingQueue() { customerType cust; cust.setWaitingTime(-1); int wTime = 0; addQueue(cust); while(wTime != -1) { cust = front(); deleteQueue(); wTime = cust.getWaitingTime(); if(wTime == -1) break; cust.incrementWaitingTime(); addQueue(cust); } } Can you think of a better way?

  33. Poisson Distribution A good way to model events that occur "on the average" E.g. suppose a bus arrives once every 10 minutes The probability of a bus arrives in 1 minute is ~1/10 If λ is the probability an event occurs in an interval, the probability that it occurs y times is P(y) = λye- λ/y! Busses Probability 0 (0.1)0 e -0.1 /0! =.9 1 (0.1)1 e -0.1 /1! =.09 2 (0.1)2 e -0.1 /2! =.0045

  34. Poisson Distribution Using this to generate arrivals for the movie theater • generate a number between 0 and 1 • see if it is > e- λ (the probability of 0 arrivals) no arrival arrival |_________________________| 0 e- λ 1 bool isCustomerArrived(double arvTimeDiff) { double value; value = static_cast<double> (rand()) / static_cast<double>(RAND_MAX); return (value > exp(- 1.0/arvTimeDiff)); } static_cast<double>? converts the expression to double

More Related