Queues

26
Queues Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 4

description

Data Structure: Chapter 4. Min Chen School of Computer Science and Engineering Seoul National University. Queues. Content. Definition of Queues Operators for Queues Insert Remove Peek Special Queues Circular Queues Priority Queues Efficiency of Queues. Definition of Stacks. - PowerPoint PPT Presentation

Transcript of Queues

Introduction to Data Structure

QueuesMin ChenSchool of Computer Science and Engineering Seoul National UniversityData Structure: Chapter 4ContentDefinition of QueuesOperators for QueuesInsertRemovePeekSpecial QueuesCircular QueuesPriority QueuesEfficiency of QueuesDefinition of StacksA Queue is a First-In-First-Out(FIFO) abstract data structure.

In a Queue, new item insert to the rear and the item in the front will be removed first

RearFrontFunctional Requirements of a QueueA queue is similar to a stack, except that you join the queue at one end and leave it at the other.We shall again use an array to storing the items in the queue.We shall need two pointers, one to indicate where the next item to be placed in the queue should be stored (rear), and another to indicate the location of the next item to be retrieved (front).Analogy of QueueFig.1 Bus Queue: A Typical Queue

Analogy of Queue: Some everyday queues

7Typical Java Interface for a queue of objectsAbstract Data Type (ADT): dont care about how to realize, just use it.public interface QueueInterface{/** Task: Add item to back */public void enqueue(Object newEntry);/** Task: Removes and returns the front */public Object dequeue();/** Task: Retrieves front of queue without removing */public Object getFront();/** Task: Determines whether the queue is empty.*/public boolean isEmpty();/** Task: Removes all entries from the queue. */public void clear();}8A Simple Example

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.Using a Queue to Simulate a Waiting Line

Using a Queue to Simulate a Waiting Line: Customer Enqueues for Service

Using a Queue to Simulate a Waiting Line: Customer Enqueues for Service 2

Types of Queuing SystemsSingle queue, single serverSingle queue, multiple servers:Improve processes, e.g., most banks use a snake queue instead individual lines at each teller. 2 queues, 2 servers vs. 1 queue, 2 servers Increase utilization ratioElectronic Queuing System (see animation)Multiple queues, single serverIntersection Management byTraffic Light (see animation) Multiple queues, multiple servers

Inpatient customer strategiesCustomers who intended to join the line but dontCustomers who join, then leaveJumping from queue to queueJumping to newly opened lineFollow the employeeTwo line strategy (bring spouse or kids), then merge into a faster queueSee AnimationWhy Study QueuesPeople hate to wait in queues shorter queues may result in increased efficiency.Some customers leave shopping carts full of groceries at the checkout counters and frozen and cold food are wasted.Operators for Queues: InsertInsert: insert a data item to the rear of the queueFig.2 Insert Operator in QueuesFrontRearProgram

Operators for Queues: RemoveRemove: Remove a data item at the front of the queueFig.3 Remove Operator in QueuesFrontRearProgram

Operators for Queues: PeekPeek: Get a data item at the front of the queue without removingFig.4 Peek Operator in QueuesFrontRearSpecial Queues 1: Circular QueuesCircular QueuesWe shall treat the underlying array as a circular buffer.Problem StatementFrontRearNo SpaceRearFig.5 Insertion of Circular QueuesCircular Queues: Simple ImplementationItems to be enqueued will be written in the array at queue[rear].Items to be dequeued will be read from queue[front].After each writing, top is incremented by one.After each reading, rear is incremented by one.Incrementing of rear and front is always modulo queue.length.The priority queue is a data structure that only allows access to the minimum item in a set.Special Queues 2: Priority QueuePriority Queue: Realization of Network Simulation

Discrete event simulation (desired simulation time is used to set priorities)Special Queues 2: Priority QueueThe items in Priority Queue has priorityInsertion in Priority Queue is differentFig.6 Insertion of Priority QueuesFrontRear219945817592112214Implementing Priority QueuesGoals: fast findMin, deleteMin, insertfindMin: Identify entry whose key is lowestdeleteMin: remove entry whose key is lowestInsert: any entry may be inserted at any timeHow should we implement a priority queue?Option 1: a linked listOption 2: a sorted linked listOption 3: a binary search treeOption 4: a balanced binary search treeProblem rised by Priority QueueStarvingWhen items with higher priority are inserted into the queue, the items with lower priority will be starvedFairness problems should be consideredEfficiency of QueuesIn conventional queues, items can be both pushed and popped from a stack in constant O(1) timeFor priority queues, insertion runs in O(N) time, whereas deletion takes O(1) timeThank you!Time

Simulation Kernel

Intrpt Code

Module ID

Event ID

Event Type