HASHING Michael Tsai 20120529 Collision collision 1 Open

  • Slides: 32
Download presentation
HASHING Michael Tsai 2012/05/29

HASHING Michael Tsai 2012/05/29

Collision 處理 注意: 要確保能夠下次也能找到同一個地方! • 兩種常用處理collision的方法: (1) Open addressing (2) Chaining Full! Find another

Collision 處理 注意: 要確保能夠下次也能找到同一個地方! • 兩種常用處理collision的方法: (1) Open addressing (2) Chaining Full! Find another empty cabinet 多出來的吊在下面

Open addressing – General Form •

Open addressing – General Form •

Open addressing – Linear probing Primary clustering: 某些open addressing的probing方法會產生一 長串填滿的格子 Input sequence of keys:

Open addressing – Linear probing Primary clustering: 某些open addressing的probing方法會產生一 長串填滿的格子 Input sequence of keys: {8, 16, 18, 5, 31, 15} [0] [1] [2] 8 8 8 16 16 16 31 31 18 18 18 5 5 5 [3] [4] [5] [6] 18 15

Open addressing – Quadratic probing •

Open addressing – Quadratic probing •

Open addressing – Double hashing •

Open addressing – Double hashing •

Chaining – Worst case •

Chaining – Worst case •

Chaining - Expected performance •

Chaining - Expected performance •

一些hash function的例子 •

一些hash function的例子 •

一些hash function的例子 • (3) shift folding • 用例子解釋: • k=12320324111220 • 每隔幾位數切一份. 例如, 三位數:

一些hash function的例子 • (3) shift folding • 用例子解釋: • k=12320324111220 • 每隔幾位數切一份. 例如, 三位數: (櫃子有1000個) • {123, 203, 241, 112, 20} • h(k)=(123+203+241+112+20)%1000=699 • (4)folding at the boundaries • {123, 302, 241, 211, 20} • h(k)=(123+302+241+211+20)%1000=897

一些hash function的例子 •

一些hash function的例子 •

例子 k h(k) A 0 100 000 A 1 100 001 B 0 101

例子 k h(k) A 0 100 000 A 1 100 001 B 0 101 000 B 1 101 001 C 1 110 001 C 2 110 010 C 3 110 011 C 5 110 101 h(k, i)=bits 0 -i of h(k) Example: h(A 0, 1)=0 h(A 1, 3)=001=1 h(B 1, 4)=1001=9

Dynamic hashing using directories directory depth= number of bits of the index of the

Dynamic hashing using directories directory depth= number of bits of the index of the hash table 00 A 0, B 0 01 A 1, B 1 10 11 000 A 0, B 0 001 A 1, B 1 C 2 010 C 2 C 3 011 C 3 C 5, overflow 100 Insert C 5 101 h(C 5, 2)=01=1 110 C 5 111 we increase d by 1 until not all h(k, d) of the keys in the cell are the same 動腦時間: 如果原本的要加入C 1呢? 如果第二步驟後加入A 4呢? 答案: Horowitz p. 412 -413 k h(k) A 0 100 000 A 1 100 001 B 0 101 000 B 1 101 001 C 1 110 001 C 2 110 010 C 3 110 011 C 5 110 101

Directoryless Dynamic hashing • r=2, q=2

Directoryless Dynamic hashing • r=2, q=2

Directoryless Dynamic hashing • 問: 再加入C 1呢? (Horowitz p. 415) 00 B 4, A

Directoryless Dynamic hashing • 問: 再加入C 1呢? (Horowitz p. 415) 00 B 4, A 0 01 A 1, B 5 10 C 2 11 C 3 r=2, q=0 insert C 5, full 000 A 0 01 A 1, B 5 10 C 2 11 C 3 100 B 4 r=2, q=1 C 5 k h(k) A 0 100 000 A 1 100 001 B 4 101 100 B 5 101 C 1 110 001 C 2 110 010 C 3 110 011 C 5 110 101

Today’s Reading Assignment • Cormen ch 11 (11. 1, 11. 2, 11. 3 except

Today’s Reading Assignment • Cormen ch 11 (11. 1, 11. 2, 11. 3 except 11. 3. 3, 11. 4) • Horowitz p. 410 -416 (posted on the course web site)