fbpx
วิกิพีเดีย

แฮชชิงคู่

แฮชชิ่งคู่ (อังกฤษ: Double Hashing) เป็นเทคนิคการเขียนโปรแกรมคอมพิวเตอร์ โดยใช้ตารางแฮชเพื่อป้องกันการชนกันของแฮช ในกรณีที่สองค่าที่แตกกต่างกันค้นหาคีย์แฮชเดียวกัน ซึ่งการแฮชสองชั้นเป็นเทคนิค Open Address การแฮชแบบสองชั้นถูกนำไปใช้ในคลังโปรแกรมที่นิยมมาก

หลักการทำงาน

1. กำหนดความกว้างของตารางแฮช และ ค่าที่ใช้เพื่อเลื่อนค่าจากตำแหน่งเดิม

2. กำหนดข้อมูลที่เราต้องการ อย่างตัวอย่างจะกำหนดให้คือ 26, 54, 94, 17, 31, 77, 44 และ 51 ความกว้างของตาราง คือ 13 ค่าที่ใช้เพื่อเลื่อนค่าจากตำแหน่งเดิม คือ 5

0 1 2 3 4 5 6 7 8 9 10 11 12
None None None None None None None None None None None None None

3. นำข้อมูลที่กำหนดให้มาคำนวณ โดย ข้อมูล ณ ตำแหน่งที่ Mod ความกว้างของตาราง ( ) จะได้   ซึ่งเป็นค่าในตำแหน่งของตารางแฮช แล้วทำต่อให้ครบทุกค่า

26 54 94 17 31 77 44 51
0 2 3 4 5 12 5 12

4. หาค่าการแฮชสองชั้นโดยใช้ ค่าที่ใช้เพื่อเลื่อนค่าจากตำแหน่งเดิม ลบกับ ข้อมูล ณ ตำแหน่งที่ Mod ค่าที่ใช้เพื่อเลื่อนค่าจากตำแหน่งเดิม  (   ) จะได้  

26 54 94 17 31 77 44 51
4 1 1 3 4 3 1 4

5. นำค่าที่ได้ในข้อที่ 3 ไปใส่ยังตารางในข้อที่ 2

5.1 ค่า 26 ตำแหน่งที่ 0

0 1 2 3 4 5 6 7 8 9 10 11 12
26 None None None None None None None None None None None None

5.2 ค่า 54 ตำแหน่งที่ 2

0 1 2 3 4 5 6 7 8 9 10 11 12
26 None 54 None None None None None None None None None None

5.3 ค่า 94 ตำแหน่งที่ 3

0 1 2 3 4 5 6 7 8 9 10 11 12
26 None 54 94 None None None None None None None None None

5.4 ค่า 17 ตำแหน่งที่ 4

0 1 2 3 4 5 6 7 8 9 10 11 12
26 None 54 94 17 None None None None None None None None

5.5 ค่า 31 ตำแหน่งที่ 5

0 1 2 3 4 5 6 7 8 9 10 11 12
26 None 54 94 17 31 None None None None None None None

5.6 ค่า 77 ตำแหน่งที่ 12

0 1 2 3 4 5 6 7 8 9 10 11 12
26 None 54 94 17 31 None None None None None None 77

5.7 ค่า 44 ตำแหน่งที่ 5 (กรณีค่าที่ตำแหน่งทับกัน จะนำค่าจากตารางในข้อที่ 4 มา บวกกับ ค่าตารางที่ 1 จะได้ 5 + 1 แล้ว Mod ด้วยความกว้างของตารางแฮช จะเท่ากับ 6)

0 1 2 3 4 5 6 7 8 9 10 11 12
26 None 54 94 17 31

44

None None None None None None 77

เมื่อได้ค่าเท่ากับ 6 แล้ว จะเป็นค่าตำแหน่งใหม่ที่ใส่ลงในตาราง

0 1 2 3 4 5 6 7 8 9 10 11 12
26 None 54 94 17 31 None None None None None None 77

ค่า 51 ตำแหน่งที่ 12 (กรณีค่าที่ตำแหน่งทับกัน จะนำค่าจากตารางในข้อที่ 4 มา บวกกับ ค่าตารางที่ 1 จะได้ 12 + 4 แล้ว Mod ด้วยความกว้างของตารางแฮช จะเท่ากับ 3)

0 1 2 3 4 5 6 7 8 9 10 11 12
26 None 54 94 17 31 44 None None None None None 77

51

เมื่อได้ค่าเท่ากับ 3 แล้ว จะเป็นค่าตำแหน่งใหม่ที่ใส่ลงในตาราง แต่เมื่อกรณียังมีค่าในตารางให้นับเพิ่มอีก 3

0 1 2 3 4 5 6 7 8 9 10 11 12
26 None 54

51

94 17 31 44 None None None None None 77

กรณียังมีค่าในตารางให้นับเพิ่มอีก 3

0 1 2 3 4 5 6 7 8 9 10 11 12
26 None 54 94 17 31

51

44 None None None None None 77

กรณียังมีค่าในตารางให้นับเพิ่มอีก 3 แต่เมื่อเจอช่องที่ว่างเปล่า สามารถนำค่านั้นลงในตารางได้ทันที

0 1 2 3 4 5 6 7 8 9 10 11 12
26 None 54 94 17 31 44 None None None None None 77

ความซับซ้อนในการทำงาน

จะสรุปได้ว่า Big(O) = O( ) Best Case คือ ขนาดของข้อมูลเป็น 0 มีค่าเดียว หรือข้อมูลมีค่ามากกว่าตาราง O(1) Worst Case คือ ขนาดของข้อมูลที่ให้ฟังก์ชั่นทำงานครบทั้งหมด O( )

ตัวอย่างการเขียนโปรแกรม

def doubleHashing(data, hashTableSize, doubleHashSize):  if len(data) > hashTableSize:  return None  listOfHashTable = [None] * hashTableSize  for i in range(len(data)):  primaryHash = data[i] % hashTableSize  doubleHash = primaryHash  if listOfHashTable[primaryHash] is None:  listOfHashTable[primaryHash] = data[i]  else:  while listOfHashTable[doubleHash] is not None:  secondary = doubleHashSize - (data[i] % doubleHashSize)  doubleHash = (doubleHash + secondary) % hashTableSize  listOfHashTable[doubleHash] = data[i]  return listOfHashTable 

อ้างอิง

Mohit Makhija . Double Hashing Code2begin. Retrieved 1 May 2018.

แฮชช, งค, แฮชช, งค, งกฤษ, double, hashing, เป, นเทคน, คการเข, ยนโปรแกรมคอมพ, วเตอร, โดยใช, ตารางแฮชเพ, อป, องก, นการชนก, นของแฮช, ในกรณ, สองค, าท, แตกกต, างก, นค, นหาค, แฮชเด, ยวก, งการแฮชสองช, นเป, นเทคน, open, address, การแฮชแบบสองช, นถ, กนำไปใช, ในคล, งโปรแ. aehchchingkhu xngkvs Double Hashing epnethkhnikhkarekhiynopraekrmkhxmphiwetxr odyichtarangaehchephuxpxngknkarchnknkhxngaehch inkrnithisxngkhathiaetkktangknkhnhakhiyaehchediywkn sungkaraehchsxngchnepnethkhnikh Open Address karaehchaebbsxngchnthuknaipichinkhlngopraekrmthiniymmak enuxha 1 hlkkarthangan 2 khwamsbsxninkarthangan 3 twxyangkarekhiynopraekrm 4 xangxinghlkkarthangan aekikh1 kahndkhwamkwangkhxngtarangaehch aela khathiichephuxeluxnkhacaktaaehnngedim2 kahndkhxmulthieratxngkar xyangtwxyangcakahndihkhux 26 54 94 17 31 77 44 aela 51 khwamkwangkhxngtarang khux 13 khathiichephuxeluxnkhacaktaaehnngedim khux 5 0 1 2 3 4 5 6 7 8 9 10 11 12None None None None None None None None None None None None None3 nakhxmulthikahndihmakhanwn ody khxmul n taaehnngthi Mod khwamkwangkhxngtarang k mod l e n g t h displaystyle k bmod l ength caid 26 mod 3 0 displaystyle 26 mod 3 0 sungepnkhaintaaehnngkhxngtarangaehch aelwthatxihkhrbthukkha 26 54 94 17 31 77 44 510 2 3 4 5 12 5 124 hakhakaraehchsxngchnodyich khathiichephuxeluxnkhacaktaaehnngedim lbkb khxmul n taaehnngthi Mod khathiichephuxeluxnkhacaktaaehnngedim N k mod N displaystyle N k mod N caid 5 k mod 5 displaystyle 5 k mod 5 26 54 94 17 31 77 44 514 1 1 3 4 3 1 45 nakhathiidinkhxthi 3 ipisyngtaranginkhxthi 25 1 kha 26 taaehnngthi 0 0 1 2 3 4 5 6 7 8 9 10 11 1226 None None None None None None None None None None None None5 2 kha 54 taaehnngthi 2 0 1 2 3 4 5 6 7 8 9 10 11 1226 None 54 None None None None None None None None None None5 3 kha 94 taaehnngthi 3 0 1 2 3 4 5 6 7 8 9 10 11 1226 None 54 94 None None None None None None None None None5 4 kha 17 taaehnngthi 4 0 1 2 3 4 5 6 7 8 9 10 11 1226 None 54 94 17 None None None None None None None None5 5 kha 31 taaehnngthi 5 0 1 2 3 4 5 6 7 8 9 10 11 1226 None 54 94 17 31 None None None None None None None5 6 kha 77 taaehnngthi 12 0 1 2 3 4 5 6 7 8 9 10 11 1226 None 54 94 17 31 None None None None None None 775 7 kha 44 taaehnngthi 5 krnikhathitaaehnngthbkn canakhacaktaranginkhxthi 4 ma bwkkb khatarangthi 1 caid 5 1 aelw Mod dwykhwamkwangkhxngtarangaehch caethakb 6 0 1 2 3 4 5 6 7 8 9 10 11 1226 None 54 94 17 31 44 None None None None None None 77emuxidkhaethakb 6 aelw caepnkhataaehnngihmthiislngintarang 0 1 2 3 4 5 6 7 8 9 10 11 1226 None 54 94 17 31 None None None None None None 77kha 51 taaehnngthi 12 krnikhathitaaehnngthbkn canakhacaktaranginkhxthi 4 ma bwkkb khatarangthi 1 caid 12 4 aelw Mod dwykhwamkwangkhxngtarangaehch caethakb 3 0 1 2 3 4 5 6 7 8 9 10 11 1226 None 54 94 17 31 44 None None None None None 77 51emuxidkhaethakb 3 aelw caepnkhataaehnngihmthiislngintarang aetemuxkrniyngmikhaintarangihnbephimxik 3 0 1 2 3 4 5 6 7 8 9 10 11 1226 None 54 51 94 17 31 44 None None None None None 77krniyngmikhaintarangihnbephimxik 3 0 1 2 3 4 5 6 7 8 9 10 11 1226 None 54 94 17 31 51 44 None None None None None 77krniyngmikhaintarangihnbephimxik 3 aetemuxecxchxngthiwangepla samarthnakhannlngintarangidthnthi 0 1 2 3 4 5 6 7 8 9 10 11 1226 None 54 94 17 31 44 None None None None None 77khwamsbsxninkarthangan aekikhcasrupidwa Big O O n 2 displaystyle n 2 Best Case khux khnadkhxngkhxmulepn 0 mikhaediyw hruxkhxmulmikhamakkwatarang O 1 Worst Case khux khnadkhxngkhxmulthiihfngkchnthangankhrbthnghmd O n 2 displaystyle n 2 twxyangkarekhiynopraekrm aekikhdef doubleHashing data hashTableSize doubleHashSize if len data gt hashTableSize return None listOfHashTable None hashTableSize for i in range len data primaryHash data i hashTableSize doubleHash primaryHash if listOfHashTable primaryHash is None listOfHashTable primaryHash data i else while listOfHashTable doubleHash is not None secondary doubleHashSize data i doubleHashSize doubleHash doubleHash secondary hashTableSize listOfHashTable doubleHash data i return listOfHashTablexangxing aekikhMohit Makhija Double Hashing Code2begin Retrieved 1 May 2018 ekhathungcak https th wikipedia org w index php title aehchchingkhu amp oldid 7605537, wikipedia, วิกิ หนังสือ, หนังสือ, ห้องสมุด,

บทความ

, อ่าน, ดาวน์โหลด, ฟรี, ดาวน์โหลดฟรี, mp3, วิดีโอ, mp4, 3gp, jpg, jpeg, gif, png, รูปภาพ, เพลง, เพลง, หนัง, หนังสือ, เกม, เกม