简体   繁体   English

使用优先级队列结构?

[英]Priority queue structure used?

While searching for some functions in C++ standard library documentation I read that push and pop for priority queues needs constant time. 在C ++标准库文档中搜索某些函数时,我读到优先级队列的推送和弹出需要恒定的时间。

http://www.cplusplus.com/reference/stl/priority_queue/push/ http://www.cplusplus.com/reference/stl/priority_queue/push/

Constant (in the priority_queue). 常量(在priority_queue中)。 Although notice that push_heap operates in logarithmic time. 虽然注意到push_heap在对数时间内运行。

My question is what kind of data structure is used to maintain a priority queue with O(1) for push and pop ? 我的问题是使用什么样的数据结构来维护优先级队列,其中包含推送和弹出的O(1)?

I assume you are referring to cplusplus.com's page . 我假设你指的是cplusplus.com的页面

Earlier on the page it says: 它在页面的前面说:

This member function effectively calls the member function push_back of the underlying container object, and then calls the push_heap algorithm to keep the heap property of priority_queues. 该成员函数有效地调用底层容器对象的成员函数push_back,然后调用push_heap算法以保持priority_queues的堆属性。

So, after the O(1) push, the data structure has lost its heap property invariant and will then always follow that with an O(log N) call to restore that property. 因此,在O(1)推送之后,数据结构已丢失其堆属性不变量,然后将始终遵循O(log N)调用以恢复该属性。 In other words, the O(1) operation is only one part of the entire operation; 换句话说, O(1)操作只是整个操作的一部分; the full operation is O(1) + O(log N) which is the same as O(log N) . 完整操作是O(1) + O(log N) ,它与O(log N)

I guess the reason they mention that is that priority queue is an adapter and they are trying to emphasize the difference between what the underlying container does vs what the adapter does. 我猜他们提到的原因是优先级队列是一个适配器,他们试图强调底层容器与适配器之间的区别。

The underlying storage for priority_queue can be a vector or a deque or anything similar that supports random access iterators. priority_queue的底层存储可以是vector或deque或任何类似的支持随机访问迭代器的存储。 The storage is maintained as a heap, which is not O(N) for push, so I suspect you have read this wrong 存储作为堆维护,对于推送不是O(N),所以我怀疑你已经读错了

I'm skeptical about the O(1) claim... where did you see it? 我对O(1)声明持怀疑态度......你在哪里看到它?

In any case, here are some notes on gcc's implementation of a priority queue . 无论如何,这里有一些关于gcc优先级队列实现的注释

There is not such a kind of heap. 没有这种堆。 They have written that it calls push_heap which is logarithmic so it is logn. 他们写过它调用push_heap是对数的,所以它是logn。

The standard defines those members in terms of push_heap and pop_heap , which implies the compilexity. 该标准根据push_heappop_heap定义了这些成员,这意味着compilexity。

If what that reference says is true (it says top is also constant), why doesn't anybody implement general-purpose O(N) sorting using std::priority_queue ? 如果该引用所说的是真的(它表示top也是常量),为什么没有人使用std::priority_queue实现通用O(N)排序?

On second though, this is what the reference may be trying to say, in a very misleading way: the complexity is that of push_back O(1) + push_heap (O(log N)) 第二,虽然这是参考文献可能试图说的一种非常误导的方式:复杂性是push_back O(1)+ push_heap (O(log N))

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

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