如何重新present Java中的一个位向量,所以我可以在O查询(log n)的 [英] How to represent a bit vector in java so I can search in O(log n)
问题描述
予有元素的列表,我需要创建由比特为每个元素的签名。我将结束与比特向量的列表。从那以后,我需要的位向量组的列表进行排序lexiographically。从那以后,我需要寻找在排序向量列表中的位向量。
I have a list of elements and I need to create a signature made of bits for each element. I will end up with a list of bit vectors. After that I need to sort that list of bit vectors lexiographically. After that I need to search for a bit vector in the sorted vectors list.
我发现,如果我重新present签名作为字符串,排序将需要O(N)和搜索将使用二进制搜索,其中M是的特征码的长度取0(M logN)的。
I found that if I represent the signature as a String, sorting will take O(N) and searching will take O(M logN) using binary search, where M is the length of the string signature.
不过,我发现,与一般的数字,排序需要O(N LOGN)和搜索需要O(logN)的使用二进制搜索。
But I found that with numbers in general, sorting takes O(n LogN) and searching will take O( logN) using binary search.
我的问题是如何重新present位向量在Java中,这样我就可以按字典顺序排序,并实现了与数字打交道,一般相同的性能?
My question is how to represent the bit vector in java so that I can sort lexicographically and achieves the same performance of dealing with numbers in general?
我最关心的是实现这一O(logN)的搜索时间使用二进制搜索,因为有人声称在一份文件来实现这一点,但没有提供任何线索如何。
I'm mostly concerned with achieving that O(logN) search time using binary search, as someone claims to achieve this in a paper but doesn't provide any clue how.
推荐答案
通过@Keith扩大的建议,使用 java.util.BitSet中
,但它延伸到实施可比
。实现一个lexiographic比较适合您的域名。也许调整散code()
和等于()
速度。
Expanding on the suggestion by @Keith, use a java.util.BitSet
, but extend it to implement Comparable
. Implement a lexiographic compare suitable to your domain. Perhaps tweak hashCode()
and equals()
for speed.
在这一点上,你可以很容易地进行排序位集的收集,并使用二进制搜索。
At that point you can easily sort the Collection of BitSets, and use a binary search.
另外,像往常一样,你可以写一个比较
,而不是做lexiographic比较。
Alternatively, as usual, you could write a Comparator
instead to do the lexiographic compare.
这篇关于如何重新present Java中的一个位向量,所以我可以在O查询(log n)的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!