Computer Science

Priority Queue

A priority queue is a data structure that stores a collection of elements and assigns a priority to each element. The element with the highest priority is always at the front of the queue and is the first to be removed. Priority queues are commonly used in algorithms such as Dijkstra's shortest path algorithm and Huffman coding.

Written by Perlego with AI-assistance

7 Key excerpts on "Priority Queue"

  • The Modern C++ Challenge
    eBook - ePub

    The Modern C++ Challenge

    Become an expert programmer by solving real-world problems

    45. Priority Queue

    A Priority Queue is an abstract data type whose elements have a priority attached to them. Instead of working as a first-in-first-out container, a Priority Queue makes elements available in the order of their priority. This data structure is used in algorithms such as Dijkstra's shortest path, Prim's algorithm, heap sort, the A* search algorithm, in Huffman codes used for data compression, and others.
    A very simple approach to implement a Priority Queue would be to use an std::vector as the underlying container of elements and always maintain it sorted. That means the maximum and minimum elements are always at the two ends. However, this approach does not provide the most efficient operations.
    The most suitable data structure that can be used to implement a Priority Queue is a heap. This is a tree-based data structure that satisfies the following property: if P is a parent node of C , then the key (the value) of P is either greater than or equal to (in a max heap) or less than or equal to (in a min heap) the key of C
  • Modern C++: Efficient and Scalable Application Development
    eBook - ePub

    Modern C++: Efficient and Scalable Application Development

    Leverage the modern features of C++ to overcome difficulties in various stages of application development

    • Richard Grimes, Marius Bancila(Authors)
    • 2018(Publication Date)
    • Packt Publishing
      (Publisher)

    45. Priority queue

    A Priority Queue is an abstract data type whose elements have a priority attached to them. Instead of working as a first-in-first-out container, a Priority Queue makes elements available in the order of their priority. This data structure is used in algorithms such as Dijkstra's shortest path, Prim's algorithm, heap sort, the A* search algorithm, in Huffman codes used for data compression, and others.
    A very simple approach to implement a Priority Queue would be to use an std::vector  as the underlying container of elements and always maintain it sorted. That means the maximum and minimum elements are always at the two ends. However, this approach does not provide the most efficient operations.
    The most suitable data structure that can be used to implement a Priority Queue is a heap. This is a tree-based data structure that satisfies the following property: if  P   is a parent node of  C , then the key (the value) of  P   is either greater than or equal to (in a max heap) or less than or equal to (in a min heap) the key of  C
  • Data Structures and Program Design Using Java
    eBook - ePub
    Descending Priority QueueIn this type of Priority Queue, elements can be inserted in any order. But at the time of deletion of elements from the queue, the largest element is searched and deleted first. For example: Operating systems, Routing.
     

    Frequently Asked Questions

    3. Define Priority Queue.
    Ans: A Priority Queue is a collection of elements such that each element has been assigned a priority and such that the order in which elements are deleted and processed comes from the following rules:
    (a) An element of higher priority is processed before any element of lower priority.
    (b) Two elements with same priority are processed according to the order in which they were added to the queue.
    The array elements in a Priority Queue can have the following structure:
    private class Node {
         int priority;
         int data;
         Node next;
         }

    5.5.2.1  Implementation of a Priority Queue

    A Priority Queue can be implemented in two ways:
    1. Array Representation of a Priority Queue
    2. Linked Representation of a Priority Queue
    Let us now discuss both these implementations in detail.
    1. Implementation of a Priority Queue using arrays
    While implementing a Priority Queue using arrays, the following points must be considered:
    • Maintain a separate queue for each level of priority or priority number.
    • Each queue will appear in its own circular array and must have its own pairs of nodes, that is, FRONT AND REAR.
    • If each queue is allocated the same amount of memory, then a 2D array can be used instead of a linear array.
    For example: FRONT [K] and REAR [K] are the nodes containing the front and rear values of row “K” of the queue, where K is the priority number. If we want to insert an element with priority K, then we will add the element at the REAR end of row K; K is the row as well as the priority number of that element. If we add F with priority number 4, then the queue will be given as shown in the following:
    Figure 5.15. Priority Queue after inserting a new element.
    2. Implementation of a Priority Queue using linked lists
    A Priority Queue can be implemented using a linked list. While implementing the Priority Queue using a linked list, every node will have three parts:
  • Data Structures and Program Design Using C++
    Descending Priority Queue – In this type of Priority Queue, elements can be inserted in any order. But at the time of deletion of elements from the queue, the largest element is searched and deleted first. For example – Operating systems, Routing.
    Frequently Asked Questions Q. Define Priority Queue. Answer. A Priority Queue is a collection of elements such that each element has been assigned a priority and such that the order in which elements are deleted and processed comes from the following rules:
    a) An element of higher priority is processed before any element of lower priority.
    b) Two elements with same priority are processed according to the order in which they were added to the queue.
    The array elements in a Priority Queue can have the following structure: struct data { int item ; int priority ; int order ; } ;
    5.5.2.1Implementation of a Priority Queue
    A Priority Queue can be implemented in two ways:
    1. Array Representation of a Priority Queue
    2. Linked Representation of a Priority Queue
    Let us now discuss both these implementations in detail. 1. Implementation of a Priority Queue using arrays While implementing a Priority Queue using arrays, the following points must be considered: •Maintain a separate queue for each level of priority or priority number. •Each queue will appear in its own circular array and must have its own pairs of pointers, that is, FRONT AND REAR. •If each queue is allocated the same amount of memory, then a 2D array can be used instead of a linear array.
    For example – FRONT [K] and REAR [K] are the pointers containing the front and rear values of row “K” of the queue, where K is the priority number. If we want to insert an element with priority K, then we will add the element at the REAR end of row K; K is the row as well as the priority number of that element. If we add F with priority number 4, then the queue will be given as shown in the following:
    FRONT REAR
    2 2
    1 3
    0 0
    5 1
    4 4
    FIGURE 5.15 Priority Queue after inserting a new element.
    2. Implementation of a Priority Queue using linked lists
  • Data Structures and Program Design Using Python
    eBook - ePub
    Descending Priority Queue – In this type of Priority Queue, elements can be inserted in any order. But at the time of the deletion of elements from the queue, the largest element is searched and deleted first (for example – Operating systems, Routing).
    Frequently Asked Questions
    Q. Define Priority Queue.
    Answer:
    A Priority Queue is a collection of elements such that each element has been assigned a priority and such that the order in which elements are deleted and processed comes from the following rules:
    a.
    An element of higher priority is processed before any element of lower priority.
    b.
    Two elements with the same priority are processed according to the order in which they were added to the queue.
    The array elements in a Priority Queue can have the following structure:
    class ListNode :  def __init__( self, data ) : self.priority=priority self.data = data self.next = None
    5.5.2.1 Implementation of a Priority Queue
    A Priority Queue can be implemented in two ways:
    1. Array Representation of a Priority Queue
    2. Linked Representation of a Priority Queue
    Let us now discuss both these implementations in detail.
    1. Implementation of a Priority Queue using arrays
    While implementing a Priority Queue using arrays, the following points must be considered:
    Maintain a separate queue for each level of priority or priority number.
    Each queue will appear in its circular array and must have its pairs of nodes, that is, FRONT AND REAR.
    If each queue is allocated the same amount of memory, then a 2D array can be used instead of a linear array.
    For Example
  • Quick Reference to Data Structures and Computer Algorithms, A
    eBook - ePub
    Here we are taking array for implementing the dequeue. Similarly, as queue it has also 2 operations:
    1. Add
    2. Delete
    We maintain 2 pointers LEFT and RIGHT which indicate the left and right position of the dequeue.

    3.9 Priority Queue

    In Priority Queue every element of queue has some priority and based on that priority it will be processed. So, the element of more priority will be processed before the element which has less priority.
    Suppose 2 elements have same priority then in this case FIFO rule will follow, means the element which is at the first place in the queue will be processed first. In computer implementation, Priority Queue is used in the CPU scheduling algorithm, in which CPU has need to process those processes first which have more priority. In Priority Queue, an element can be inserted or deleted not only at the ends but at any position on the queue.
    Here, an element X of priority Pi may be deleted before an element which is at FRONT, similarly, insertion of an element is based on its priority, that is, instead of adding it after the REAR it may be inserted at an intermediate position dictated by its priority value.
    Note: Priority Queue doesn’t strictly follow first-in -first-out (FIFO) principle which is the basic principle of a queue.
    Two operations going on: Insertion and Deletion. There are various ways of implementing the structure of a Priority Queue. These are as follows:
    (i) Using a simple /circular array
    (ii) Multi-queue implementations
    (iii) Using a double linked list, and
    (iv) Using heap tree
    Linked –list representation of a Priority Queue
    This representation assumes the node structure as the following:
    LLINK and RLINK are two usual link fields DATA is to store the actual content and PRIORITY is to store the priority value of the item. FRONT and REAR are 2 pointers pointing the first and last node in the queue, respectively. Here all the nodes are in sorted order according to the priority values of the items in the nodes. Following is an instance of Priority Queue:
  • Hands-On Data Structures and Algorithms with Python

    7

    Heaps and Priority Queues

    A heap data structure is a tree-based data structure in which each node of the tree has a specific relationship with other nodes, and they are stored in a specific order. Depending upon the specific order of the nodes in the tree, heaps can be of different types, such as a min heap and a max heap.
    A Priority Queue is an important data structure that is similar to the queue and stack data structures that stores data along with the priority associated with them. In this, the data is served according to the priority. Priority Queues can be implemented using an array, linked list, and trees; however, they are often implemented using a heap as it is very efficient.
    In this chapter, we will learn the following:
    • The concept of the heap data structure and different operations on it
    • Understanding the concept of the Priority Queue and its implementation using Python

    Heaps

    A heap data structure is a specialization of a tree in which the nodes are ordered in a specific way. A heap is a data structure where each data elements satisfies a heap property, and the heap property states that there must be a certain relationship between a parent node and its child nodes. According to this certain relationship in the tree, the heaps can be of two types, in other words, max heaps and min heaps. In a max heap, each parent node value must always be greater than or equal to all its children. In this kind of tree, the root node must be the greatest value in the tree. For example, see Figure 7.1 showing the max heap in which all the nodes have greater values compared to their children:
    Figure 7.1: An example of a max heap
    In a min heap, the relationship between parent and children is that the value of the parent node must always be less than or equal to its children. This rule should be followed by all the nodes in the tree. In the min heap, the root node holds the lowest value. For example, see Figure 7.2 showing the min
Index pages curate the most relevant extracts from our library of academic textbooks. They’ve been created using an in-house natural language model (NLM), each adding context and meaning to key research topics.