使用优先级队列结构?

发布于 2024-08-29 02:57:24 字数 364 浏览 9 评论 0原文

在 C++ 标准库文档中搜索某些函数时,我读到优先级队列的推送和弹出需要恒定的时间。

http://www.cplusplus.com/reference/stl/priority_queue/push/< /a>

常量(在priority_queue中)。尽管请注意,push_heap 以对数时间运行。

我的问题是使用什么样的数据结构来维护优先级队列,其推送和弹出操作的复杂度为 O(1)?

While searching for some functions in C++ standard library documentation I read that push and pop for priority queues needs constant time.

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

Constant (in the priority_queue). Although notice that push_heap operates in logarithmic time.

My question is what kind of data structure is used to maintain a priority queue with O(1) for push and pop ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

注定孤独终老 2024-09-05 02:57:24

我假设您指的是 cplusplus.com 页面

该页面早些时候写着:

这个成员函数实际上调用了底层容器对象的成员函数push_back,然后调用push_heap算法来保留priority_queues的heap属性。

因此,在O(1)推送之后,数据结构已经失去了其堆属性不变性,并且将始终遵循O(log N)调用 恢复该属性。换句话说,O(1)操作只是整个操作的一部分;完整的操作是O(1) + O(log N),与O(log N)相同。

我猜他们提到这一点的原因是优先级队列是一个适配器,他们试图强调底层容器的功能与适配器的功能之间的区别。

I assume you are referring to cplusplus.com's page.

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.

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. In other words, the O(1) operation is only one part of the entire operation; the full operation is O(1) + O(log N) which is the same as 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.

无人接听 2024-09-05 02:57:24

Priority_queue 的底层存储可以是向量、双端队列或任何支持随机访问迭代器的类似存储。存储以堆的形式维护,对于推送来说这不是 O(N),所以我怀疑你读错了

The underlying storage for priority_queue can be a vector or a deque or anything similar that supports random access iterators. The storage is maintained as a heap, which is not O(N) for push, so I suspect you have read this wrong

凯凯我们等你回来 2024-09-05 02:57:24

我对 O(1) 声明持怀疑态度......你在哪里看到它?

无论如何,这里有一些关于 gcc 优先级实现的注释队列。

I'm skeptical about the O(1) claim... where did you see it?

In any case, here are some notes on gcc's implementation of a priority queue.

¢好甜 2024-09-05 02:57:24

不存在这样的堆。他们写道,它调用push_heap,它是对数的,所以它是logn。

There is not such a kind of heap. They have written that it calls push_heap which is logarithmic so it is logn.

无法回应 2024-09-05 02:57:24

该标准根据 push_heappop_heap 定义了这些成员,这意味着编译性。

如果该引用所说的是真的(它说top< /code> 也是常量),为什么没有人使用 std::priority_queue 实现通用 O(N) 排序?

其次,这就是参考文献可能试图以一种非常误导性的方式表达的内容:复杂性是 push_back O(1) + push_heap (O(日志N))

The standard defines those members in terms of push_heap and pop_heap, which implies the 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?

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))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文