伪代码 2 个简单部分的等价
我要实现一种算法,我在纸上的想法与我们教科书中建议的伪代码略有不同。我试图说服自己,当必须分别在下面的“二”和“一”中实现“包含”和生成所有子集时,下面的两个伪代码片段将执行相同的操作,而在时间复杂度顺序上没有任何重大差异。我很难想出一些严格的东西来说服我。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这取决于您如何实现子集生成和包含函数。我的猜测是,在大多数情况下,生成所有这些子集是不值得的,并且(因为
a
位于A_t
中,当且仅当a
位于>A
并且在t
的子集集合中,即a
在A_t
中当且仅当a
位于A
中,并且t
包含a
),您可以将第一个代码段重写为,而第二个代码段则为
(在这两种情况下都假设对于所有
A
中的a
,a.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 inA_t
iffa
is inA
and in the set of subsets oft
, i.e.a
is inA_t
iffa
is inA
andt
containsa
) you can just rewrite your first snippet towhile your second snippet is
(in both cases assuming that for all
a
inA
,a.count
is initially set to 0)