GOVERNMENT ARTS COLLEGE
Melur, Madurai
By
Mr. V. VEERANAN
Roll No: P22PCS123
I M.Sc. Computer Science
PG. Department of Computer Science
Topic: Priority Queue
DATA STRUTURES AND ALGORITHMS
Queue
Simple Priority
Circular Deque
Enqueue-rear
Dequeue-Front
FIFO
Ex. Pipes
Last Member is
linked to the
first
Ex. Week
Predefined
Priority of
Service
Ex. high priority
Enqueue and
Dequeue
both the ends
and can remove
an item from
both the ends
Types of
Priority Queue
Implementation
of Priority Queue
Ascending
Descending
Array
Linked
List
Heap
Binary
Search
Tree
Priority
Ascending
Descending
Highest Priority to Lowest Number
Highest Priority to Highest Number
Implementation of Priority Queue
Array Linked List Heap
Binary Search
Tree
enqueue()
dequeue()
peek() / top()
push()
pop(0
peek() / top ()
insert()
extract Max()
remove(i)
get Max()
change
priority()
peek()
insert()
delete()
Any Doubt’s
Mutiple Choice Questions
Three Groups
C C++ Java
Table of the Content
S.No. Topic
1 Queue & Types of Queue
2 Simple Queue
3 Circular Queue
4 Priority Queue
5 Double Ended Queue (Deque)
6 Types of Priority Queue
7 Ascending Order Priority Queue
8 Descending Order Priority Queue
9 Implementation of Priority Queue
10 Implement Priority Queue Using Array
11 Implement Priority Queue Using Linked List
12 Implement Priority Queue Using Heap
13 Implement Priority Queue Using Binary Search Tree
14 Priority Queue Applications
Queues
 In computer science, a queue is a collection of entities that
are maintained in a sequence and can be modified by the
addition of entities at one end of the sequence and the
removal of entities from the other end of the sequence
 In this article, we’ll learn four types of queues with their
applications. Understand them one-by-one with an
illustration.
Types of Queues
 Simple Queue
 Circular Queue
 Priority Queue
 Double Ended Queue (Deque)
Queue Example
Simple Queue
• A simple queue is the most basic queue. In this queue, the
enqueue operation takes place at the rear, while the dequeue
operation takes place at the front.
• Its applications are process scheduling, disk scheduling,
memory management, IO buffer, pipes, call center phone
systems and interrupt handling.
Circular Queue
A circular queue is a special case of a simple queue in
which the last member is linked to the first. As a result, a circle-
like structure is formed.
 The last node is connected to the first node.
 Also known as a Ring Buffer as the nodes are connected end
to end.
 Insertion takes place at the front of the queue and deletion at
the end of the queue.
 Application of circular queue: Insertion of days in a week.
Priority Queue
• A priority queue is a special kind of queue in which each
item has a predefined priority of service.
• In this queue, the enqueue operation takes place at the rear in
the order of arrival of the items, while the dequeue operation
takes place at the front based on the priority of the items.
• That is to say that an item with a high priority will be
dequeued before an item with a low priority.
Priority Queue (Continue)
 In the case, when two or more items have the same priority,
then they’ll be dequeued in the order of their arrival. Hence,
it may or may not strictly follow the FIFO rule:
 It’s used in interrupt handling, Prim’s algorithm, Dijkstra’s
algorithm, A* search algorithm, heap sort, and Huffman
code generation.
Double-Ended Queue (Deque)
 A deque is also a special type of queue. In this queue, the
enqueue and dequeue operations take place at both front
and rear. That means, we can insert an item at both the ends
and can remove an item from both the ends. Thus, it may or
may not adhere to the FIFO order:
Types of Priority Queue
A priority queue is of two types:
 Ascending Order Priority Queue
 Descending Order Priority Queue
Ascending Order Priority Queue
 An ascending order priority queue gives the highest priority
to the lower number in that queue.
 For example, you have six numbers in the priority queue that
are 4, 8, 12, 45, 35, 20. Firstly, you will arrange these numbers
in ascending order.
 The new list is as follows: 4, 8, 12, 20. 35, 45. In this list, 4 is the
smallest number.
 Hence, the ascending order priority queue treats number 4 as
the highest priority.
Ascending Order Priority Queue
Example
 In the above table, 4 has the highest priority, and 45 has the
lowest priority.
4 8 12 20 35 45
Descending Order Priority Queue
 A descending order priority queue gives the highest priority
to the highest number in that queue.
 For example, you have six numbers in the priority queue that
are 4, 8, 12, 45, 35, 20. Firstly, you will arrange these numbers
in ascending order.
 The new list is as follows: 45, 35, 20, 12, 8, 4. In this list, 45 is
the highest number. Hence, the descending order priority
queue treats number 45 as the highest priority.
Descending Order Priority Queue
 In the above table, 4 has the lowest priority, and 45 has the
highest priority.
45 35 20 12 8 4
Implement Priority Queue
Priority queue can be implemented using the following data
structures:
 Arrays
 Linked list
 Heap data structure
 Binary search tree
Implement Priority Queue Using Array
A simple implementation is to use an array of the following
structure.
struct item
{
int item;
int priority;
}
Implement Priority Queue Using Array
• enqueue(): This function is used to insert new data into the
queue.
• dequeue(): This function removes the element with the
highest priority from the queue.
• peek()/top(): This function is used to get the highest priority
element in the queue without removing it from the queue.
Link: https://www.geeksforgeeks.org/priority-queue-using-
array-in-c/
Arrays enqueue() dequeue() peek()
Time
Complexity
O(1) O(n) O(n)
Implement Priority Queue Using Linked List
 push(): This function is used to insert new data into the
queue.
 pop(): This function removes the element with the highest
priority from the queue.
 peek() / top(): This function is used to get the highest priority
element in the queue without removing it from the queue.
Linked List push() pop() peek()
Time
Complexity
O(n) O(1) O(1)
Implement Priority Queue Using Heaps
 insert(p): Inserts a new element with priority p.
 extractMax(): Extracts an element with maximum priority.
 remove(i): Removes an element pointed by an iterator i.
 getMax(): Returns an element with maximum priority.
 changePriority(i, p): Changes the priority of an element
pointed by i to p.
Binary Heap insert() remove() peek()
Time
Complexity
O(log n) O(log n) O(1)
Implement Priority Queue Using
Binary Search Tree
 A Self-Balancing Binary Search Tree like AVL Tree, Red-
Black Tree, etc. can also be used to implement a priority
queue.
 Operations like peek(), insert() and delete() can be performed
using BST.
Binary Search
Tree
peek() insert() delete()
Time
Complexity
O(1) O(log n) O(log n)
Priority Queue Applications
 CPU Scheduling
 Graph algorithms like Dijkstra’s shortest path algorithm,
Prim’s Minimum Spanning Tree, etc.
 Stack Implementation
 All queue applications where priority is involved.
 Data compression in Huffman code
 Event-driven simulation such as customers waiting in a
queue.
 Finding Kth largest/smallest element.
Mr. V.Veeranan, M.Sc. Computer Science, Dip. in Yoga

GAC DS Priority Queue Presentation 2022.ppt

  • 1.
    GOVERNMENT ARTS COLLEGE Melur,Madurai By Mr. V. VEERANAN Roll No: P22PCS123 I M.Sc. Computer Science PG. Department of Computer Science Topic: Priority Queue DATA STRUTURES AND ALGORITHMS
  • 2.
    Queue Simple Priority Circular Deque Enqueue-rear Dequeue-Front FIFO Ex.Pipes Last Member is linked to the first Ex. Week Predefined Priority of Service Ex. high priority Enqueue and Dequeue both the ends and can remove an item from both the ends Types of Priority Queue Implementation of Priority Queue Ascending Descending Array Linked List Heap Binary Search Tree
  • 3.
    Priority Ascending Descending Highest Priority toLowest Number Highest Priority to Highest Number Implementation of Priority Queue Array Linked List Heap Binary Search Tree enqueue() dequeue() peek() / top() push() pop(0 peek() / top () insert() extract Max() remove(i) get Max() change priority() peek() insert() delete()
  • 4.
    Any Doubt’s Mutiple ChoiceQuestions Three Groups C C++ Java
  • 5.
    Table of theContent S.No. Topic 1 Queue & Types of Queue 2 Simple Queue 3 Circular Queue 4 Priority Queue 5 Double Ended Queue (Deque) 6 Types of Priority Queue 7 Ascending Order Priority Queue 8 Descending Order Priority Queue 9 Implementation of Priority Queue 10 Implement Priority Queue Using Array 11 Implement Priority Queue Using Linked List 12 Implement Priority Queue Using Heap 13 Implement Priority Queue Using Binary Search Tree 14 Priority Queue Applications
  • 6.
    Queues  In computerscience, a queue is a collection of entities that are maintained in a sequence and can be modified by the addition of entities at one end of the sequence and the removal of entities from the other end of the sequence  In this article, we’ll learn four types of queues with their applications. Understand them one-by-one with an illustration. Types of Queues  Simple Queue  Circular Queue  Priority Queue  Double Ended Queue (Deque)
  • 7.
  • 8.
    Simple Queue • Asimple queue is the most basic queue. In this queue, the enqueue operation takes place at the rear, while the dequeue operation takes place at the front. • Its applications are process scheduling, disk scheduling, memory management, IO buffer, pipes, call center phone systems and interrupt handling.
  • 9.
    Circular Queue A circularqueue is a special case of a simple queue in which the last member is linked to the first. As a result, a circle- like structure is formed.  The last node is connected to the first node.  Also known as a Ring Buffer as the nodes are connected end to end.  Insertion takes place at the front of the queue and deletion at the end of the queue.  Application of circular queue: Insertion of days in a week.
  • 10.
    Priority Queue • Apriority queue is a special kind of queue in which each item has a predefined priority of service. • In this queue, the enqueue operation takes place at the rear in the order of arrival of the items, while the dequeue operation takes place at the front based on the priority of the items. • That is to say that an item with a high priority will be dequeued before an item with a low priority.
  • 11.
    Priority Queue (Continue) In the case, when two or more items have the same priority, then they’ll be dequeued in the order of their arrival. Hence, it may or may not strictly follow the FIFO rule:  It’s used in interrupt handling, Prim’s algorithm, Dijkstra’s algorithm, A* search algorithm, heap sort, and Huffman code generation.
  • 12.
    Double-Ended Queue (Deque) A deque is also a special type of queue. In this queue, the enqueue and dequeue operations take place at both front and rear. That means, we can insert an item at both the ends and can remove an item from both the ends. Thus, it may or may not adhere to the FIFO order:
  • 13.
    Types of PriorityQueue A priority queue is of two types:  Ascending Order Priority Queue  Descending Order Priority Queue
  • 14.
    Ascending Order PriorityQueue  An ascending order priority queue gives the highest priority to the lower number in that queue.  For example, you have six numbers in the priority queue that are 4, 8, 12, 45, 35, 20. Firstly, you will arrange these numbers in ascending order.  The new list is as follows: 4, 8, 12, 20. 35, 45. In this list, 4 is the smallest number.  Hence, the ascending order priority queue treats number 4 as the highest priority.
  • 15.
    Ascending Order PriorityQueue Example  In the above table, 4 has the highest priority, and 45 has the lowest priority. 4 8 12 20 35 45
  • 16.
    Descending Order PriorityQueue  A descending order priority queue gives the highest priority to the highest number in that queue.  For example, you have six numbers in the priority queue that are 4, 8, 12, 45, 35, 20. Firstly, you will arrange these numbers in ascending order.  The new list is as follows: 45, 35, 20, 12, 8, 4. In this list, 45 is the highest number. Hence, the descending order priority queue treats number 45 as the highest priority.
  • 17.
    Descending Order PriorityQueue  In the above table, 4 has the lowest priority, and 45 has the highest priority. 45 35 20 12 8 4
  • 18.
    Implement Priority Queue Priorityqueue can be implemented using the following data structures:  Arrays  Linked list  Heap data structure  Binary search tree
  • 19.
    Implement Priority QueueUsing Array A simple implementation is to use an array of the following structure. struct item { int item; int priority; }
  • 20.
    Implement Priority QueueUsing Array • enqueue(): This function is used to insert new data into the queue. • dequeue(): This function removes the element with the highest priority from the queue. • peek()/top(): This function is used to get the highest priority element in the queue without removing it from the queue. Link: https://www.geeksforgeeks.org/priority-queue-using- array-in-c/ Arrays enqueue() dequeue() peek() Time Complexity O(1) O(n) O(n)
  • 21.
    Implement Priority QueueUsing Linked List  push(): This function is used to insert new data into the queue.  pop(): This function removes the element with the highest priority from the queue.  peek() / top(): This function is used to get the highest priority element in the queue without removing it from the queue. Linked List push() pop() peek() Time Complexity O(n) O(1) O(1)
  • 22.
    Implement Priority QueueUsing Heaps  insert(p): Inserts a new element with priority p.  extractMax(): Extracts an element with maximum priority.  remove(i): Removes an element pointed by an iterator i.  getMax(): Returns an element with maximum priority.  changePriority(i, p): Changes the priority of an element pointed by i to p. Binary Heap insert() remove() peek() Time Complexity O(log n) O(log n) O(1)
  • 23.
    Implement Priority QueueUsing Binary Search Tree  A Self-Balancing Binary Search Tree like AVL Tree, Red- Black Tree, etc. can also be used to implement a priority queue.  Operations like peek(), insert() and delete() can be performed using BST. Binary Search Tree peek() insert() delete() Time Complexity O(1) O(log n) O(log n)
  • 24.
    Priority Queue Applications CPU Scheduling  Graph algorithms like Dijkstra’s shortest path algorithm, Prim’s Minimum Spanning Tree, etc.  Stack Implementation  All queue applications where priority is involved.  Data compression in Huffman code  Event-driven simulation such as customers waiting in a queue.  Finding Kth largest/smallest element.
  • 25.
    Mr. V.Veeranan, M.Sc.Computer Science, Dip. in Yoga