#include<queue>priority_queue<int> pq; // create priority queue for intspq.push(10); // push integer in priority queuepq.top(); // get the top element from pq without popping itpq.pop(); // pop the top element from pq. It returns voidwhile(!pq.empty()){ // pq.empty returns whether pq is empty or not}
Note: If we update the value of a node inside our priority_queue, that node will not be auto-heapified and hence will not be placed at correct position. Our priority queue will become inconsistent. So whenever you have to update a node inside priority queue
update the node's value
create a new node* pointing to that node and push that in priority queue
our new node* will be placed at correct position.
Since we are updating the value in the node and the priority queue contains only node pointers, the old node pointer and fresh node pointer, both will point to same node which always contain the updated value. So when we will process this priority queue, it doesn't matter which pointer is popped out first since both are pointing to node with updated value. Now, to prevent multiple processing of node, keep a "isProcessed" field inside every node, initially set to false. whenever you process that node for the first time (can be via old pointer or via fresh pointer, doesn't matter), set that isProcessed field to true. Check this filed whenever you are processing a node after popping it out, ignore that node if it is already processed
// priority queue when keys inside the nodes of priority queue are updated during prrocessing (e.g. in dijkstra)#include<iostream>#include<queue>usingnamespace std;classNode {public:int key; // priority queue will do sorting based on key string label; // label is general metadaata stored in the node.bool processed; // whenever we update key in a node, we insert a new node pointer in the pq // To prevent multiple processing of same node, we use this flag to keep track // if some node is already processedNode(int key,string label,bool processed):key(key),label(label),processed(processed){ }};classNodeComparator{public:booloperator()(Node*&lowerPriorityNode,Node*&higherPriorityNode){returnhigherPriorityNode->key <=lowerPriorityNode->key; }};intmain(int argc,charconst*argv[]){ // Always use Node* instead of Node. If there is some update in node key which decreases node's priority // (by increasing its key value here in this example), then stale node will be popped earlier than the updated node // which is wrong. If we use pointer, then stale node pointer and fresh node pointer, both will point to same node // which contains the updated value of key. // https://stackoverflow.com/a/27305600 priority_queue<Node*, vector<Node*>, NodeComparator> pq; Node* n3 =newNode(50,"N3",false);pq.push(newNode(10,"N1",false));pq.push(newNode(3,"N2",false));pq.push(n3);pq.push(newNode(150,"N4",false));pq.push(newNode(1,"N5",false));pq.push(newNode(100,"N6",false));pq.push(newNode(15,"N7",false)); cout <<"Priority Queue Initialised\n\n"<< endl;n3->key =5;pq.push(n3); cout <<"n3 key updated\n"<< endl; cout <<"\n\nstarting processing of nodes in priority queue \n"<< endl;while(!pq.empty()){ Node* top =pq.top();pq.pop();if(!top->processed){ // process node cout <<"top node = "<<top->label <<" : "<<top->key << endl;top->processed =true; } }return0;}