0% found this document useful (0 votes)
16 views15 pages

Ds13-Priority Queue

The document provides an overview of priority queues, detailing their structure, implementation using arrays, and various applications such as Huffman coding, Dijkstra's algorithm, and CPU scheduling. It also discusses the challenges of multiple queues and their representation in a single array. Additionally, the document highlights the direct and indirect applications of queues in real-world scenarios, including simulations for customer service optimization.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views15 pages

Ds13-Priority Queue

The document provides an overview of priority queues, detailing their structure, implementation using arrays, and various applications such as Huffman coding, Dijkstra's algorithm, and CPU scheduling. It also discusses the challenges of multiple queues and their representation in a single array. Additionally, the document highlights the direct and indirect applications of queues in real-world scenarios, including simulations for customer service optimization.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

DATA STRUCTURES

PRIORITY QUEUE AND APPLICATIONS OF QUEUE


OBJECTIVE

“ To study priority queue and applications of queue .



01 Introduction to Priority Queue

02
Array Implementation of Priority queue

03
03
Applications of Priority Queue

04 Multiple Queue AAPP

05 AAPP
Applications of Queue

06
05
PRIORITY QUEUE

A priority queue is a data structure in which each element is assigned a


priority. The priority of the element will be used to determine the order in
which the elements will be processed.

The general rules of processing the elements of a priority queue are


⚫ An element with higher priority is processed before an element with a

lower priority.
⚫ Two elements with the same priority are processed on a first-come-

first-served (FCFS) basis.


Array Representation of a Priority Queue
⚫ When arrays are used to implement a priority queue, then a separate queue for each priority
number is maintained. Each of these queues will be implemented using circular arrays or
circular queues.
⚫ Every individual queue will have its own FRONT and REAR pointers.
⚫ We use a two-dimensional array for this purpose where each queue will be allocated the same
amount of space.
2D Array representation of a priority queue
⚫ FRONT[K] and REAR[K] contain the front and rear values of row K , where K is the priority
number.
⚫ To insert a new element with priority K in the priority queue, add the element at the rear end of
row K , where K is the row number as well as the priority number of that element.
⚫ To delete an element, we find the first non- empty queue and then process the front element of
the first non-empty queue. In our priority queue, the first non-empty queue is the one with priority
number 1 and the front element is A , so A will be deleted and processed first. In technical terms,
find the element with the smallest K , such that FRONT[K] != NULL .
Example: Suppose you have a few assignment from different
subjects. Which assignment will you want to do first?

subjects Due date priority


DSGT 15 OCT 4
DLCA 6 OCT 2
DS 4 OCT 1
Priority Queues: Some Applications
• Huffman Codes
• Huffman Codes are used to compress a block of data into a smaller space.
• The algorithm starts by collecting the frequencies of all the characters in the data block and then processes ea
ch one in order of descending frequency - a perfect place to use a Priority Queue.

• Dijkstra's Algorithm for All Shortest Paths


• This graph algorithm always selects the next connected edge of lowest path cost from the starting node. Thes
e edges can be stored and retrieved in a Priority Queue.

• Prim's Algorithm
• Prim's is another graph algorithm which can utilize a Priority Queue. It works by always selecting the next con
nected edge of lowest path cost.

• CPU Scheduling
• A CPU can only run one process at a time, but there may be many jobs of various priorities waiting to be run.
• A Priority Queue can be used to quickly select the next process to run based upon its priority.
CPU Scheduling

The priority of the process may be set based on the CPU time it requires to get executed
completely. For example, if there are three processes, where the first process needs 5 ns to
complete, the second process needs 4 ns, and the third process needs 7 ns, then the second
process will have the highest priority and will thus be the first to be executed.

However, CPU time is not the only factor that determines the priority, rather it is just one among
several factors. Another factor is the importance of one process over another. In case we have to
run two processes at the same time, where one process is concerned with online order booking
Queues and the second with printing of stock details, then obviously the online booking is more
important and must be executed first.
Multiple Queues
Problems with the queues represented with arrays are:
• We have to allocate large space to avoid the overflow.
• Large space will be wasted due to less size of queue.
There exits a trade-off between the number of overflows and the space.
• One possibility to reduce this tradeoff is to represent more than one
queue in the same array of sufficient size

f[1] r[1] f[2] r[2]f[3] r[3]f[4] r[4]f[5]


r[5]
Queue1 Queue2 Queue3 Queue4 Queue5
Representation of five queues by a single array
Application of Queues
• Direct applications
– Waiting lines: Queues are commonly used in systems where waiting line has to be m
aintained for obtaining access to a resource. For example:
• an operating system may keep a queue of processes that are waiting to run on th
e CPU.
• Access to shared resources (e.g., printer)
• Simulation of real-world situations, e.g., determine how many tellers to have in or
der to serve each customer within “reasonable” wait time.
– Multiprogramming
• Indirect applications
– Auxiliary data structure for algorithms
– Component of other data structures
• Example: Tree and Graph Breadth-First traversal
Application of Queues: Simulations
• Problem Statement: To simulate the flow of customers through a checkout line:
– The objective is to try to reduce the number of tellers in a way that, “most
probably”, customers would have to wait a maximum of x minutes before getting
served.

– Assume that:
• Every minute, 0, 1, or 2 customers will need to be served in a checkout line.
• The expected service time for a customer is 1 minute.
• There is one checkout line available.

– Find the number of customers not served, so far, after n minutes of service.
Probable University Questions
• Explain priority queue.
• List the applications of priority queue.
• Write a C program for Array Implementation of a Priority Queue.
Thank you

You might also like