Data structure for efficiently returning the top-K

2020-07-09 09:49发布

Here's a description:

It operates like a regular map with get, put, and remove methods, but has a getTopKEntries(int k) method to get the top-K elements, sorted by the key:

For my specific use case, I'm adding, removing, and adjusting a lot of values in the structure, but at any one time there's approximately 500-1000 elements; I want to return the entries for the top 10 keys efficiently.

  • I call the put and remove methods many times.
  • I call the getTopKEntries method.
  • I call the put and remove methods some more times.
  • I call the getTopKEntries method.
  • ...

I'm hoping for O(1) get, put, and remove operations, and for getTopKEntries to be dependent only on K, not on the size of the map.

So what's a data structure for efficiently returning the top-K elements of a map?

My other question is similar, but is for the case of returning all elements of a map, sorted by the key.

If it helps, both the keys and values are 4-byte integers.

8条回答
forever°为你锁心
2楼-- · 2020-07-09 10:11

If the sort key is a simple integer or decimal number, a trie will be quite fast. It will use up memory, and technically finding an element in a trie is O(log n). But in practice it'll be something like log256 n, so the constant factor is very small (log256 of 2 billion = 4).

查看更多
倾城 Initia
3楼-- · 2020-07-09 10:15

I would recommend a fibonacci heap.

查看更多
登录 后发表回答