{"id":9226,"date":"2022-08-30T11:58:50","date_gmt":"2022-08-30T11:58:50","guid":{"rendered":"https:\/\/www.prepbytes.com\/blog\/?p=9226"},"modified":"2024-01-30T06:43:52","modified_gmt":"2024-01-30T06:43:52","slug":"prims-algorithm-using-priority_queue-in-stl","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/prims-algorithm-using-priority_queue-in-stl\/","title":{"rendered":"Prim\u2019s algorithm using priority_queue in STL"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661417108291-Primes%20Algo%20Header%20Image.jpeg\" alt=\"\" \/><br \/>\nEmbarking on the journey of understanding Prim&#8217;s algorithm and its implementation using the priority_queue in the C++ Standard Template Library (STL) opens a gateway to the realm of efficient minimum spanning tree algorithms. Prim&#8217;s algorithm is renowned for its simplicity and effectiveness in finding the minimum spanning tree of a connected, undirected graph. This article delves into the intricacies of Prim&#8217;s algorithm and demonstrates its application using the priority_queue data structure in the STL. We\u2019ll be given an undirected, weighted and connected graph. We have to find the minimum spanning tree using prim\u2019s algorithm. Let&#8217;s unravel the concepts, explore the implementation, and understand how this algorithm optimally connects the dots in graph theory.<\/p>\n<h3>What is Prim\u2019s Algorithm?<\/h3>\n<p>Prim\u2019s algorithm is a greedy algorithm. Prim&#8217;s algorithm is used to find the minimum spanning tree. It finds the subset of edges that includes every vertex such that the sum of the weights of the edges can be minimized.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661859736267-Image-01-5.png\" alt=\"\" \/><\/p>\n<h3>How does Prim&#8217;s algorithm work?<\/h3>\n<p>First, we\u2019ll initialize the MST with a random vertex then we\u2019ll find the tree that connects the tree with the new vertices.<br \/>\nThen select the minimum edge and add it to the tree. Then repeat the previous step until a minimum spanning tree is found.<\/p>\n<h3>Applications of prim\u2019s algorithm:<\/h3>\n<ul>\n<li>It is used in network designing.<\/li>\n<li>It is used in electrical cables.<\/li>\n<li>Also can be used in network cycles.<\/li>\n<\/ul>\n<p>We&#8217;ll implement our own <a href=\"https:\/\/prepbytes.com\/blog\/queues\/priority-queue-in-c-stl\/\" title=\"priority queue\">priority queue<\/a>. In CPP STL provides priority queue but it doesn\u2019t support decrease key operation.<br \/>\nIn prim\u2019s algorithm, we need a priority queue and some operations such as:<\/p>\n<ul>\n<li>Extract_min: All the vertices which are not included in the minimum spanning tree, we need to get the minimum key value vertex.<\/li>\n<li>Decrease_key: After extracting the vertex we need to update the keys which are its adjacent vertices and if the value of the new key is smaller, then we\u2019ll update it.<\/li>\n<\/ul>\n<h3>Algorithm:<\/h3>\n<ol>\n<li>Initialize all the keys as infinite and mark the parent of every vertex as -1.<\/li>\n<li>Create an empty priority_queue q.<\/li>\n<li>Every item of p is a pair of weight, vertex.<\/li>\n<li>Then initialize all the vertices as not part of the minimum spanning tree (for this we\u2019ll use bool array).<\/li>\n<li>Insert a source vertex into p and mark its key as 0.<\/li>\n<li>Then, extract the minimum key vertex and mark that vertex as x.<\/li>\n<li>Mark x as true.<\/li>\n<li>Traverse all the adjacent vertices and repeat the steps.<\/li>\n<\/ol>\n<h3>Dry Run<\/h3>\n<p>Consider the graph shown below.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661859723231-Image-02.png\" alt=\"\" \/><\/p>\n<p>Let us say that we want to construct the minimum spanning tree for this graph. So, let us take an empty priority queue as shown below.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661859707309-Image-03.png\" alt=\"\" \/><\/p>\n<p>So, we have a boolean array visited which shows that initially, no vertex is visited and we have a priority queue that is empty. Initially, let us insert the vertex 0 and the weight here will also be 0.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661859693528-Image-04.png\" alt=\"\" \/><\/p>\n<p>Now, as per the algorithm, we remove it from the priority queue, mark it visited in the boolean array, and insert its neighbors in the priority queue.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661859676290-Image-05.png\" alt=\"\" \/><\/p>\n<p>So, as shown above, we have removed vertex 0, marked it true in the visited array, and inserted the neighboring vertices 1 and 3 with their respective edge weights.<\/p>\n<p>Now, the lower cost edge weight will be considered by the priority queue. So, vertex 1 will be removed now and its neighbor vertex 2 with weight 10 will be inserted.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661859663778-Image-06.png\" alt=\"\" \/><\/p>\n<p>Also, the spanning tree is shown by shading the edges (look at the image below).<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661859650475-Image-07.png\" alt=\"\" \/><\/p>\n<p>Now, vertex 2 will be removed and vertex 3 will be inserted with a cost of 10.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661859637800-Image-08.png\" alt=\"\" \/><\/p>\n<p>Now, the vertex 3 will be removed but the one with the cost 10. Also, vertex 4 will be inserted.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661859584956-Image-09.png\" alt=\"\" \/><\/p>\n<p>Now, vertex 4 will be removed and vertex 5 with the weight 3 and vertex 6 with the weight 8 will be added.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661859568725-Image-10.png\" alt=\"\" \/><\/p>\n<p>Now, vertex 5 will be removed and vertex 6 with weight 3 will be added.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661859551827-Image-11.png\" alt=\"\" \/><\/p>\n<p>Finally, vertex 6 with weight 3 will be removed.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661859538958-Image-12.png\" alt=\"\" \/><\/p>\n<p>So, we finally get the minimum spanning tree.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661859520679-Image-13.png\" alt=\"\" \/><\/p>\n<p>\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_9477 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_9477 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_9477 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_9477 .wpsm_nav-tabs > li.active > a, #tab_container_9477 .wpsm_nav-tabs > li.active > a:hover, #tab_container_9477 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_9477 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_9477 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_9477 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_9477 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_9477 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_9477 .wpsm_nav-tabs > li > a:hover , #tab_container_9477 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_9477 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_9477 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_9477 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_9477 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_9477 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_9477 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_9477 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_9477 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_9477 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_9477 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_9477 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_9477\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_9477\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_9477_1\" aria-controls=\"tabs_desc_9477_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C++<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_9477\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_9477_1\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"cpp\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n\r\n#include&lt;bits\/stdc++.h&gt;\r\nusing namespace std;\r\n# define INF 0x3f3f3f3f\r\n\r\ntypedef pair&lt;int, int&gt; iPair;\r\n\r\n\r\nclass Graph\r\n{\r\n\tint V; \r\n\tlist&lt; pair&lt;int, int&gt; &gt; *adj;\r\n\r\npublic:\r\n\tGraph(int V); \r\n\r\n\tvoid addEdge(int u, int v, int w);\r\n\r\n\tvoid primMST();\r\n};\r\n\r\nGraph::Graph(int V)\r\n{\r\n\tthis-&gt;V = V;\r\n\tadj = new list&lt;iPair&gt; [V];\r\n}\r\n\r\nvoid Graph::addEdge(int u, int v, int w)\r\n{\r\n\tadj[u].push_back(make_pair(v, w));\r\n\tadj[v].push_back(make_pair(u, w));\r\n}\r\n\r\nvoid Graph::primMST()\r\n{\r\n\tpriority_queue&lt; iPair, vector &lt;iPair&gt; , greater&lt;iPair&gt; &gt; pq;\r\n\r\n\tint src = 0; \r\n\r\n\r\n\tvector&lt;int&gt; key(V, INF);\r\n\r\n\tvector&lt;int&gt; parent(V, -1);\r\n\r\n\tvector&lt;bool&gt; inMST(V, false);\r\n\r\n\r\n\tpq.push(make_pair(0, src));\r\n\tkey[src] = 0;\r\n\r\n\twhile (!pq.empty())\r\n\t{\r\n\t\tint u = pq.top().second;\r\n\t\tpq.pop();\r\n\t\t\r\n\t\r\n\t\tif(inMST[u] == true){\r\n\t\t\tcontinue;\r\n\t\t}\r\n\t\r\n\t\tinMST[u] = true; \r\n\r\n\t\tlist&lt; pair&lt;int, int&gt; &gt;::iterator i;\r\n\t\tfor (i = adj[u].begin(); i != adj[u].end(); ++i)\r\n\t\t{\r\n\t\t\tint v = (*i).first;\r\n\t\t\tint weight = (*i).second;\r\n\r\n\t\t\tif (inMST[v] == false &amp;&amp; key[v] &gt; weight)\r\n\t\t\t{\r\n\t\t\t\tkey[v] = weight;\r\n\t\t\t\tpq.push(make_pair(key[v], v));\r\n\t\t\t\tparent[v] = u;\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\tfor (int i = 1; i &lt; V; ++i)\r\n\t\tprintf(&quot;%d - %d&#92;n&quot;, parent[i], i);\r\n}\r\n\r\nint main()\r\n{\r\n\tint V = 9;\r\n\tGraph g(V);\r\n\r\n\tg.addEdge(0, 1, 4);\r\n\tg.addEdge(0, 7, 8);\r\n\tg.addEdge(1, 2, 8);\r\n\tg.addEdge(1, 7, 11);\r\n\tg.addEdge(2, 3, 7);\r\n\tg.addEdge(2, 8, 2);\r\n\tg.addEdge(2, 5, 4);\r\n\tg.addEdge(3, 4, 9);\r\n\tg.addEdge(3, 5, 14);\r\n\tg.addEdge(4, 5, 10);\r\n\tg.addEdge(5, 6, 2);\r\n\tg.addEdge(6, 7, 1);\r\n\tg.addEdge(6, 8, 6);\r\n\tg.addEdge(7, 8, 7);\r\n\r\n\tg.primMST();\r\n\r\n\treturn 0;\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_9477 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_9477 a\"),jQuery(\"#tab-content_9477\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t<br \/>\n<strong>Time complexity :<\/strong> O(E log V).<\/p>\n<p>Faster Implementation using an array of vectors represented as a weighted graph.<\/p>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_9478 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_9478 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_9478 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_9478 .wpsm_nav-tabs > li.active > a, #tab_container_9478 .wpsm_nav-tabs > li.active > a:hover, #tab_container_9478 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_9478 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_9478 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_9478 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_9478 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_9478 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_9478 .wpsm_nav-tabs > li > a:hover , #tab_container_9478 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_9478 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_9478 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_9478 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_9478 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_9478 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_9478 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_9478 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_9478 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_9478 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_9478 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_9478 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_9478\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_9478\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_9478_1\" aria-controls=\"tabs_desc_9478_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C++<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_9478\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_9478_1\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"cpp\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include&lt;bits\/stdc++.h&gt;\r\nusing namespace std;\r\n# define INF 0x3f3f3f3f\r\n\r\ntypedef pair&lt;int, int&gt; iPair;\r\n\r\nvoid addEdge(vector &lt;pair&lt;int, int&gt; &gt; adj[], int u,\r\n\t\t\t\t\t\t\t\t\tint v, int wt)\r\n{\r\n\tadj[u].push_back(make_pair(v, wt));\r\n\tadj[v].push_back(make_pair(u, wt));\r\n}\r\n\r\nvoid primMST(vector&lt;pair&lt;int,int&gt; &gt; adj[], int V)\r\n{\r\n\tpriority_queue&lt; iPair, vector &lt;iPair&gt; , greater&lt;iPair&gt; &gt; pq;\r\n\r\n\tint src = 0;\r\n\r\n\r\n\tvector&lt;int&gt; key(V, INF);\r\n\r\n\r\n\tvector&lt;int&gt; parent(V, -1);\r\n\r\n\tvector&lt;bool&gt; inMST(V, false);\r\n\r\n\tpq.push(make_pair(0, src));\r\n\tkey[src] = 0;\r\n\r\n\twhile (!pq.empty())\r\n\t{\r\n\t\t\r\n\t\tint u = pq.top().second;\r\n\t\tpq.pop();\r\n\r\n\t\tif(inMST[u] == true){\r\n\t\t\tcontinue;\r\n\t\t}\r\n\t\r\n\t\tinMST[u] = true; \r\n\r\n\t\tfor (auto x : adj[u])\r\n\t\t{\r\n\t\t\tint v = x.first;\r\n\t\t\tint weight = x.second;\r\n\r\n\t\t\tif (inMST[v] == false &amp;&amp; key[v] &gt; weight)\r\n\t\t\t{\r\n\t\t\t\tkey[v] = weight;\r\n\t\t\t\tpq.push(make_pair(key[v], v));\r\n\t\t\t\tparent[v] = u;\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\tfor (int i = 1; i &lt; V; ++i)\r\n\t\tprintf(&quot;%d - %d&#92;n&quot;, parent[i], i);\r\n}\r\n\r\nint main()\r\n{\r\n\tint V = 9;\r\n\tvector&lt;iPair &gt; adj[V];\r\n\r\n\taddEdge(adj, 0, 1, 4);\r\n\taddEdge(adj, 0, 7, 8);\r\n\taddEdge(adj, 1, 2, 8);\r\n\taddEdge(adj, 1, 7, 11);\r\n\taddEdge(adj, 2, 3, 7);\r\n\taddEdge(adj, 2, 8, 2);\r\n\taddEdge(adj, 2, 5, 4);\r\n\taddEdge(adj, 3, 4, 9);\r\n\taddEdge(adj, 3, 5, 14);\r\n\taddEdge(adj, 4, 5, 10);\r\n\taddEdge(adj, 5, 6, 2);\r\n\taddEdge(adj, 6, 7, 1);\r\n\taddEdge(adj, 6, 8, 6);\r\n\taddEdge(adj, 7, 8, 7);\r\n\r\n\tprimMST(adj, V);\r\n\r\n\treturn 0;\r\n}\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_9478 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_9478 a\"),jQuery(\"#tab-content_9478\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<p><strong>Conclusion<\/strong><br \/>\nIn conclusion, Prim&#8217;s algorithm, when implemented with the aid of the priority_queue in the C++ Standard Template Library, emerges as a powerful tool for finding minimum spanning trees in connected, undirected graphs. Its simplicity, combined with the efficiency of the priority_queue, makes it a valuable asset in network design, ensuring optimal connectivity with minimal edge weights. As we navigate the intricacies of this algorithm, we gain insights into its applications and the elegance with which it addresses graph theory problems, solidifying its place as a cornerstone in the realm of computer science algorithms.<\/p>\n<h2>FAQs Related to Prim\u2019s Algorithm using Priority Queue<\/h2>\n<p>Here are some FAQs related to Prim\u2019s Algorithm.<\/p>\n<p><strong>1. What is Prim&#8217;s algorithm, and what is its significance in graph theory?<\/strong><br \/>\nPrim&#8217;s algorithm is a greedy algorithm used to find the minimum spanning tree of a connected, undirected graph. Its significance lies in its ability to efficiently connect all the vertices of the graph with the minimum possible total edge weight, making it a key tool in network design and optimization.<\/p>\n<p><strong>2. How does Prim&#8217;s algorithm work?<\/strong><br \/>\nPrim&#8217;s algorithm starts with an arbitrary node and systematically adds the shortest edge connecting any explored node to an unexplored one. This process continues until all nodes are included, forming the minimum spanning tree. The algorithm always selects the shortest available edge at each step, ensuring the minimization of the total edge weight.<\/p>\n<p><strong>3. What is the role of a priority queue in Prim&#8217;s algorithm implementation?<\/strong><br \/>\nA priority queue is used to efficiently retrieve the minimum-weight edge at each step of the algorithm. The priority_queue in the STL allows the algorithm to prioritize edges based on their weights, facilitating a streamlined selection process and contributing to the overall efficiency of Prim&#8217;s algorithm.<\/p>\n<p><strong>4. How is the priority_queue structured in Prim&#8217;s algorithm implementation?<\/strong><br \/>\nThe priority_queue is structured in a way that the edge with the minimum weight is always at the front. This ensures that when an edge is selected for inclusion in the minimum spanning tree, it is the one with the smallest weight, adhering to the greedy nature of Prim&#8217;s algorithm.<\/p>\n<p><strong>5. Can Prim&#8217;s algorithm handle graphs with weighted edges and disconnected components?<\/strong><br \/>\nPrim&#8217;s algorithm is designed for connected graphs. In the case of disconnected components, the algorithm can be applied separately to each connected component to find the minimum spanning tree for each subgraph.<\/p>\n<p><strong>6. How does Prim&#8217;s algorithm compare to other minimum spanning tree algorithms, like Kruskal&#8217;s algorithm?<\/strong><br \/>\nPrim&#8217;s algorithm and Kruskal&#8217;s algorithm both find minimum spanning trees, but they differ in their approaches. Prim&#8217;s is a greedy algorithm that starts from a single vertex and grows the tree, while Kruskal&#8217;s is based on sorting all edges and adding them one by one while avoiding cycles. The choice between them often depends on the characteristics of the graph.<\/p>\n<p><strong>7. Are there any scenarios where Prim&#8217;s algorithm might not be the optimal choice?<\/strong><br \/>\nPrim&#8217;s algorithm is well-suited for dense graphs with many edges. However, in sparse graphs where the number of edges is significantly smaller than the total possible edges, other algorithms like Kruskal&#8217;s might be more efficient. The choice of algorithm depends on the specific characteristics of the graph in question.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Embarking on the journey of understanding Prim&#8217;s algorithm and its implementation using the priority_queue in the C++ Standard Template Library (STL) opens a gateway to the realm of efficient minimum spanning tree algorithms. Prim&#8217;s algorithm is renowned for its simplicity and effectiveness in finding the minimum spanning tree of a connected, undirected graph. This article [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[128],"tags":[],"class_list":["post-9226","post","type-post","status-publish","format-standard","hentry","category-queues"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Prim\u2019s algorithm using priority_queue in STL | PrepBytes Blog<\/title>\n<meta name=\"description\" content=\"This article tried to discuss Prim\u2019s algorithm using priority_queue in STL. Hope this blog helps you understand and solve the problems in the interviews.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prepbytes.com\/blog\/prims-algorithm-using-priority_queue-in-stl\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Prim\u2019s algorithm using priority_queue in STL | PrepBytes Blog\" \/>\n<meta property=\"og:description\" content=\"This article tried to discuss Prim\u2019s algorithm using priority_queue in STL. Hope this blog helps you understand and solve the problems in the interviews.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/prims-algorithm-using-priority_queue-in-stl\/\" \/>\n<meta property=\"og:site_name\" content=\"PrepBytes Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prepbytes0211\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-08-30T11:58:50+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-01-30T06:43:52+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661417108291-Primes%20Algo%20Header%20Image.jpeg\" \/>\n<meta name=\"author\" content=\"Prepbytes\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prepbytes\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/prims-algorithm-using-priority_queue-in-stl\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/prims-algorithm-using-priority_queue-in-stl\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"Prim\u2019s algorithm using priority_queue in STL\",\"datePublished\":\"2022-08-30T11:58:50+00:00\",\"dateModified\":\"2024-01-30T06:43:52+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/prims-algorithm-using-priority_queue-in-stl\/\"},\"wordCount\":1249,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/prims-algorithm-using-priority_queue-in-stl\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661417108291-Primes%20Algo%20Header%20Image.jpeg\",\"articleSection\":[\"Queues\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/prims-algorithm-using-priority_queue-in-stl\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/prims-algorithm-using-priority_queue-in-stl\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/prims-algorithm-using-priority_queue-in-stl\/\",\"name\":\"Prim\u2019s algorithm using priority_queue in STL | PrepBytes Blog\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/prims-algorithm-using-priority_queue-in-stl\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/prims-algorithm-using-priority_queue-in-stl\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661417108291-Primes%20Algo%20Header%20Image.jpeg\",\"datePublished\":\"2022-08-30T11:58:50+00:00\",\"dateModified\":\"2024-01-30T06:43:52+00:00\",\"description\":\"This article tried to discuss Prim\u2019s algorithm using priority_queue in STL. Hope this blog helps you understand and solve the problems in the interviews.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/prims-algorithm-using-priority_queue-in-stl\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/prims-algorithm-using-priority_queue-in-stl\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/prims-algorithm-using-priority_queue-in-stl\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661417108291-Primes%20Algo%20Header%20Image.jpeg\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661417108291-Primes%20Algo%20Header%20Image.jpeg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/prims-algorithm-using-priority_queue-in-stl\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/43.205.93.38\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Queues\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/queues\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Prim\u2019s algorithm using priority_queue in STL\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/43.205.93.38\/#website\",\"url\":\"http:\/\/43.205.93.38\/\",\"name\":\"PrepBytes Blog\",\"description\":\"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING\",\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/43.205.93.38\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/43.205.93.38\/#organization\",\"name\":\"Prepbytes\",\"url\":\"http:\/\/43.205.93.38\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"contentUrl\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"width\":160,\"height\":160,\"caption\":\"Prepbytes\"},\"image\":{\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/prepbytes0211\/\",\"https:\/\/www.instagram.com\/prepbytes\/\",\"https:\/\/www.linkedin.com\/company\/prepbytes\/\",\"https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\",\"name\":\"Prepbytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"caption\":\"Prepbytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Prim\u2019s algorithm using priority_queue in STL | PrepBytes Blog","description":"This article tried to discuss Prim\u2019s algorithm using priority_queue in STL. Hope this blog helps you understand and solve the problems in the interviews.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/prepbytes.com\/blog\/prims-algorithm-using-priority_queue-in-stl\/","og_locale":"en_US","og_type":"article","og_title":"Prim\u2019s algorithm using priority_queue in STL | PrepBytes Blog","og_description":"This article tried to discuss Prim\u2019s algorithm using priority_queue in STL. Hope this blog helps you understand and solve the problems in the interviews.","og_url":"https:\/\/prepbytes.com\/blog\/prims-algorithm-using-priority_queue-in-stl\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2022-08-30T11:58:50+00:00","article_modified_time":"2024-01-30T06:43:52+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661417108291-Primes%20Algo%20Header%20Image.jpeg","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/prims-algorithm-using-priority_queue-in-stl\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/prims-algorithm-using-priority_queue-in-stl\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"Prim\u2019s algorithm using priority_queue in STL","datePublished":"2022-08-30T11:58:50+00:00","dateModified":"2024-01-30T06:43:52+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/prims-algorithm-using-priority_queue-in-stl\/"},"wordCount":1249,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/prims-algorithm-using-priority_queue-in-stl\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661417108291-Primes%20Algo%20Header%20Image.jpeg","articleSection":["Queues"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/prims-algorithm-using-priority_queue-in-stl\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/prims-algorithm-using-priority_queue-in-stl\/","url":"https:\/\/prepbytes.com\/blog\/prims-algorithm-using-priority_queue-in-stl\/","name":"Prim\u2019s algorithm using priority_queue in STL | PrepBytes Blog","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/prims-algorithm-using-priority_queue-in-stl\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/prims-algorithm-using-priority_queue-in-stl\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661417108291-Primes%20Algo%20Header%20Image.jpeg","datePublished":"2022-08-30T11:58:50+00:00","dateModified":"2024-01-30T06:43:52+00:00","description":"This article tried to discuss Prim\u2019s algorithm using priority_queue in STL. Hope this blog helps you understand and solve the problems in the interviews.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/prims-algorithm-using-priority_queue-in-stl\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/prims-algorithm-using-priority_queue-in-stl\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/prims-algorithm-using-priority_queue-in-stl\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661417108291-Primes%20Algo%20Header%20Image.jpeg","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1661417108291-Primes%20Algo%20Header%20Image.jpeg"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/prims-algorithm-using-priority_queue-in-stl\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/43.205.93.38\/"},{"@type":"ListItem","position":2,"name":"Queues","item":"https:\/\/prepbytes.com\/blog\/category\/queues\/"},{"@type":"ListItem","position":3,"name":"Prim\u2019s algorithm using priority_queue in STL"}]},{"@type":"WebSite","@id":"http:\/\/43.205.93.38\/#website","url":"http:\/\/43.205.93.38\/","name":"PrepBytes Blog","description":"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING","publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/43.205.93.38\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/43.205.93.38\/#organization","name":"Prepbytes","url":"http:\/\/43.205.93.38\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/","url":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","contentUrl":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","width":160,"height":160,"caption":"Prepbytes"},"image":{"@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/prepbytes0211\/","https:\/\/www.instagram.com\/prepbytes\/","https:\/\/www.linkedin.com\/company\/prepbytes\/","https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA"]},{"@type":"Person","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e","name":"Prepbytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","caption":"Prepbytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/9226","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/comments?post=9226"}],"version-history":[{"count":5,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/9226\/revisions"}],"predecessor-version":[{"id":18783,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/9226\/revisions\/18783"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=9226"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=9226"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=9226"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}