伪代码 2 个简单部分的等价

发布于 2024-10-19 02:32:15 字数 594 浏览 4 评论 0原文

我要实现一种算法,我在纸上的想法与我们教科书中建议的伪代码略有不同。我试图说服自己,当必须分别在下面的“二”和“一”中实现“包含”和生成所有子集时,下面的两个伪代码片段将执行相同的操作,而在时间复杂度顺序上没有任何重大差异。我很难想出一些严格的东西来说服我。

T 和 A 是有限集合 I 的子集的集合。没有集合或子集是空的,并且每个集合都有一个称为“计数”的“字段”。

片段一:

For-each t in T do {
  A_t = A intersected with the set of all non-empty subsets of t
  For-each a in A_t do {
    a.count++
  }
}

片段二:

For-each a in A do {
  a.count = count(a, T)
}

这里计数定义为

count(a, T) {
  c = 0
  For-each t in T do {
    if (t contains a) {
      c++
    }
return c
}

I am to implement an algorithm where the way I thought it out on paper is slightly different from the pseudo-code suggested in our text book. I am trying to convince myself that the 2 snippets of pseudo below will do the same thing without any major difference in order of time complexity when having to implement the 'contains' and generation of all subsets in respectively Two and One below. I am having trouble coming up with something rigorous that will convince me.

T and A are sets of subsets of a finite set I. No set or subset is empty and every set has a "field" called 'count'.

Snippet One:

For-each t in T do {
  A_t = A intersected with the set of all non-empty subsets of t
  For-each a in A_t do {
    a.count++
  }
}

Snippet Two:

For-each a in A do {
  a.count = count(a, T)
}

Here count is defined by

count(a, T) {
  c = 0
  For-each t in T do {
    if (t contains a) {
      c++
    }
return c
}

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

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

发布评论

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

评论(1

污味仙女 2024-10-26 02:32:15

这取决于您如何实现子集生成和包含函数。我的猜测是,在大多数情况下,生成所有这些子集是不值得的,并且(因为 a 位于 A_t 中,当且仅当 a 位于 >A并且在t的子集集合中,即aA_t中当且仅当a位于 A 中,并且 t 包含 a ),您可以将第一个代码段重写为,

For-each t in T do {
  For-each a in A do {
    if (t contains a){
      a.count++
    }
  }
}

而第二个代码段则为

For-each a in A do {
  For-each t in T do {
    if (t contains a){
      a.count++
    }
  }
}

(在这两种情况下都假设对于所有A 中的 aa.count 初始设置为 0)

It depends on how you implement your subset generation and contains function. My guess is that in most cases, generating all those subsets is not worth it and (as a is in A_t iff a is in A and in the set of subsets of t, i.e. a is in A_t iff a is in A and t contains a) you can just rewrite your first snippet to

For-each t in T do {
  For-each a in A do {
    if (t contains a){
      a.count++
    }
  }
}

while your second snippet is

For-each a in A do {
  For-each t in T do {
    if (t contains a){
      a.count++
    }
  }
}

(in both cases assuming that for all a in A, a.count is initially set to 0)

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