Pai Ch05 Queue

34
1 PROPRIETARY MATERIAL. © 2008 The McGraw-Hill Companies, Inc. All rights reserved. No part of this PowerPoint slide may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their individual course preparation. If you are a student using this PowerPoint slide, you are using it without permission. Education Copyrighted Material -Additional resource material supplied with the book Data Structures and Algorithms : Concepts, Techniques and Applications authored by G.A.V. Pai and published by Tata McGraw Hill. This resource material is for Instructor's use only. Queues (Chapter 5)

description

queue

Transcript of Pai Ch05 Queue

Page 1: Pai Ch05 Queue

1

PROPRIETARY MATERIAL. © 2008 The McGraw-Hill Companies, Inc. All rights reserved. No part of this PowerPoint slide may be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their individual course preparation. If you are a student using this PowerPoint slide, you are using it without permission.

@ McGraw-Hill Education

Copyrighted Material -Additional resource material supplied with the book Data Structures and Algorithms : Concepts, Techniques and Applications authored by G.A.V. Pai and published by Tata McGraw Hill. This resource material is for Instructor's use only.

Queues

(Chapter 5)

Page 2: Pai Ch05 Queue

2

1. Introduction

- Definition

- Operations on queues2. Linear Queue implementation

- Using array as a queue

- Implementation of insert and delete operations on a queue

- Limitations of linear queues

3. Circular queues- Operations on circular queues

- Implementation of insert and delete operations on a circular queue

4. Other types of queues - Priority queues

- Deques

5. Applications

6. ADT for queues

Outline of Chapter

Page 3: Pai Ch05 Queue

3

1. INTRODUCTION

Page 4: Pai Ch05 Queue

4

• A A QueueQueue is a is a linear listlinear list in which all insertions are in which all insertions are made at one end of the list known as made at one end of the list known as rearrear or or tailtail of of the queue and all deletions are made at the other the queue and all deletions are made at the other end known as end known as frontfront or or headhead of the queue. of the queue.

Definition: What is Queue?

Front RearFront Rear

A queue A queue

a b ca b cFront

Rear

Page 5: Pai Ch05 Queue

5

• Assume that we have a queue Assume that we have a queue QQ

• An An insertioninsertion operation is also operation is also referred to as referred to as enqueuingenqueuing a a queuequeue

• A A deletiondeletion operation is referred operation is referred to as to as dequeuingdequeuing a queue a queue..

• A queue data structure obeys A queue data structure obeys the principle of the principle of first in first out first in first out (FIFO)(FIFO) or or first come first first come first served served (FCFS)(FCFS)

What is Queue (2) ?

Front RearFront Rear(a) A queue Q(a) A queue Q

a b ca b c

a b c a b c dd

Front RearFront Rear(b) Insert ‘d’ into Q(b) Insert ‘d’ into Q

b c db c d

Front RearFront Rear(c) Delete from Q(c) Delete from Q

Page 6: Pai Ch05 Queue

6

The The queuequeue data data structure supports structure supports twotwo operations viz., operations viz.,

• InsertionInsertion (or (or addition addition or or enqueueenqueue)) of of elements to a elements to a queue queue

• DeletionDeletion (or (or removal removal or or dequeuedequeue) of ) of elements from a elements from a queuequeue

Operations on Queue?

Front RearFront Rear(a) A queue Q(a) A queue Q

a b ca b c

a b c a b c dd

Front RearFront Rear(b) Insert ‘d’ into Q(b) Insert ‘d’ into Q

b c db c d

Front RearFront Rear(c) Delete from Q(c) Delete from Q

Page 7: Pai Ch05 Queue

7

Linear queueLinear queuePriority queue Priority queue

DequeDeque

Circular queue Circular queue

Different Types of Queues

Conceptual Viewrear front

H

DE

F

G B

A

C

Page 8: Pai Ch05 Queue

8

2. LINEAR QUEUES IMPLEMENTATION

Page 9: Pai Ch05 Queue

9

2 ways:2 ways:• A common A common method of implementingmethod of implementing a queue data a queue data

structure is to use another sequential data structure structure is to use another sequential data structure viz, viz, arrays arrays (Chapter 3).

– The The FrontFront and and RearRear variables contain indices of Front and Rear variables contain indices of Front and Rear• Queues have also been implemented using a Queues have also been implemented using a linked linked

data structuredata structure (Chapter 6) (Chapter 6)– the array implementation puts a limitation on the capacity of the array implementation puts a limitation on the capacity of

the queuethe queue

Implementing Queue Data Objects

Front RearFront RearA queue QA queue Q

R S V GR S V G

Array representation Array representation of Q[1:7]of Q[1:7]

R S V GR S V G

[1] [2] [3] [4] [5] [6] [1] [2] [3] [4] [5] [6] [7][7]

RearRear 44FronFrontt

00

Page 10: Pai Ch05 Queue

10

2 ways:2 ways:• A common A common method of implementingmethod of implementing a queue data a queue data

structure is to use another sequential data structure structure is to use another sequential data structure viz, viz, arrays arrays (Chapter 3).

– The The FrontFront and and RearRear variables contain indices of Front and Rear variables contain indices of Front and Rear• Queues have also been implemented using a Queues have also been implemented using a linked linked

data structuredata structure (Chapter 6) (Chapter 6)– the array implementation puts a limitation on the capacity of the array implementation puts a limitation on the capacity of

the queuethe queue

Implementing Queue Data Objects (2)

Front RearFront RearA queue QA queue Q

R S V GR S V G

Linked ListLinked List

RR

SS

VV

GG

Page 11: Pai Ch05 Queue

11

• Every Every insertioninsertion (or enqueue) of an element (or enqueue) of an element into the queue will increment the value of into the queue will increment the value of RearRear. .

Queue Operations - Insertion

Array representation of Array representation of Q[1:7]Q[1:7]

R S V G PR S V G P

[1] [2] [3] [4] [5] [6] [7][1] [2] [3] [4] [5] [6] [7]

RearRear 55FrontFront 00

Array representation of Array representation of Q[1:7]Q[1:7]

R S V G P A YR S V G P A Y

[1] [2] [3] [4] [5] [6] [7][1] [2] [3] [4] [5] [6] [7]

RearRear 77FrontFront 00

Page 12: Pai Ch05 Queue

12

• Every Every insertioninsertion (or enqueue) of an element (or enqueue) of an element into the queue has to necessarily test for a into the queue has to necessarily test for a QUEUE-FULLQUEUE-FULL condition condition beforebefore executing the executing the insertioninsertion operation. operation.

Queue Operations – Insertion (2)

Array representation Array representation of Q[1:7]of Q[1:7]

N=7N=7

R S V G P A R S V G P A Y Y[1] [2] [3] [4] [5] [6] [1] [2] [3] [4] [5] [6] [7][7]

RearRear 77FronFrontt

00 Queue-FullQueue-FullWhenWhen

Rear=NRear=NWhere Where

N=size of N=size of queuequeue

Page 13: Pai Ch05 Queue

13

• Every Every deletion deletion (or dequeue) of an element from the (or dequeue) of an element from the queue will increment the value of queue will increment the value of FrontFront. .

Queue Operations - Deletion

Array representation of Array representation of Q[1:7]Q[1:7]

R S V G P A YR S V G P A Y

[1] [2] [3] [4] [5] [6] [7][1] [2] [3] [4] [5] [6] [7]

RearRear 77FrontFront 00

Array representation of Array representation of Q[1:7]Q[1:7]

S V G P A YS V G P A Y

[1] [2] [3] [4] [5] [6] [7][1] [2] [3] [4] [5] [6] [7]

RearRear 77FrontFront 11

Deque RDeque R

Page 14: Pai Ch05 Queue

14

• Each Each deletiondeletion (or dequeue) has to ensure that it (or dequeue) has to ensure that it is not attempted on a queue which is already is not attempted on a queue which is already empty calling for the need to test for a empty calling for the need to test for a QUEUE-QUEUE-EMPTYEMPTY condition condition beforebefore executing the executing the deletiondeletion operation. operation.

Queue Operations - Deletion

Array representation Array representation of Q[1:7]of Q[1:7]

[1] [2] [3] [4] [5] [6] [1] [2] [3] [4] [5] [6] [7][7]

RearRear 00FronFrontt

00

Queue-Queue-EmptyEmpty

WhenWhenRear=FrontRear=Front

Page 15: Pai Ch05 Queue

15

Algorithm 5.1: Implementation of an insert operation on a queue

procedure INSERTQ (Q, n, ITEM, REAR) /* insert item ITEM into Q with capacity n */if (REAR = n) then QUEUE_FULL;REAR = REAR + 1; /* Increment REAR*/Q[REAR] = ITEM; /* Insert ITEM as the rear element*/end INSERTQ

Queue: Insert Algorithm

Checking for Checking for Queue-FullQueue-Full

Insert Data into Insert Data into Queue, if OKQueue, if OK

Page 16: Pai Ch05 Queue

16

Algorithm 5.2: Implementation of a delete operation on a queue

procedure DELETEQ (Q, FRONT, REAR, ITEM ) /* delete ITEM from FRONT of Q */

if (FRONT =REAR) then QUEUE_EMPTY;FRONT = FRONT +1; ITEM = Q[FRONT]; end DELETEQ.

Queue: Delete Algorithm

Checking for Checking for Queue-EmptyQueue-Empty

Delete Data Delete Data from Queue, if from Queue, if

OKOK

Page 17: Pai Ch05 Queue

17

• QueuesQueues whose insert / delete operations whose insert / delete operations follow the procedures implemented in follow the procedures implemented in Algorithms 5.1 and 5.2Algorithms 5.1 and 5.2, are known as , are known as linear linear queuesqueues

Limitations of Linear Queues?Limitations of Linear Queues?

• When a QUEUE_FULL condition is invoked it When a QUEUE_FULL condition is invoked it does not necessarily imply that the queue is does not necessarily imply that the queue is ‘physically’ full. ‘physically’ full. – This leads to the limitation of rejecting insertions This leads to the limitation of rejecting insertions

despite the space available to accommodate them. despite the space available to accommodate them. • SOLUTION? The rectification of this limitation SOLUTION? The rectification of this limitation

leads to what are known as circular queues. leads to what are known as circular queues.

Limitation of Linear Queues?

Page 18: Pai Ch05 Queue

18

3. CIRCULAR QUEUES

Page 19: Pai Ch05 Queue

19

• As the name indicates a As the name indicates a circularcircular queuequeue is not linear is not linear in structure but instead in structure but instead circularcircular. .

• In other words, the In other words, the FRONTFRONT and and REARREAR variables which variables which displayed a linear (left to displayed a linear (left to right) movement over a right) movement over a queue, display a circular queue, display a circular movement (clock wise) over movement (clock wise) over the queue data structure.the queue data structure.

• Implementation: Use Implementation: Use arrayarray

• Question? How do we Question? How do we determine the determine the next locationnext location in circular manner? in circular manner? – Answer? Use Answer? Use modmod operator operator

Circular Queues?

0 1 2 3 4 5 6 7

A B EDC GF H

front rear

Conceptual Viewrear front

H

DE

F

G B

A

C

Page 20: Pai Ch05 Queue

20

Algorithm 5.3: Implementation of insert operation on a circular queue (with 5 parameters)

procedure INSERT_CIRCQ(CIRC_Q, FRONT,REAR, n, ITEM)If (FRONT =(REAR + 1) mod n) then CIRCQ_FULL; /* Here CIRCQ_FULL tests for the queue full condition and if so, retracts REAR to its previous value*/ REAR=(REAR + 1) mod n;CIRC_Q [REAR]= ITEM; // if OK, insert ITEMend INSERT_CIRCQ.

Circular Queue: Insert Algorithm

Page 21: Pai Ch05 Queue

21

Algorithm 5.4: Implementation of a delete operation on a circular queue.

Procedure DELETE_CIRCQ(CIRC_Q, FRONT,REAR, n, ITEM)If (FRONT = REAR) then CIRCQ_EMPTY; /* CIRC_Q is physically empty*/FRONT = (FRONT+1) mod n; // find locationITEM = CIRC_Q [FRONT]; // get @ remove valueend DELETE_CIRCQ

Circular Queue: Delete Algorithm

Eg: FRONT=0; n=8Then FRONT=(FRONT + 1) mod n = 1 mod 8 = 1

Eg: FRONT=5; n=8Then FRONT=(FRONT + 1) mod n = 6 mod 8 = 6

Page 22: Pai Ch05 Queue

22

4. OTHER TYPES OF QUEUES : PRIORITY QUEUES & DEQUES

Page 23: Pai Ch05 Queue

23

• A A priority queuepriority queue is a is a queuequeue in which in which insertioninsertion or or deletiondeletion of items from any position in the queue are of items from any position in the queue are done based on some done based on some propertyproperty (such as (such as priority priority of taskof task))

• A common A common method of implementationmethod of implementation of a priority of a priority queue is to queue is to open as many queues open as many queues as there are priority as there are priority factors. factors. – A A low priority low priority queue will be operated for deletion only when all queue will be operated for deletion only when all

its its high priorityhigh priority predecessors are empty. predecessors are empty. • Another method of implementation could be to Another method of implementation could be to sortsort the the

elements in the queue according to the elements in the queue according to the descendingdescending order of priorities every time an insertion takes place. order of priorities every time an insertion takes place. – The The top priority element top priority element at the at the headhead of the queue is the one to of the queue is the one to

be be deleteddeleted. .

(I) Priority Queues

Page 24: Pai Ch05 Queue

24

Implementations: Implementations: • #1 - Multiple #1 - Multiple queues queues as there are priority factors. as there are priority factors.

– A A low priority low priority queue will be operated for deletion only when all queue will be operated for deletion only when all its its high priorityhigh priority predecessors are empty. predecessors are empty.

• #2 - Single list with #2 - Single list with sortedsorted elements in the queue elements in the queue according to the according to the descendingdescending order of priorities every order of priorities every time an insertion takes place. time an insertion takes place.

(I) Priority Queues

Page 25: Pai Ch05 Queue

25

• A A dequedeque (double ended queue) is a (double ended queue) is a linear listlinear list in which all in which all insertionsinsertions and and deletionsdeletions are are made at the made at the end of the listend of the list. .

• A A dequedeque is therefore more general than a is therefore more general than a stack or queue and is a sort of stack or queue and is a sort of FLIFLOFLIFLO (first in (first in last in or first out last out).last in or first out last out).

• Thus while one speaks of the top or bottom Thus while one speaks of the top or bottom of a stack, or front or rear of a queue, one of a stack, or front or rear of a queue, one refers to the refers to the right end right end or or left end left end of a deque. of a deque.

(II) Deques

Page 26: Pai Ch05 Queue

26

• A deque has two variants, viz., A deque has two variants, viz., input input restricted dequerestricted deque and and output restricted output restricted dequedeque. .

• An An input restricted deque:input restricted deque:

– where insertions are allowed at one end where insertions are allowed at one end only while deletions are allowed at both only while deletions are allowed at both ends. ends.

• An An output restricted deque:output restricted deque:

– Allows insertions at both ends of the deque Allows insertions at both ends of the deque but permits deletions only at one end. but permits deletions only at one end.

• A deque is commonly A deque is commonly implementedimplemented as as a a circular arraycircular array with two variables with two variables LEFTLEFT and and RIGHTRIGHT taking care of the active taking care of the active ends of the deque ends of the deque

Deques (2)

Conceptual View

rear frontH

DE

F

G B

A

C

Page 27: Pai Ch05 Queue

27

5. APPLICATION OF QUEUES

Page 28: Pai Ch05 Queue

28

• A A CPUCPU (processor) endowed with (processor) endowed with memory resources, is to be memory resources, is to be sharedshared by by nn number of computer users. The number of computer users. The sharing of the processor and memory sharing of the processor and memory resources is done by allotting a resources is done by allotting a definite definite timetime sliceslice of the processor’s of the processor’s attention on the users and in a attention on the users and in a round-robinround-robin fashion. fashion.

• In a system such as this the users are In a system such as this the users are unaware of the presence of other unaware of the presence of other users and are led to believe that their users and are led to believe that their job receives the undivided attention job receives the undivided attention of the CPU. of the CPU.

• However, to However, to keepkeep tracktrack of the jobs of the jobs initiated by the users, the processor initiated by the users, the processor relies on a relies on a queue data structurequeue data structure recording the active user-ids. recording the active user-ids.

App#1: CPU TIME SHARING SYSTEM

New job enters (with user-id)

Job leaves CPU

Page 29: Pai Ch05 Queue

29

• Assume a CPU Assume a CPU time sharing systemtime sharing system in which in which jobjob requests by users are of requests by users are of different categoriesdifferent categories. Some . Some requests requests – may be may be realreal time, time, – the others the others onlineonline and and – the last may be the last may be batchbatch processing requests. processing requests.

• It is known that It is known that – real timereal time job requests carry the job requests carry the highesthighest priority, priority, – followed by followed by onlineonline processing and processing and – batchbatch processing in that order. processing in that order.

• In such a situation the job scheduler needs to maintain In such a situation the job scheduler needs to maintain a a priority queue priority queue to execute the job requests based to execute the job requests based on there priorities. on there priorities.

Application of Priority Queues

Page 30: Pai Ch05 Queue

30

• Bank ATM SystemBank ATM System

App#2: ATM Simulation

Page 31: Pai Ch05 Queue

31

6. ADT FOR QUEUES

Page 32: Pai Ch05 Queue

32

Data objects:A finite set of elements of the same type

Operations: Create an empty queue and initialize front and rear variables of the queue

CREATE ( QUEUE, FRONT, REAR) Check if queue QUEUE is empty

CHK_QUEUE_EMPTY (QUEUE ) (Boolean function)

Check if queue QUEUE is full CHK_QUEUE_FULL (QUEUE) (Boolean function)

Insert ITEM into rear of queue QUEUE ENQUEUE (QUEUE, ITEM)

Delete element from the front of queue QUEUE and output the element deleted in ITEM

DEQUEUE (QUEUE , ITEM)

ADT for Queue?

Page 33: Pai Ch05 Queue

33

7. CONCLUSION

Page 34: Pai Ch05 Queue

34

•A QueueQueue is a linear list in which all insertions are made at one end of the list known as rear or tail of the queue and all deletions are made at the other end known as front or head of the queue.

• An insertion operation is also referred to as enqueuing a queue and a deletion operation is referred to as dequeuing a queue.• A queue data structure obeys the principle of first in first out (FIFO) or first come first served (FCFS)

• Different types of queues:• Linear queues• Circular queue• Deques (double ended queue)• Priority queues

Conclusion