1 cs 132 spring 2008 chapter 8 queues. 2 queue a data structure in which the elements are added at...

35
1 CS 132 Spring 2008 Chapter 8 Queues

Upload: brittany-fields

Post on 01-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other

1

CS 132 Spring 2008Chapter 8

Queues

Page 2: 1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other

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

Page 3: 1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other

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

Page 4: 1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other

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

Page 5: 1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other

5

A Queue

A B C D E

4

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()?

3

Page 6: 1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other

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]

Page 7: 1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other

7

Adding to a Queue

Page 8: 1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other

8

Deleting from a Queue

What now?

Page 9: 1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other

9

Conceptually: a Circular Queue

Idea: reuse slots that have been vacated by deleteQueueWhat do we have that allows us to wraparound, going from 99+1 to 0?

Page 10: 1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other

10

Page 11: 1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other

11

Queues as Circular Arrays

What happens if we add an element 'z'?

Page 12: 1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other

12

Queues as Circular Arrays

It looks the same as an empty queueSolutions: 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

Page 13: 1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other

13

private:

{

int maxQueueSize;

int count;

int queueFront;

int queueRear;

Type *list

}

[maxQueueSize-1]

Implementation

Page 14: 1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other

14

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

Page 15: 1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other

15

What about a linkedQueue?

queueFront queueRear

private:

{

nodeType<Type> queueFront;

nodeType<Type> queueRear;

}

How would addQueue(50) work?

deleteQueue?

Page 16: 1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other

16

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

Page 17: 1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other

17

NewCustomer

Server

CustomerCustomer

One Server

Page 18: 1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other

18

Two Servers with Two Queues

NewCustomer

Server

CustomerCustomer

Server

Customer

Page 19: 1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other

19

Three Servers with a Single Queue

NewCustomer

Server

Customer

Customer

(arriving)

Server

CustomerCustomer Customer

Server

Customer

(leaving)

(next)

Page 20: 1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other

20

Movie Theater Simulation

What do we want to track? how many customers were served

the sum of their waiting times

Assumptionswhen 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)

Page 21: 1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other

21

Movie Theater Simulation

Time-driven simulationrun 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 simulationincrements 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

Page 22: 1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other

22

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?

Page 23: 1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other

23

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

NewCustomer

Server

Customer

Customer

(arriving)

Server

CustomerCustomer Customer

Server

Customer

(leaving)

(next)

Page 24: 1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other

24

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

Page 25: 1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other

25

Object-Oriented Approach

serverType properties– whether free or busy

– the current customer

– remaining transaction time

serverTypeList properties– how many servers

– the list

Page 26: 1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other

26

Movie Theater Simulation

Page 27: 1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other

27

Movie Theater Simulation

Page 28: 1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other

28

waitingCustomerQueueType

A queue of customers (suppose clock= 12)

queueRear queueFront

What happens with a clock tick?

cust#: 5:arrT: 9transT: 6waitT: 3

cust#: 4arrT: 7transT: 5waitT: 5

cust#: 3arrT: 5transT: 5waitT: 7

queueRear queueFront

Is it different from the normal queue?

Its type is customertype

Additional operation: update the waiting time

cust#: 5:arrT: 9transT: 6waitT: 3 4

cust#: 4arrT: 7transT: 5waitT: 5 6

cust#: 3arrT: 5transT: 5waitT: 7 8

Page 29: 1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other

29

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

Page 30: 1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other

30

waitingCustomerQueueType

Page 31: 1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other

31

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

Page 32: 1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other

32

updateWaitingQueue()

Before:

queueRear queueFront

cust#: 5:arrT: 9transT: 6waitT: 3

cust#: 4arrT: 7transT: 5waitT: 5

cust#: 3arrT: 5transT: 5waitT: 7

cust#: 5:arrT: 9transT: 6waitT: 3

cust#: 4arrT: 7transT: 5waitT: 5

cust#: 3arrT: 5transT: 5waitT: 7

cust#: -1arrT: 0transT: 0waitT: 0

(add one to waitT)

Page 33: 1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other

33

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?

Page 34: 1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other

34

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

Page 35: 1 CS 132 Spring 2008 Chapter 8 Queues. 2 Queue A data structure in which the elements are added at one end, called the rear, and deleted from the other

35

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