【发布时间】:2019-07-16 05:05:02
【问题描述】:
我有一个应用程序,其中有一个O(n) 集合列表。
每组Set(i) 是一个n-vector。假设n=4,例如,
Set(1) 可以是[0|1|1|0]
Set(2) 可以是[1|1|1|0]
Set(3) 可以是[1|1|0|0]
Set(4) 可以是[1|1|1|0]
我想处理这些集合,以便作为输出,我只得到其中唯一的集合。所以,在上面的例子中,我会得到输出:
Set(1), Set(2), Set(3)。请注意,Set(4) 被丢弃,因为它与 Set(2) 相同。
一种相当蛮力的计算方式给了我O(n^3)的最坏情况界限:
Given: Input List of size O(n)
Output List L = Set(1)
for(j = 2 to Length of Input List){ // Loop Outer, check if Set(j) should be added to L
for(i = 1 to Length of L currently){ // Loop Inner
check if Set(i) is same as Set(j) //This step is O(n) since Set() has O(n) elements
if(they are same) exit inner loop
else
if( i is length of L currently) //so, Set(j) is unique thus far
Append Set(j) to L
}
}
n 没有先验界限:它可以任意大。这似乎排除了使用将二进制集映射为十进制的简单散列函数。我可能是错的。
除了O(n^3)之外,还有其他方法可以在最坏情况下运行时间更好吗?
【问题讨论】:
标签: algorithm data-structures time-complexity complexity-theory