您的位置:首页 > 其它

算法导论 第十二章总结

2011-02-26 17:17 169 查看
1.直接寻址表:全集U中每一个的数据对.直接寻址表中的一个下标。

DIRECT-ADDRESS-SEARCH(T, k)

return T [k]

DIRECT-ADDRESS-INSERT(T, x)

T[key[x]] ← x

DIRECT-ADDRESS-DELETE(T, x)

T[key[x]] ← NIL

Each of these operations is fast: only O(1) time is required.

在数据量较小时可以使用。

2.散列表:用空间较小的表来对应全集中的元素。

冲突解决:链表。

3.散列表对应全集元素要用到散列函数。

(1)除法

例题: A prime not too close to an exact power of 2 is often a good choice for m. For example, suppose we wish to allocate a hash table, with collisions resolved by chaining, to hold roughly n = 2000 character strings, where a character has 8 bits. We don't mind examining an average of 3 elements in an unsuccessful search, so we allocate a hash table of size m = 701. The number 701 is chosen because it is a prime near 2000/3 but not near any power of 2. Treating each key k as an integer, our hash function would be

h(k) = k mod 701.

也就是说 m 的选择在于不接近2^n的质数。

(2)乘法

h(k)=

-1。

例题:As an example, suppose we have k = 123456, p = 14, m = 214 = 16384, and w = 32. Adapting Knuth's suggestion, we choose A to be the fraction of the form s/232 that is closest to

, so that A = 2654435769/232. Then k · s = 327706022297664 = (76300 · 232) + 17612864, and so r1 = 76300 and r0 = 17612864. The 14 most significant bits of r0 yield the value h(k) = 67.

(3)Universal法:使用的散列函数不再是固定的一个函数,而是随机函数。

ha,b(k)=( (ak+b) mod p ) mod m.

4.open addressing

HASH-INSERT(T, k)

1 i ← 0

2 repeat j ← h(k, i)

3 if T[j] = NIL

4 then T[j] ← k

5 return j

6 else i ← i + 1

7 until i = m

8 error "hash table overflow"

HASH-SEARCH(T, k)

1 i ← 0

2 repeat j ← h(k, i)

3 if T[j] = k

4 then return j

5 i ← i + 1

6 until T[j] = NIL or i = m

7 return NIL

这里要注意的是删除数据的操作。由于查找算法遇到空或i=m时结束,所以删除节点不能再原位置存储NIL,而应存储deleted作为删除标志。

(1) 线性探查:h(k,i)=(h(k)+i) mod m

(2) 平方探查:h(k,i)=(h’(k)+c1 i2 +c2 i2) mod m,为了充分利用散列表的空间,应该对c1 , c2 的值进行限制。比如,可以让c1 = c2 =1/2

(3) 双向探查:h(k, i) = (h1(k) + ih2(k)) mod m,

h1(k)=k mod m,

h2(k) =1 + (k mod m'),

where m' is chosen to be slightly less than m (say, m - 1). For example, if k = 123456, m = 701, and m' = 700, we have h1(k) = 80 and h2(k) = 257, so the first probe is to position 80, and then every 257th slot (modulo m) is examined until the key is found or every slot is examined.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: