Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well, as Darren already said, <a href="http://www.cplusplus.com/reference/stl/priority_queue/" rel="noreferrer"><code>std::priority_queue</code></a> doesn't have means for decreasing the priority of an element and neither the removal of an element other than the current min. But the default <code>std::priority_queue</code> is nothing more than a simple container adaptor around a <code>std::vector</code> that uses the std heap functions from <code>&lt;algorithm&gt;</code> (<a href="http://www.cplusplus.com/reference/algorithm/push_heap/" rel="noreferrer"><code>std::push_heap</code></a>, <a href="http://www.cplusplus.com/reference/algorithm/pop_heap/" rel="noreferrer"><code>std::pop_heap</code></a> and <a href="http://www.cplusplus.com/reference/algorithm/make_heap/" rel="noreferrer"><code>std::make_heap</code></a>). So for Dijkstra (where you need priority update) I usually just do this myself and use a simple <code>std::vector</code>.</p> <p>A push is then just the O(log N) operation</p> <pre><code>vec.push_back(item); std::push_heap(vec.begin(), vec.end()); </code></pre> <p>Of course for constructing a queue anew from N elements, we don't push them all using this O(log N) operation (making the whole thing O(Nlog N)) but just put them all into the vector followed by a simple O(N)</p> <pre><code>std::make_heap(vec.begin(), vec.end()); </code></pre> <p>The min element is a simple O(1)</p> <pre><code>vec.front(); </code></pre> <p>A pop is the simple O(log N) sequence</p> <pre><code>std::pop_heap(vec.begin(), vec.end()); vec.pop_back(); </code></pre> <p>So far this is just what <code>std::priority_queue</code> usually does under the hood. Now to change an item's priority we just need to change it (however it may be incorporated in the item's type) and make the sequence a valid heap again</p> <pre><code>std::make_heap(vec.begin(), vec.end()); </code></pre> <p>I know this is an O(N) operation, but on the other hand this removes any need for keeping track of an item's position in the heap with an additional data structure or (even worse) an augmentation of the items' type. And the performance penalty over a logarithmic priority update is in practice not that signficant, considering the ease of use, compact and linear memory useage of <code>std::vector</code> (which impacts runtime, too), and the fact that I often work with graphs that have rather few edges (linear in the vertex count) anyway.</p> <p>It may not in all cases be the fastest way, but certainly the easiest.</p> <p><strong>EDIT:</strong> Oh, and since the standard library uses max-heaps, you need to use an equivalent to <code>&gt;</code> for comparing priorities (however you get them from the items), instead of the default <code>&lt;</code> operator.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload