有一种数据结构可以返回包含这个元素的所有集合吗?

huangapple go评论64阅读模式
英文:

Is there a data structure that allows you to return all the sets that contain this element?

问题

我正在寻找一种数据结构,它能够返回包含特定元素的所有集合,同时支持集合的添加和删除操作。

显而易见的方法是将每个集合存储为哈希表,并在每次查询时检查集合是否包含给定元素。

但存在一些限制:

  1. 给定的集合可以相互交叉或甚至包含在彼此内。
  2. 保证元素的数量远大于集合的数量。
  3. 需要支持更新和删除集合的操作。

更具体地说,

给定一些集合 S_1, S_2, S_3, ... S_n。每个集合由其元素的枚举定义。
我需要一种数据结构,允许我有效地执行以下操作:
get(E) - 获取包含给定元素 E 的所有集合
add(i, S) - 添加第 i 个集合
update(i, S) - 更新第 i 个集合
delete(i) - 删除第 i 个集合

英文:

I am looking for a data structure that returns all sets that contain a given element, as well as supporting set addition and deletion operations

The obvious approach is to store each set as a hash table and for each query check whether the sets contain a given element

But there are some limitations

  1. The given sets can intersect each other or even contain
  2. It is guaranteed that the number of elements is much larger than the number of sets
  3. It is necessary to support the operation of updating and deleting sets

More stricly

Given a number of sets S_1, S_2, S_3, ... S_n. Each set is defined by an enumeration of its elements <br>
I need a data structure that will allow me to effectively perform the following operations: <br>
get(E) - Get all sets that contain a given element E <br>
add(i, S) - Add the i-th set <br>
update(i, S) - Update the i-th set <br>
delete(i) - Delete the i-th set

答案1

得分: 0

你要找的数据结构是一个(稀疏)二分(无向)图

有一种数据结构可以返回包含这个元素的所有集合吗?
(CC BY-SA 4.0 Watchduck)

在这里,对于每个元素 e 和每个集合 S,你会有一个额外的顶点。对于每个 eS 中,你会有一条边 {e,S}E 中。然后,检查集合 S 简化为:给我 S 的邻居。检查所有包含 e 的集合简化为:给我 e 的邻居。向集合添加值意味着插入新边,删除元素意味着删除边。你可能还想确保完全删除孤立的顶点。

英文:

The data structure you are looking for is a (sparse) bipartite (undirected) graph.

有一种数据结构可以返回包含这个元素的所有集合吗?
(CC BY-SA 4.0 Watchduck)

Here, for each element e and each set S you would have an extra vertex. And for each e in S you would have an edge {e,S} in E. Then, examining set S boils down to: give me S's neighbours. Examining all sets e belongs to boils down to: give me e's neighbours. Adding values to sets means inserting new edges, and removing elements means removing edges. You might also want to make sure to entirely remove isolated vertices.

huangapple
  • 本文由 发表于 2023年6月11日 23:40:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76451224.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定