Priority queue of pairs in C++ (Ordered by first)

C++Server Side ProgrammingProgramming

Priority queue is an abstract data type for storing a collection of prioritized elements that supports insertion and deletion of an element based upon their priorities, that is, the element with first priority can be removed at any time. The priority queue doesn’t stores elements in linear fashion with respect to their locations like in Stacks, Queues, List, etc. The priority queue ADT(abstract data type) stores elements based upon their priorities.

Priority Queue supports the following functions −

Size() − it is used to calculate the size of the priority queue as it returns the number of elements in it.

Empty() − it return true if the Priority Queue is empty and false otherwise

Insert(element) − used to insert the new element into a Priority Queue

Min() − it returns the element with the smallest associated key value and display error message if Priority Queue is empty.

removeMin() − it removes the element referenced by the min() function.

The task is to implement the concept of priority queue of pairs in C++ ordered by the first.

We can solve the problem in a similar fashion of heaps, have two ways to solve the problem

  • Max priority or Max heap
  • Min priority or Min heap

Heap is a tree structure in which the nodes are arranged in a specific order. There are two types of heaps Min heap and Max heap. In Min heap the root node or the parent node is smaller than its child node, whereas in Max heap the root node or the parent node is larger than its child node.

Example

Input: priorityq.push(make_pair(18, 200))
Input: priorityq.push(make_pair(18, 200))
priorityq.push(make_pair(29, 100))
priorityq.push(make_pair(11, 400))
Output: 29 100

Input: priorityq.push(make_pair(10, 200))
priorityq.push(make_pair(20, 100))
priorityq.push(make_pair(19, 400))
Output: 20 100
Through Max priority (Max heap)

Algorithm

Start
Step 1-> In main function()
   Define priority_queue<pair<int, int> > priorityq
   Call priorityq.push(make_pair(18, 200))
   Call priorityq.push(make_pair(29, 100))
   Call priorityq.push(make_pair(11, 400))
   Set pair<int, int> top = priorityq.top()
   Print top.first and top.second
Stop

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
// main program
int main() {
   priority_queue<pair<int, int> > priorityq;
   priorityq.push(make_pair(18, 200));
   priorityq.push(make_pair(29, 100));
   priorityq.push(make_pair(11, 400));
   pair<int, int> top = priorityq.top();
   cout << top.first << " " << top.second;
   return 0;
}

Output

29 100

Through Min priority (Min heap)

Algorithm

Start
Step 1-> In main function()
   Define priority_queue<pair<int, int> > priorityq
   Call pq.push(make_pair(10, 200))
   Call pq.push(make_pair(20, 100))
   Call pq.push(make_pair(15, 400))
   Set pair<int, int> top = pq.top()
   Print top.first and top.second
Stop

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pi;
// main program
int main() {
   priority_queue<pi, vector<pi>, greater<pi> > pq;
   pq.push(make_pair(10, 200));
   pq.push(make_pair(20, 100));
   pq.push(make_pair(15, 400));
   pair<int, int> top = pq.top();
   cout << top.first << " " << top.second;
   return 0;
}

Output

10 200
raja
Published on 23-Dec-2019 16:10:06
Advertisements