queues, deques, and priority queues chapter 14. 2 chapter contents specifications for the adt queue...

23
Queues, Deques, and Priority Queues Chapter 14

Post on 22-Dec-2015

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Queues, Deques, and Priority Queues Chapter 14. 2 Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes

Queues, Deques, and Priority Queues

Chapter 14

Page 2: Queues, Deques, and Priority Queues Chapter 14. 2 Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes

2

Chapter Contents

Specifications for the ADT QueueUsing a Queue to Simulate a Waiting Line• The Classes WaitLine and Customer

Using a Queue to Compute the capital Gain in a Sale of Stock• The Classes StockLedger and StockPurchase

Specifications of the ADT DequeUsing a Deque to Compute the Capital Gain in a Sale of StockSpecifications of the ADT Priority QueueUsing a Priority Queue to Track Your Assignments

Page 3: Queues, Deques, and Priority Queues Chapter 14. 2 Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes

3

Specifications for the ADT Queue

Queue organizes entries according to order of entry,

Front of queue has items added first, end of queue has items added last. It organizes items chronologically• Exhibits first-in, first-out behavior• Contrast to stack which is last-in, first-out

All additions are at the back of the queue, removals are at the front of the queue.

Page 4: Queues, Deques, and Priority Queues Chapter 14. 2 Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes

4

Specifications for the ADT Queue

Some everyday queues.

Page 5: Queues, Deques, and Priority Queues Chapter 14. 2 Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes

5

Specifications for the ADT Queue

Interface for a queue of objectspublic interface queueInterface{ /** Task: Adds a new entry to the back of the queue.

* @param newEntry an object to be added */public void enqueue(Object newEntry);

/** Task: Removes and returns the front of the queue.* @return either the object at the front of the queue or null if the queue was empty */public Object dequeue();

/** Task: Retrieves the front of the queue.* @return either the object at the front of the queue or null if the queue is empty */public Object getFront();

/** Task: Determines whether the queue is empty.* @return true if the queue is empty */public boolean isEmpty();

/** Task: Removes all entries from the queue. */public void clear();

} // end queueInterface

Page 6: Queues, Deques, and Priority Queues Chapter 14. 2 Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes

6

Specifications for the ADT Queue

Queue of strings after (a) enqueue adds Jim; (b) Jess; (c) Jill; (d) Jane; (e) Joe; (f) dequeue retrieves,

removes Jim; (g) enqueue adds Jerry; (h) dequeue retrieves, removes Jess.

Page 7: Queues, Deques, and Priority Queues Chapter 14. 2 Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes

7

Using a Queue to Simulate a Waiting Line

A line, or queue of people.

Short waiting time increase satisfaction.

Page 8: Queues, Deques, and Priority Queues Chapter 14. 2 Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes

8

Waiting Line

Time-driven simulation, a counter enumerates simulated units of time - minutes etc.

Customers arrive at random times and are assigned a random transaction time. • Math.random();• Maximal_transaction_time * Math.random()

At the conclusion, summary statistics are created including total number of customers served and the average waiting time.

Page 9: Queues, Deques, and Priority Queues Chapter 14. 2 Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes

9

Classes WaitLine and Customer

A diagram of the classes WaitLine and Customer to generate a new customer

Page 10: Queues, Deques, and Priority Queues Chapter 14. 2 Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes

10

Classes WaitLine and Customer

A simulated waiting line … continued →

Page 11: Queues, Deques, and Priority Queues Chapter 14. 2 Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes

11

Classes WaitLine and Customer

(ctd) A simulated waiting line.

Page 12: Queues, Deques, and Priority Queues Chapter 14. 2 Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes

12

Pseudocode of Method SimulateAlgorithm simulate (duration, arrivalProbability, maxTransactionTime)for( clock = 0; clock < duration; clock++){

if( a new customer arrives){ numberOfArrivals++ transactionTime = a random time does not exceed maxTransactionTime nextArrival = a new customer containing clock, transactionTime, and a customer number that is numberOfArrival

line.enqueue(nextArrival)}if( transactionTimeLeft > 0 ) // if present customer is still being served transactionTimeLeft—else if(!line.isEmpty)

{ nextCustomer = line.dequeue() transactionTimeLeft = nextCustomer.getTransactionTime() -1 timeWaited = clock() – nextCustomer.getArrivalTime() totalTimeWaited = totalTimeWaited + timeWaited numberServed++}

}

Page 13: Queues, Deques, and Priority Queues Chapter 14. 2 Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes

13

Using a Queue to Compute Capital Gain in a Sale of Stock

Buy n shares of a stock for d dollars each. Later you sell some of these shares.

If the sale price exceeds the purchase price, you have made a profit - a capital gain.

Otherwise, you experience a loss.

Let’s assume you sell shares in the order in which you purchase them. ( first-in, first-out)

Page 14: Queues, Deques, and Priority Queues Chapter 14. 2 Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes

14

Using a Queue to Compute Capital Gain in a Sale of Stock

Must sell stocks in same order they were purchased• Must use the difference in selling price and

purchase price to calculate capital gain

We use a queue to• Record investment transactions

chronologically• Compute capital gain of the stock

Page 15: Queues, Deques, and Priority Queues Chapter 14. 2 Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes

15

Classes StockLedger and StockPurchase

A diagram of the class StockLedger and StockPurchase.

Page 16: Queues, Deques, and Priority Queues Chapter 14. 2 Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes

16

30 shares at $45 eachtwo queues

(a) Adds each of the 30 individual shares to a queue

(b) Encapsulate 30 shares into one object

If we only wan to sell 20 shares with 10 shares left. (a) is easy to implement. (b). Need to place 10 left shares

to the front of the queue.

Page 17: Queues, Deques, and Priority Queues Chapter 14. 2 Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes

17

Specifications of the ADT Deque

A Double ended queue• At a post office, when it is your turn, you

need to fill up a form, step aside and get served when finished.

Has operations that • Add, remove, or retrieve entries • At both its front and back

Combines and expands the operations of queue and stack

Page 18: Queues, Deques, and Priority Queues Chapter 14. 2 Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes

18

Specifications of the ADT Deque

An instance of a deque.

Page 19: Queues, Deques, and Priority Queues Chapter 14. 2 Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes

19

Specifications of the ADT Deque

A Java interface

public interface DequeInterface{ public void addToFront(Object newEntry);

public void addToBack(Object newEntry);public Object removeFront();public Object removeBack();public Object getFront();public Object getBack();public boolean isEmpty();public void clear();

} // end DequeInterface

Page 20: Queues, Deques, and Priority Queues Chapter 14. 2 Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes

20

Specifications of the ADT Deque

A comparison of the operations that add, remove, and retrieve the entries of a stack s, queue q, and a deque

d; (a) add; (b) remove; (c) retrieve.

Page 21: Queues, Deques, and Priority Queues Chapter 14. 2 Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes

21

Specifications of the ADT Priority Queue

Organizes objects according to priorities• Contrast to regular queue in order of arrival

Priority queue example – a hospital ER: assign a priority number to each patient to override the arrival time.

Priority can be specified by an integer• Must define whether high number is high priority or …• Low number (say 0) is high priority

Other objects can specify priority• Object must have a compareTo method

Page 22: Queues, Deques, and Priority Queues Chapter 14. 2 Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes

22

Specifications of the ADT Priority Queue

public interface PriorityQueueInterface{ public void add(Comparable newEntry);

public Comparable remove();public Comparable get();public boolean isEmpty();public int getSize();public void clear();

} // end PriorityQueueInterface

Page 23: Queues, Deques, and Priority Queues Chapter 14. 2 Chapter Contents Specifications for the ADT Queue Using a Queue to Simulate a Waiting Line The Classes

23

Using Priority Queue to Track Your Assignment

Class TrackAssignment• Include data field DueDate • Method compareTo is made available• This field is the priority – earliest due date is

highest priority• Organize tasks in the order in which we should

complete them.