算法:检查集合包含的最快方法是什么?

发布于 2024-09-14 12:28:48 字数 42 浏览 1 评论 0原文

拥有一个包含 N 个集合的数组,生成这些集合的包含图的最快方法是什么?

Having an array of N sets, what is the fastest way to generate the inclusion graph of these sets?

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

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

发布评论

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

评论(4

尘世孤行 2024-09-21 12:28:48

如果您有一组 N 个集合,其中每个集合都不包含另一个集合,并且大小都相同,则您将需要进行 N^2 集合比较。

If you have a set of N sets none of which contain another, all of the same size, you are going to need to do N^2 set comparisons.

后来的我们 2024-09-21 12:28:48

好吧,让我们从最坏的情况来推理,为了简单起见,假设您有一个有效的集合表示,您可以在恒定时间内检查成员资格。

要确定一组 X 是否是 Y 的子集,您必须执行 |X| Y 上的隶属度测试数量。所需时间与 |X| 成线性比例。

因此,如果你有 N 个集合,并且想要找出哪些集合是哪些其他集合的子集,则必须进行 N2 这样的子集测试,因此我认为最终的复杂度为O(AN2) 其中 A 是最大集合中的元素数量。

即使你可以做一些聪明的事情来同时决定“X子集Y”和“Y子集X”,你所获得的收益也不会超过2倍,因此复杂性不会提高。

Well, let's reason about it in terms of worst case, and for the sake of simplicity assume that you have an efficient set representation, in which you can check for membership in constant time.

To determine if one set X is a subset of Y, you have to do |X| number of membership tests on Y. Takes time linearly proportional to |X|.

So if you have N sets, and want to figure out which sets are subset of which other sets, you would have to do N2 such subset-tests, thus I suppose you end up with a complexity of O(AN2) where A is the number of elements in the largest set.

Even if you could do something smart to decide "X subset Y" and "Y subset X" at the same time, you wouldn't gain more than a factor 2, so the complexity would not improve.

泅渡 2024-09-21 12:28:48

首先,您可以证明,在给定 n 个集合的情况下,该图将包含 O(n^2) 条边:考虑集合 A1, ..., An,其中每个 Ak = {1, ..., k}。那么A1子集A2,A1子集A3,...,A1子集An,A2子集A3,...,即包含图中的n(n-1)/2条边。

鉴于此,我可以想到一个相当简单的方法来解决这个问题。设 Ai 可能是 Aj 的子集,当且仅当 Aj 中有某个 x 不在 Ai 中。现在,Ai 子集 Aj 当且仅当 Ai 可能是 Aj 的子集,而不是 Aj 可能是 Ai 的子集。

现在,对于每个元素 x 将您的集合分为两部分:包含 x 的集合和不包含 x 的集合。后者可能是前者的子集。将相应的边添加到可能子集图中。每当我们有一对在每个方向上连接的顶点时,我们就知道两个顶点都不是另一个顶点的子集。对于 m 个元素和 n 个集合的宇宙,该算法的复杂度为 O(mn^2)。

等等瞧!

First off, you can show that this graph will contain O(n^2) edges given n sets: consider sets A1, ..., An with each Ak = {1, ..., k}. Then A1 subset A2, A1 subset A3, ..., A1 subset An, A2 subset A3, ..., which is n(n-1)/2 edges in the inclusion graph.

Given that, I can think of a reasonably simple way to tackle this problem. Let Ai maybe-subset Aj iff there is some x in Aj which is not in Ai. Now, Ai subset Aj iff Ai maybe-subset Aj and not Aj maybe-subset Ai.

Now, for each element x divide your sets into two: those that contain x and those that don't. The latter maybe-subset the former. Add the corresponding edges to the maybe-subset graph. Whenever we have a pair of vertices connected in each direction, we know neither vertex is a subset of the other. This algorithm is O(mn^2) for a universe of m elements and n sets.

Et voila!

短暂陪伴 2024-09-21 12:28:48
  • 假设您可以按顺序迭代集合。

使用归并排序。

您的输出是包含(行,列)的 MxM 矩阵。这被初始化为全 TRUE。这是您的算法:

  1. 如果所有集合为空,则退出
  2. 查看每个非空集合的第一项
  3. 查找这些集合中的最小值
  4. 哪些所查看的集合包含这些最小项目?
  5. 对于步骤 4 中标识的集合,将includes(*,那些集合)设置为 false
  6. 弹出步骤 4 中标识的每个集合的顶部项目
  7. GOTO 0

如果所有集合都是空的,则您有一个 MxM 集合

  • Assumes you can iterate over the sets in order.

Use merge sort.

Your output is an MxM matrix of includes(Row, Column). This is initialized to all TRUE. Here is your algorithm:

  1. If all sets empty then exit
  2. Peek at the first item of each non-empty set
  3. Find the minimum of these
  4. Which of the peeked sets contains those minimum items?
  5. For the sets identified in step 4, set includes(*, those sets) to false
  6. Pop top item of each set identified in step 4
  7. GOTO 0

If all the sets are empty you have a MxM set of

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