東京大学 情報理工学系研究科 創造情報学専攻 2008年8月実施 筆記試験 第1問
Author
itsuitsuki
Description
Given n records r1,r2,…,rn, each has a key k1,k2,…,kn, respectively. Given a key, consider searching for the corresponding record. For a given query key v, if there is a key kj where v=kj (1≤j≤n) then the search is successful and the record rj is returned, otherwise the search fails and the failure is returned.
Let pj be the probability where the query key v is kj, and pmiss be the probability of the search failure. The computation time is almost proportional to the number of comparisons with v. Let us calculate the average number of comparisons C and the maximum number of comparisons Cmax.
(1) Consider a sequential search that compares v with keys from k1 to kn.
(a) Obtain C and Cmax when p1=p2=⋯=pn=2n1,pmiss=21.
(b) Prove that C≤2 when p1=21,p2=41,…,pn=2n1,pmiss=2n1.
(2) Consider a binary search after sorting the keys. Let n=2m−1 (m is a natural number), and moreover, one comparison will determine whether v<kj,v=kj or v>kj.
(a) Obtain C and Cmax for each case of m=1,2,3,4, when p1=p2=⋯=pn=pmiss.
(b) Obtain C and Cmax as a function of m when p1=p2=⋯=pn=pmiss.
(3) Consider a search using a hash table. The records r1,…,r8 are inserted into the hash table of size S=17 using the hash function h(x)=xmodS. Let the key values each be k1=10,k2=1,k3=17,k4=97,k5=21,k6=4,k7=39,k8=73.
(a) Draw the structure of the hash table by choosing an appropriate method for avoiding collision.
(b) Obtain C and Cmax when p1=p2=⋯=p8,pmiss=0.
(4) Describe in general the advantages and disadvantages of sequential search, binary search and search using a hash table.
题目描述
给定 n 条记录 r1,r2,…,rn,对应键为 k1,k2,…,kn。对查询键 v,若存在 v=kj(1≤j≤n),则查找成功并返回 rj;否则返回失败。记 v=kj 的概率为 pj,失败概率为 pmiss。计算时间近似与同 v 比较的次数成正比,以下均要求平均比较次数 C 和最大比较次数 Cmax。
-
按 k1 到 kn 的顺序进行顺序查找。
- 当 p1=⋯=pn=2n1、pmiss=21 时,求 C 与 Cmax。
- 当 p1=21,p2=41,…,pn=2n1、pmiss=2n1 时,证明 C≤2。
-
将键排序后进行二分查找。令 n=2m−1,其中 m 为自然数;一次比较即可判断 v<kj、v=kj 或 v>kj。
- 在 p1=⋯=pn=pmiss 时,分别对 m=1,2,3,4 求 C 与 Cmax。
- 在相同等概率条件下,用 m 表示 C 与 Cmax。
-
使用大小 S=17 的哈希表和哈希函数 h(x)=xmodS,依次插入键值
k1=10, k2=1, k3=17, k4=97, k5=21, k6=4, k7=39, k8=73.
- 自选一种合适的冲突处理方法,画出哈希表结构。
- 当 p1=⋯=p8、pmiss=0 时,求 C 与 Cmax。
-
概括比较顺序查找、二分查找和哈希表查找各自的优缺点。