开发者

Equivalence of 2 simple parts of pseudocode

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
}


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜