Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithm and Data Structure Part II Dr. Naimah Yaakob

Similar presentations


Presentation on theme: "Algorithm and Data Structure Part II Dr. Naimah Yaakob"— Presentation transcript:

1 Algorithm and Data Structure Part II Dr. Naimah Yaakob
EKT 334 Algorithm and Data Structure Chapter 5 – Queues Part II Dr. Naimah Yaakob

2 Circular queues Used to rectify the limitations in linear queue
As the name indicates a circular queue is not linear in structure but instead circular. In other words, the FRONT and REAR variables which displayed a linear (left to right) movement over a queue, display a circular movement (clock wise) over the queue data structure.

3 C moves towards the front

4 REAR FRONT ak+n a1 ak+l a2 ... ... ak+2 ... ak+1 ak Fig. 5.5: circular MOVEMENT of FRONT and REAR variables in a circular queue

5

6 INSERT OPERATION.. Algorithm 5.3: Implementation of insert operation on a circular queue procedure INSERT_CIRCQ(CIRC_Q, FRONT,REAR, n, ITEM) REAR=(REAR + 1) mod n; If (FRONT = REAR) then CIRCQ_FULL; /* Here CIRCQ_FULL tests for the queue full condition and if so, retracts REAR to its previous value*/ CIRC_Q [REAR]= ITEM; end INSERT_CIRCQ.

7 DELETE OPERATION.. 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; ITEM = CIRC_Q [FRONT]; end DELETE_CIRCQ

8 Priority queues A priority queue is a queue in which insertion or deletion of items from any position in the queue are done based on some property (such as priority of task) A common method of implementation of a priority queue is to open as many queues as there are priority factors. A low priority queue will be operated for deletion (service) only when all its high priority predecessors are empty. Let P be a priority queue with three different elements a,b,c, whose priority factors are 2,1,1. The higher the number, the higher is the priority. When a new element d with priority 4 is inserted, d joins at the head (front) of the queue superceding the remaining elements. The same priority elements follow the FIFO or FCFS.

9 A Scenario... A file of patients waiting for their turn in a queue to have an appointment with a doctor. All patients are recorded equal priority and follow the FCFS by appointment. Suppose however, when a patient with bleeding injuries is brought in, he/she is accorded high priority and is immediately moved to the head of the queue for immediate attention by the doctor. This is priority queue at work. A low priority queue will be operated for deletion (service) only when all its high priority predecessors are empty. i.e: The deletion of an element in a priority queue Q1 with priority Pi is possible only when those queues Qj with priorities Pj (Pj>Pi) are empty. ***However, the insertion still obeys the FIFO scheme with regards to the q1 alone. Another method of implementation could be to sort the elements in the queue according to the descending order of priorities every time an insertion takes place. The top priority element at the head of the queue is the one to be deleted.

10 Performance Trade-off
While the first method (cluster (group) of queue) consumes space, the time complexity for this method is only O(1). - in the case of deletion of an element in a specific queue with a specific priority, it calls for the checking of all other queues preceding it in priority, to be empty. The second method (sorting) consumes less space since it handles just a single queue. However, the insertion of every element, calls for sorting all the queue elements in the descending order, the most efficient of which report a time complexity of O(n.logn). High High Medium Low Let say we want to delete an item from a low-priority queue, we need to check all other higher-priority queues to ensure they are empty High Low Delete Sorted-Priority Queue

11

12 Same priority: FCFS

13

14

15 PRIORITY QUEUE AS AN ARRAY...
Rows : Number of priorities accorded to the data elements Columns : Maximum number of elements that can be accommodated in the queues corresponding to the priority queues If PRIO_QUE[1:m, 1:n] is an array representing a priority queue; Then, The priority numbers are ranging from 1 to m Maximum number of elements is n E.g: [1:3, 1:2]

16 Deques A deque (double ended queue) is a linear list in which all insertions and deletions are made at the end of the list. A deque is therefore more general than a stack or queue and is a sort of FLIFLO (first in last in or first out last out). Thus while one speaks of the top or bottom of a stack, or front or rear of a queue, one refers to the right end or left end of a deque.

17 A Comparison: Stack, queue, dequeue


Download ppt "Algorithm and Data Structure Part II Dr. Naimah Yaakob"

Similar presentations


Ads by Google