cs 132 spring 2008 chapter 8

35
1 CS 132 Spring 2008 Chapter 8 Queues

Upload: hisoki

Post on 21-Jan-2016

53 views

Category:

Documents


0 download

DESCRIPTION

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: - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CS 132 Spring 2008 Chapter 8

1

CS 132 Spring 2008Chapter 8

Queues

Page 2: CS 132 Spring 2008 Chapter 8

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

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

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

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

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

7

Adding to a Queue

Page 8: CS 132 Spring 2008 Chapter 8

8

Deleting from a Queue

What now?

Page 9: CS 132 Spring 2008 Chapter 8

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

10

Page 11: CS 132 Spring 2008 Chapter 8

11

Queues as Circular Arrays

What happens if we add an element 'z'?

Page 12: CS 132 Spring 2008 Chapter 8

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

13

private:

{

int maxQueueSize;

int count;

int queueFront;

int queueRear;

Type *list

}

[maxQueueSize-1]

Implementation

Page 14: CS 132 Spring 2008 Chapter 8

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

15

What about a linkedQueue?

queueFront queueRear

private:

{

nodeType<Type> queueFront;

nodeType<Type> queueRear;

}

How would addQueue(50) work?

deleteQueue?

Page 16: CS 132 Spring 2008 Chapter 8

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

17

NewCustomer

Server

CustomerCustomer

One Server

Page 18: CS 132 Spring 2008 Chapter 8

18

Two Servers with Two Queues

NewCustomer

Server

CustomerCustomer

Server

Customer

Page 19: CS 132 Spring 2008 Chapter 8

19

Three Servers with a Single Queue

NewCustomer

Server

Customer

Customer

(arriving)

Server

CustomerCustomer Customer

Server

Customer

(leaving)

(next)

Page 20: CS 132 Spring 2008 Chapter 8

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

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

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

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

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

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

26

Movie Theater Simulation

Page 27: CS 132 Spring 2008 Chapter 8

27

Movie Theater Simulation

Page 28: CS 132 Spring 2008 Chapter 8

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

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

30

waitingCustomerQueueType

Page 31: CS 132 Spring 2008 Chapter 8

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

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

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

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

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