← 回題庫

卷別

資料結構與演算法

選擇

國立臺灣大學 2024 · 考試 · 第 4 題

考科演算法

(10 points) Consider a hash function hh that maps numerous keys into an array AA, indexed from 0 to n−1n-1. The concern is that excessive collisions arising from the hashed keys can potentially cause inefficiency within the hash table. The following pseudocode, while not complete, implements a function that utilizes the divide-and-conquer strategy to identify the case where strictly more than half of the keys share the same hash value. The function takes the hash function hh and the subarray AA with the index range from lowlow to highhigh as input and returns the majority hash value shared by over ⌊(high−low+1)/2⌋\lfloor (high - low + 1)/2 \rfloor keys. If the keys stored in the subarray A[low..high]A[low..high] do not have such a majority hash value, the function returns −1-1.

1: function FindMajorityHashValue(h, A, low, high)
2:     if low = high then return h(A[low]) end if
3:     mid ← ⌊(low + high)/2⌋
4:     leftMajority  ← FindMajorityHashValue(h, A, low, mid)
5:     rightMajority ← FindMajorityHashValue(h, A, mid + 1, high)
6:     leftCount  ← CountElement(A, low, high, leftMajority)
7:     rightCount ← CountElement(A, low, high, rightMajority)
8:     [ ... ]
9: end function

In the pseudocode, CountElement(A,low,high,val)\text{CountElement}(A, low, high, val) calculates and returns the count of keys in the subarray A[low..high]A[low..high] whose hashed values are equal to the given valval. Please select the correct description(s) below.

  • (A)
    If leftMajority=rightMajority=−1leftMajority = rightMajority = -1, A[low..high]A[low..high] may still have a majority hash value.
  • (B)
    If A[low..high]A[low..high] has a majority hash value and rightMajority=−1rightMajority = -1, the function should return leftMajorityleftMajority.
  • (C)
    If leftCount=rightCountleftCount = rightCount, the function should return −1-1.
  • (D)
    Suppose that an implementation of CountElement runs within Θ(ℓ)\Theta(\ell) time, where ℓ=high−low+1\ell = high - low + 1. The time complexity of FindMajorityHashValue can be O(nlog⁡n)O(n \log n).
  • (E)
    Suppose that a magical implementation of CountElement runs within Θ(ℓ/log⁡ℓ)\Theta(\ell/\log \ell) time, where ℓ=high−low+1\ell = high - low + 1. The time complexity of FindMajorityHashValue can be O(n)O(n).

標準解析

AI 已複查