MELJUN CORTES Jedi slides data st-chapter03-queues


Published on

MELJUN CORTES Jedi slides data st-chapter03-queues

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

MELJUN CORTES Jedi slides data st-chapter03-queues

  1. 1. 3 Queues Data Structures – Queues 1
  2. 2. 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 Data Structures – Queues 2
  3. 3. 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) Data Structures – Queues 3
  4. 4. 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); } } Data Structures – Queues 4
  5. 5. 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 Data Structures – Queues 5
  6. 6. 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++; Data Structures – Queues 6
  7. 7. 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++; } Data Structures – Queues 7
  8. 8. 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” 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; } } Data Structures – Queues 8
  9. 9. 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 Data Structures – Queues 9
  10. 10. Circular Queue Java 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; } Data Structures – Queues 10
  11. 11. Linked Representation● Linked Representation – Queue is empty if front = null – Overflow will happen only when the program runs out of memory Data Structures – Queues 11
  12. 12. 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 { = n; rear = n; } } Data Structures – Queues 12●
  13. 13. 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 =; return x; } } –• Data Structures – Queues 13
  14. 14. Application: Topological Sorting Introduction● 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 Data Structures – Queues 14
  15. 15. Application: Topological Sorting Partial 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 Data Structures – Queues 15
  16. 16. Application: Topological Sorting The ProblemArrange the objects in a linear sequence such that no objectappears in the sequence before its direct predecessor(s) Data Structures – Queues 16
  17. 17. Application:● 0,1 Topological Sorting● 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 Data Structures – Queues 17
  18. 18. Application: Topological Sorting● Data Structures – Queues 18
  19. 19. Application: Topological Sorting The 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) Data Structures – Queues 19
  20. 20. Application: Topological Sorting The Solution● 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 Data Structures – Queues 20
  21. 21. Application: Topological Sorting The Solution● 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 Data Structures – Queues 21
  22. 22. 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 Data Structures – Queues 22