Equivalence testing of n objects

前端 未结 4 1259
一向
一向 2021-01-07 05:20

Assume that we are given \'n\' objects and a subroutine that takes two inputs and says if they are equivalent or not (e.g. it can give output as 1 if they are equal).

<
4条回答
  •  爱一瞬间的悲伤
    2021-01-07 05:45

    Here is a classical divide and conquer solution which gives O(n log n)

    split into two subsets, A1 and A2, ..., and show T(n) is O(n log n). If A has a majority element v, v must also be a majority element of A1 or A2 or both. The equivalent contra-positive restatement is immediate: (If v is <= half in each, it is <= half in the total.) If both parts have the same majority element, it is automatically the majority element for A. If one of the parts has a majority element, count the number of repetitions of that element in both parts (in O(n) time) to see if it is a majority element. If both parts have a majority, you may need to do this count for each of the two candidates, still O(n). This splitting can be done recursively. The base case is when n = 1. A recurrence relation is T(n) = 2T(n/2) + O(n), so T(n) is O(n log n) by the Master Theorem.

    http://anh.cs.luc.edu/363/handouts/MajorityProblem.pdf

提交回复
热议问题