meljun cortes jedi slides data st-chapter03-queues

22
1 Data Structures – Queues 3 Queues

Upload: meljun-cortes

Post on 23-Jan-2015

663 views

Category:

Documents


4 download

DESCRIPTION

MELJUN CORTES Jedi slides data st-chapter03-queues

TRANSCRIPT

Page 1: MELJUN CORTES Jedi slides data st-chapter03-queues

1Data Structures – Queues

3 Queues

Page 2: MELJUN CORTES Jedi slides data st-chapter03-queues

2Data Structures – Queues

ObjectivesAt the end of the lesson, the student should be able to:● Define the basic concepts and operations on the ADT

queue● Implement the ADT queue using sequential and linked

representation● Perform operations on circular queue● Use topological sorting in producing an order of elements

satisfying a given partial order

Page 3: MELJUN CORTES Jedi slides data st-chapter03-queues

3Data Structures – Queues

Introduction● Queue - linearly ordered set of elements having the first-in,

first-out (FIFO) discipline ● Applications: job-scheduling, topological sorting, graph

traversals, etc.● 2 basic operations for data manipulation: insertion at the

rear (enqueue) and deletion at the front (dequeue)

Page 4: MELJUN CORTES Jedi slides data st-chapter03-queues

4Data Structures – Queues

Introduction● Two implementations: sequential and linked● ADT Queue in Java:

interface Queue{/* Insert an item */void enqueue(Object item) throws QueueException;/* Delete an item */Object dequeue() throws QueueException;

}

class QueueException extends RuntimeException{public QueueException(String err){

super(err);}

}

Page 5: MELJUN CORTES Jedi slides data st-chapter03-queues

5Data Structures – Queues

Sequential Representation● Sequential Representation

– Makes use of one-dimensional array/vector– Deletion from an empty queue causes an underflow– Insertion onto a full queue causes an overflow

Page 6: MELJUN CORTES Jedi slides data st-chapter03-queues

6Data Structures – Queues

Sequential RepresentationJava Implementation

● Front points to the actual front element of the queue while rear points to the cell immediately after the rear element

● Queue is empty if front=rear and full if front=0 and rear=n● Initialization : front = 0; rear = 0● Insertion : Q[rear] = x; rear++;● Deletion : x = Q[front]; front++;

Page 7: MELJUN CORTES Jedi slides data st-chapter03-queues

7Data Structures – Queues

Sequential RepresentationJava Implementation

● class SequentialQueue implements Queue{Object Q[];int n = 100 ; /* size of the queue, default 100 */int front = 0; /* front and rear set to 0 initially */int rear = 0;

/* Create a queue of default size 100 */SequentialQueue(){

● Q = new Object[n];}

/* Create a queue of the given size */SequentialQueue(int size){

n = size;Q = new Object[n];

– }●

/* Inserts an item onto the queue */public void enqueue(Object item) throws QueueException{

if (rear == n) moveQueue(); Q[rear] = item;rear++;

}

Page 8: MELJUN CORTES Jedi slides data st-chapter03-queues

8Data Structures – Queues

Sequential RepresentationJava Implementation

● /* Deletes an item from the queue /public Object dequeue() throws QueueException{

if (front == rear) throw new QueueException("Deleting from an empty queue.");

Object x = Q[front];front++;return x;

}

/* Moves the items to make room at the “rear-side” for future insertions */

void moveQueue() throws QueueException{if (front==0) throw new QueueException("Inserting

into a full queue");for(int i=front; i<n; i++)

Q[i-front] = Q[i];rear = rear – front;front = 0;

} }

Page 9: MELJUN CORTES Jedi slides data st-chapter03-queues

9Data Structures – Queues

Circular Queue● Cells are considered arranged

in a circle● front points to the actual

element at the front of the queue

● rear points to the cell on the right of the actual rear element

● Full queue always has one unused cell

Page 10: MELJUN CORTES Jedi slides data st-chapter03-queues

10Data Structures – Queues

Circular QueueJava Implementation

● Initialization: front = 0, rear = 0● Empty queue: if front == rear● Full queue: if front == (rear mod n)+ 1

public void enqueue(Object item) throws QueueException{if (front == (rear % n) + 1) throw new QueueException(

"Inserting into a full queue.");Q[rear] = item;rear = (rear %n) + 1);

}

public Object dequeue() throws QueueException{Object x;if (front == rear) throw new QueueException(

"Deleting from an empty queue.");x = Q[front];front = (front % n) + 1 ;return x;

}

Page 11: MELJUN CORTES Jedi slides data st-chapter03-queues

11Data Structures – Queues

Linked Representation● Linked Representation

– Queue is empty if front = null – Overflow will happen only when the program runs out of memory

Page 12: MELJUN CORTES Jedi slides data st-chapter03-queues

12Data Structures – Queues

Linked RepresentationJava Implementation

● class LinkedQueue implements Queue{queueNode front, rear;

/* Create an empty queue */LinkedQueue(){}

/* Create a queue with node n initially */LinkedQueue(queueNode n){

front = n;rear = n;

}

/* Inserts an item onto the queue */public void enqueue(Object item){

queueNode n = new queueNode(item, null);if (front == null) {

front = n;rear = n;

} else {rear.link = n;rear = n;

}}

Page 13: MELJUN CORTES Jedi slides data st-chapter03-queues

13Data Structures – Queues

Linked RepresentationJava Implementation

– /* Deletes an item from the queue */ public Object dequeue() throws QueueException{

Object x; if (front == null) throw new QueueException

("Deleting from an empty queue."); x = front.info; front = front.link; return x;

} }

Page 14: MELJUN CORTES Jedi slides data st-chapter03-queues

14Data Structures – Queues

Application: Topological SortingIntroduction

● A problem involving activity networks● Uses both sequential and link allocation techniques in which

the linked queue is embedded in a sequential vector● Uses simple techniques to reduce time complexity

– e.g. the use of pre-allocated memory to avoid unnecessary passes through a structure

● Applied to the elements of a set on which partial order is defined

Page 15: MELJUN CORTES Jedi slides data st-chapter03-queues

15Data Structures – Queues

Application: Topological SortingPartial Order

● Partial Order– A set S, has a partial ordering of elements, if there’s a

relation among its elements, denoted by the symbol , read as “precedes or equals”, satisfying the following properties for any elements x, y and z:

● Partial ordering properties of ≼:– Reflexivity : x ≼ x– Antisymmetry : if x ≼ y and y ≼ x, then x = y– Transitivity : if x ≼ y and y ≼ z, then x ≼ z

● Corollaries. If x ≼ y and x ≠ y then x ≺ y. Equivalently,

– Irreflexivity : x ≺ x– Asymmetry : if x ≺ y then y ≺ x– Transitivity : if x ≺ y and y ≺ z, then x ≺ z

Page 16: MELJUN CORTES Jedi slides data st-chapter03-queues

16Data Structures – Queues

Application: Topological SortingThe Problem

Arrange the objects in a linear sequence such that no object

appears in the sequence before its direct predecessor(s)

Page 17: MELJUN CORTES Jedi slides data st-chapter03-queues

17Data Structures – Queues

Application: Topological Sorting

● 0,1● 0,3● 0,5● 1,2● 1,5● 2,4● 3,2● 3,4● 5,4● 6,5● 6,7● 7,1 Output: 0 6 3 7 1 2 5 4● 7,5

Page 18: MELJUN CORTES Jedi slides data st-chapter03-queues

18Data Structures – Queues

Application: Topological Sorting

Page 19: MELJUN CORTES Jedi slides data st-chapter03-queues

19Data Structures – Queues

Application: Topological SortingThe Solution

● Input - partially ordered elements [ (i,j) for partial order i j ]– Input can be in any order

● Output - list of elements in which there is no element listed with its predecessor not yet in the output

● Algorithm proper: – Keep COUNT of direct predecessor for the objects. If this count=0,

the object is ready for output● COUNT is a vector of type integer

– Once an object is placed in the output, its direct successors are located, through a LIST, to decrease their direct predecessor count

● LIST is a vector that contains pointers to singly-linked list of each element having node structure (SUC, NEXT)

Page 20: MELJUN CORTES Jedi slides data st-chapter03-queues

20Data Structures – Queues

● Algorithm proper (cont): – COUNT is initialized to 0 while LIST to null– If partial order (i, j) arrives,

● Increment count of j ● Add j to the list of successors of i

– To generate the output:1. Look for an item, say k, with count of direct predecessors equal to zero, i.e.,

COUNT[k] = 0. Put k in the output2. Decrement the count of each such successor by 13. Repeat steps 1 and 2 until no more items can be placed in the output

Application: Topological SortingThe Solution

Page 21: MELJUN CORTES Jedi slides data st-chapter03-queues

21Data Structures – Queues

● A linked queue could be used to avoid having to go through the COUNT vector repeatedly to look for objects with a count of zero– QLINK[j] = k if k is the next item in the queue

= 0 if j is the rear element in the queue– COUNT of each item j in the queue could be reused as a link field

● Remarks:– If the input satisfies partial ordering, then the algorithm will terminate when the

queue is empty– If a loop exists, it will also terminate but will not output the elements in the

loop. Instead, it will just inform the user that a loop is present

Application: Topological SortingThe Solution

Page 22: MELJUN CORTES Jedi slides data st-chapter03-queues

22Data Structures – Queues

Summary● A queue is a linearly ordered set of elements obeying the first-in,

first-out (FIFO) principle ● Two basic queue operations are insertion at the rear and deletion

at the front● Queues have 2 implementations - sequential and linked● In circular queues, there is no need to move the elements to

make room for insertion● The topological sorting approach discussed uses both sequential

and linked allocation techniques, as well as a linked queue embedded in a sequential vector