简体   繁体   English

证明算法有下界

[英]Prove that an algorithm has a lower bound

I'm trying to prove this problem:我试图证明这个问题:

if an algorithm exists that can determine if a sorted list of n elements has duplicate elements in it, than the number of comparisons needed has a lower bound of n-1 .如果存在可以确定 n 元素的排序列表中是否包含重复元素的算法,则所需比较次数的下限为n-1

I'm not quite familiar with lower and higher bounds and I seem to confuse it, can someone help me with an easy to understand proof?我对下限和上限不太熟悉,而且我似乎很困惑,有人可以帮助我提供一个易于理解的证明吗?

The problem statement is not rigorous.问题陈述不严谨。 It should say "the number of comparisons in the worst case ".它应该说“最坏情况下的比较次数”。

In a sorted array, there are n-1 relations between pairs of successive elements, which are either < or = .在有序数组中,连续元素对之间存在n-1关系,即<= If all elements are different, you cannot deduce the outcome of a comparison from that of other comparisons.如果所有元素都不同,则无法从其他比较的结果中推断出比较的结果。 Hence you cannot avoid an exhaustive search, taking up to n-1 tests.因此,您无法避免详尽的搜索,最多进行n-1次测试。


By the way, n-1 is also an upper bound on the worst case, as after the exhaustive search you always have the answer.顺便说一句,在最坏的情况下, n-1也是一个上限,因为经过详尽的搜索后,您总能找到答案。


In the best case, when the first two elements are equal, you find the answer after exactly 1 comparison.在最好的情况下,当前两个元素相等时,您可以在恰好1次比较后找到答案。 Hence, lower and upper bounds on the best case are both 1 .因此,最佳情况的下限和上限均为1

If you have the array list = {1, 2, 3, 4, 5} , you have to compare the 1 and the 2, the 2 and the 3, the 3 and the 4 and so on, to determine if it has any duplicates in it.如果你有数组list = {1, 2, 3, 4, 5} ,你必须比较 1 和 2, 2 和 3, 3 和 4 等等,以确定它是否有任何在其中重复。

So the total number of comparisons is 4 and the total number of elements is 5 and thus n-1 comparisons are needed.所以比较的总数是 4,元素的总数是 5,因此需要n-1比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM