简体   繁体   English

基于迭代器构造提升优先级队列

[英]construct boost priority queue based on iterators

I have a list of binomial_heaps and each iteration of the algorithm I have to update the priority of an element in some of the binomial_heaps. 我有一个binomial_heaps列表,并且算法的每次迭代都必须更新某些binomial_heaps中元素的优先级。 For this I use the update function of the boost binomial_heap . 为此,我使用boost binomial_heap的更新功能。 However one of the binomial_heaps I have to remove and rebuild completely (as all priorities change). 但是,我必须完全删除并重建binomial_heaps之一(因为所有优先级都会发生变化)。 Instead of using push every time (which if I understand correctly would have a complexity of n*log(n)) I would like to construct it based on iterators of an underlying container (a kind of heapify or make_heap operation which would be linear time). 而不是每次都使用push(如果我理解正确的话,它的复杂度为n * log(n)),我想基于基础容器的迭代器(一种为线性时间的heapify或make_heap操作)构造它)。 This seems possible in the standard priority_queue , but not in the boost implementation. 在标准priority_queue中这似乎是可能的,但在boost实现中则不可能。 On the other hand the standard one does not provide me with an update function. 另一方面,标准版本没有为我提供更新功能。 Is there a way around this where I can have both, or another library that supports both. 有没有办法解决这个问题,我可以同时拥有这两者,或者另一个同时支持这两者的库。 Or maybe my reasoning, that pushing all elements on an empty priority queue is slower, is not correct? 也许我的推理是,将所有元素推送到空优先级队列中比较慢,这是不正确的?

Some might say there is something seriously wrong with the fact that I need to rebuild an entire priority queue which would make the use of the priority queue completely superfluous. 有人可能会说我需要重建整个优先级队列这一事实有严重的错误,这将使优先级队列的使用完全多余。 The algorithm I want to implement is "Finding community structure in very large networks by Aaron Clauset" in which the authors do exactly that (unless I didn't interpret it correctly) 我要实现的算法是“通过Aaron Clauset在非常大的网络中查找社区结构”,其中的作者确实做到了这一点(除非我没有正确解释它)

(Sorry couldn't post the link to the paper as I don't have enough reputation to post more than 2 links) (抱歉,由于我没有足够的声誉来发布两个以上的链接,因此无法将链接发布到论文上)

The "fast modularity" algorithm by Clauset et al. Clauset等人的“快速模块化”算法。 ( paper here , code here ) uses a pair of linked data structures. 此处 纸,此处为 代码 )使用一对链接的数据结构。 On the one hand, you have a sparse matrix data structure (which is really just an adjacency list in which instead of storing the elements hanging off a particular array element as a linked list, we store them using a balanced binary tree data structure), and a max-heap. 一方面,您有一个稀疏的矩阵数据结构(实际上只是一个邻接表,在该表中,我们使用平衡的二叉树数据结构存储它们,而不是将悬挂在特定数组元素上的元素存储为链接列表),和最大堆。 All the values in the sparse matrix (which are really the dQ_ij values for the potential merges in the algorithm) are also stored in the max-heap. 稀疏矩阵中的所有值(实际上是算法中潜在合并的dQ_ij值)也存储在max-heap中。

So, the max-heap is just an efficient way of finding the edge in the sparse matrix with the most positive value. 因此,最大堆只是在稀疏矩阵中找到具有最大正值的边的有效方法。 Once you have the ij pair for that edge, you want to "insert" the elements of column (row) i into the elements of column (row) j, and then you want to delete column (row) i. 在具有该边的ij对之后,您想要将列(行)i的元素“插入”到列(行)j的元素中,然后要删除列(行)i。 So, you're not going to rebuild the entire max-heap after each pop from the max-heap. 因此,在每次从max-heap弹出之后,您都不会重建整个max-heap。 Instead, you want to delete some elements from it (the ones in the row/column that you delete from the sparse matrix) and update the values of others (the ones in the updated row/column for j). 相反,您要从中删除某些元素(从稀疏矩阵中删除的行/列中的元素),并更新其他元素的值(对于j,则为更新的行/列中的元素)。

This is where the linked data structure is helpful -- in the original implementation, each element in the sparse matrix stores a pointer to its corresponding entry in the max-heap so that if you update the value in the sparse matrix, you can then find the corresponding element in the max-heap and update its value. 这是链接数据结构有用的地方-在原始实现中,稀疏矩阵中的每个元素在其最大堆中存储一个指向其对应条目的指针,这样,如果您更新稀疏矩阵中的值,则可以找到max-heap中的相应元素并更新其值。 Once you do this, you need to re-heapify the updated heap element, by letting it move (recursively) up or down in the heap. 完成此操作后,您需要通过让更新的堆元素(递归)在堆中上下移动来重新堆集。 Similarly, if you delete an element in the sparse matrix, you can find its entry in the heap and call a delete function on it. 同样,如果删除稀疏矩阵中的元素,则可以在堆中找到它的条目并在其上调用delete函数。

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

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