0% found this document useful (0 votes)
16 views

In The Name of Allah The Most Beneficent The Most Merciful: Subject: Data Structures & Algorithms

Priority queues are a special type of queue where each element has an associated priority and elements are served according to their priority. If elements have the same priority, they are served in the order they are inserted into the queue. Deques, or double-ended queues, allow insertion and removal of elements from both ends of the queue. They can behave like stacks or queues depending on how elements are added and removed. Priority queues and deques have applications in areas like scheduling, graph algorithms, simulations, and undo/redo operations.

Uploaded by

Abdullah Jutt
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

In The Name of Allah The Most Beneficent The Most Merciful: Subject: Data Structures & Algorithms

Priority queues are a special type of queue where each element has an associated priority and elements are served according to their priority. If elements have the same priority, they are served in the order they are inserted into the queue. Deques, or double-ended queues, allow insertion and removal of elements from both ends of the queue. They can behave like stacks or queues depending on how elements are added and removed. Priority queues and deques have applications in areas like scheduling, graph algorithms, simulations, and undo/redo operations.

Uploaded by

Abdullah Jutt
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 17

In the Name of Allah the Most

Beneficent the Most Merciful

Subject : Data Structures & Algorithms

Lecture : 14

Monday, January 18, 20 1


21
Priority Queue

• A priority queue is a special type of queue in


which each element is associated with a priority
and is served according to its priority. If
elements with the same priority occur, they are
served according to their order in the queue.

AL 2
Priority Queue
Priority Queue is an extension of queue with
following properties.

1.Every item has a priority associated with it.

2.An element with high priority is dequeued before an


element with low priority.

3.If two elements have the same priority, they are served
according to their order in the queue.
AL 3
Priority Queue
 Idea behind the Priority Queue is simple:
 It is a queue
 But the items are ordered by a key.
 Implies your ‘position’ in the queue may be
changed by the arrival of a new item.

 Note: a priority queue is no longer FIFO!


 You will still remove from front of queue, but
insertions are governed by a priority.
AL 4
Priority Queue
#include "Event.cpp"
#define PQMAX 30

class PriorityQueue {
public:
PriorityQueue() {
size = 0; rear = -1;
};
~PriorityQueue() {};

int full(void)
{
return ( size == PQMAX ) ? 1 : 0;
};
Priority Queue
Event* remove()
{
if( size > 0 ) {
Event* e = nodes[0];
for(int j=0; j < size-2; j++ )
nodes[j] = nodes[j+1];

size = size-1; rear=rear-1;


if( size == 0 ) rear = -1;

return e;
}
return (Event*)NULL;
cout << "remove - queue is empty." << endl;
};
Priority Queue
int insert(Event* e)
{
if( !full() ) {
rear = rear+1;
nodes[rear] = e;
size = size + 1;
sortElements(); // in ascending order
return 1;
}
cout << "insert queue is full." << endl;
return 0;
};

int length() { return size; };


};
Applications of Priority Queue

 CPU Scheduling

 Graph algorithms like Dijkstra’s shortest path


algorithm, Prim’s Minimum Spanning Tree, etc

 In Simulators
 Scheduling the next event (smallest event time)

 All queue applications where priority is involved.

AL 8
Comparing queue implementations
Big-O Comparison of Queue Operations
Operation Array Linked
Implementation Implementation
Class constructor O(1) O(1)
Front O(1) O(1)
IsFull O(1) O(1)
IsEmpty O(1) O(1)
Enqueue O(1) O(1)
Dequeue O(1) O(1)
Destructor O(1) O(N)
Double-ended queue
• A deque, also known as a double-ended queue,
is an ordered collection of items similar to the
queue. It has two ends, a front, and a rear, and
the items remain positioned in the collection. 
• In a sense, this hybrid linear structure provides
all the capabilities of stacks and queues in a
single data structure.

• Allows insertion and removal of elements from


both the ends, i.e. , front and back.
AL 10
Double-ended queue
• New items can be added at either the front or the
rear. Likewise, existing items can be removed
from either end.

• It is important to note that even though the


deque can assume many of the characteristics of
stacks and queues, it does not require the LIFO
and FIFO orderings that are enforced by those
data structures. It is up to you to make consistent
use of the addition and removal operations. 11
Double-ended queue

AL 12
Implementation of Double ended Queue

• insert_rear() : inserts element at back


• insert_front() : inserts element at front
• delete_rear() : removes last element
• delete_front() : removes first element
• get_rear() : returns last element
• get_front() : returns first element
• Empty() : returns true if queue is empty
• Full() : returns true if queue is full
AL 13
Types of Double ended Queue (Deque)

• Input restricted deque


– Elements can be inserted only at one end.
– Elements can be removed from both the
ends.

• Output restricted deque


– Elements can be removed only at one end.
– Elements can be inserted from both the ends.
AL 14
Deque as stack and queue

• As Stack
– When insertion and deletion is made at the
same side.

• As Queue
– When items are inserted at one end and
removed at other end.

AL 15
Applications of Deque

• A-Steal job scheduling algorithm


– The A-steal algorithm implements task scheduling for
several processors (multiprocessor scheduling).
– The processor gets the first element from the deque.
– When one of the processor complete the execution of
its own threads it can steal a thread from another
processor.
– It gets a last element from the deque of another
processor and executes it.
AL 16
Applications of Deque…

• A Undo-Redo operations in software applications.


– Storing a software application's list of undo
operations.
• A web browser's history.
– Recently visited URLs are added to the front of the
deque, and the URL at the back of the deque is
removed after some specified number of insertions at
the front.

AL 17

You might also like