简体   繁体   English

STL中的扩充/索引priority_queue

[英]augmenting/index priority_queue in STL

I am using STL priority_queue as an data structure in my graph application. 我在图形应用程序中使用STL priority_queue作为数据结构。 You can safely assume it like a advance version of Prim's spanning tree algorithm. 您可以放心地假设它像Prim's生成树算法的高级版本。 With in the Algorithm I want to find a node in the priority queue (not just a minimum node) efficiently.[ this is needed because cost of node might get changed and need to be fixed in priority_queue] All i have to do is augment the priority_queue and index it based on my node key's also. 在算法中,我想高效地在优先级队列中找到一个节点(而不仅仅是最小节点)。[这是必需的,因为节点的成本可能会发生变化,并且需要在priority_queue中固定]。 priority_queue并根据我的节点密钥对其进行索引。 I don't find any way this can be done in STL. 我找不到在STL中可以完成的任何方法。 Can anyone have better idea how to do it in STL? 任何人都可以更好地了解如何在STL中进行操作吗?

The std::priority_queue<T> doesn't support efficient look-up of nodes: it uses a d-ary heap , typically with d == 2 . std::priority_queue<T>不支持有效的节点查找:它使用dary堆 ,通常使用d == 2 This representation doesn't keep nodes put. 这种表示方法不会保留节点。 If you really want to use a std::priority_queue<T> with Prim's algorithm, the only way is to just add nodes with their current shortest distance and possibly add each node multiple times. 如果您真的想在Prim算法中使用std::priority_queue<T> ,则唯一的方法是仅添加具有当前最短距离的节点,并可能多次添加每个节点。 This turns the size of the into O(E) instead of O(N) , though, ie, for graphs with many edges it will result in a much higher complexity. 不过,这会将O(E)的大小变为O(E)而不是O(N) ,即,对于具有许多边的图,这将导致更高的复杂度。

You can use something like std::map<...> but that really suffers from pretty much the same problem: you can either locate the next node to extract efficiently or you can locate the nodes to update efficiently. 您可以使用类似std::map<...>但这确实存在几乎相同的问题:您可以找到下一个要有效提取的节点,也可以找到要有效更新的节点。

The "proper" approach is to use a node-based priority queue, eg, a Fibanocci-heap : Since the nodes stay put, you can get a handle from the heap when inserting a node and efficiently update the distance of a node through the handle. “适当”的方法是使用基于节点的优先级队列,例如Fibanocci-heap :由于节点保持放置状态,因此在插入节点时可以从堆中获取一个句柄,并通过该节点有效地更新节点的距离。处理。 Access to the closest node is efficient using the few top nodes in the heap's set of trees. 使用堆树集中的几个顶部节点,可以最有效地访问最近的节点。 The overall performance of basic heap operations ( push() , top() , and pop() ) are slower for Fibonacci heaps than for d-ary heaps but the efficient update of individual nodes makes their use worthwhile. 对于Fibonacci堆,基本堆操作( push()top()pop() )的总体性能要比dary堆慢,但单个节点的有效更新使其值得使用。 I seem to recall that Prim's algorithm actually required Fibonacci-heaps anyway to achieve the tight complexity bound. 我似乎还记得,Prim的算法实际上实际上需要Fibonacci堆来实现严格的复杂性界限。

I know that there is an implementation of Fibonacci-heaps at Boost . 我知道Boost处有一个斐波那契堆的实现。 An efficient implementation of Fibonacci heaps isn't entirely trivial but they are more efficient than just being of theoretical interest. 斐波纳契堆的有效实现并不完全是琐碎的,但它们比仅仅具有理论意义的效率更高。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM