MELJUN_CORTES_JEDI Course Notes Data Structures Students Manual
MELJUN_CORTES_JEDI Slides Data Structures Chapter03 Queues
-
Upload
meljun-cortes-mbampa -
Category
Documents
-
view
234 -
download
0
Transcript of MELJUN_CORTES_JEDI Slides Data Structures Chapter03 Queues
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter03 Queues
1/22
1Data Structures Queues
3 Queues
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter03 Queues
2/22
2Data Structures Queues
Objectives
At the end of the lesson, the student should be able to:
Define the basic concepts and operations on the ADTqueue
Implement the ADT queue using sequential and linkedrepresentation
Perform operations on circular queue
Use topological sorting in producing an order of elements
satisfying a given partial order
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter03 Queues
3/22
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)
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter03 Queues
4/22
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);
}
}
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter03 Queues
5/22
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
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter03 Queues
6/22
6Data Structures Queues
Sequential Representation
Java 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++;
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter03 Queues
7/22
7Data Structures Queues
Sequential Representation
Java 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++;
}
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter03 Queues
8/22
8Data Structures Queues
Sequential Representation
Java 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 forfuture insertions */
void moveQueue() throws QueueException{if (front==0) throw new QueueException("Inserting
into a full queue");
for(int i=front; i
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter03 Queues
9/22
9Data Structures Queues
Circular Queue
Cells are considered arrangedin a circle
front points to the actualelement at the front of thequeue
rear points to the cell on theright of the actual rearelement
Full queue always has oneunused cell
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter03 Queues
10/22
10Data Structures Queues
Circular Queue
Java Implementation Initialization: front = 0, rear = 0
Empty queue: iffront == rear
Full queue: iffront == (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;
}
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter03 Queues
11/22
11Data Structures Queues
Linked Representation
Linked Representation
Queue is empty if front = null
Overflow will happen only when the program runs out of memory
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter03 Queues
12/22
12Data Structures Queues
Linked Representation
Java 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;
}}
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter03 Queues
13/22
13Data Structures Queues
Linked Representation
Java 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;
}
}
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter03 Queues
14/22
14Data Structures Queues
Application: Topological SortingIntroduction
A problem involving activity networks
Uses both sequential and link allocation techniques in whichthe 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 isdefined
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter03 Queues
15/22
15Data Structures Queues
Application: Topological SortingPartial Order
Partial Order
A set S, has a partial ordering of elements, if theres arelation among its elements, denoted by the symbol ,read as precedes or equals, satisfying the followingproperties 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
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter03 Queues
16/22
16Data Structures Queues
Application: Topological SortingThe Problem
Arrange the objects in a linear
sequence such that no objectappears in the sequence before
its direct predecessor(s)
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter03 Queues
17/22
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
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter03 Queues
18/22
18Data Structures Queues
Application:
Topological Sorting
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter03 Queues
19/22
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 listedwith 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 arelocated, through a LIST, to decrease their direct predecessor count
LIST is a vector that contains pointers to singly-linked list of each element havingnode structure (SUC, NEXT)
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter03 Queues
20/22
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 output
2. Decrement the count of each such successor by 1
3. Repeat steps 1 and 2 until no more items can be placed in the output
Application: Topological SortingThe Solution
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter03 Queues
21/22
21Data Structures Queues
A linked queue could be used to avoid having to go through the COUNTvector 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
-
8/3/2019 MELJUN_CORTES_JEDI Slides Data Structures Chapter03 Queues
22/22
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 areinsertion at the rear and deletionat the front
Queues have 2 implementations - sequential and linked
In circular queues, there is no need to move the elements tomake room for insertion
The topological sorting approach discussed uses both sequential
and linked allocation techniques, as well as a linked queueembedded in a sequential vector