การแฮช (Hashing)
แนะนำใหม่
15:07
ประเภทข้อมูลที่กำหนดโดยผู้ใช้
ฟิลด์ข้อความธรรมดาจะเก็บข้อมูลไร้สาระได้อย่างสบายใจ ขอชนิดยานพาหนะมา คนหนึ่งพิมพ์ "กล้วย" — โปรแกรมรับไว้โดยไม่บ่น แต่หากคุณ…
บรรยายภาษาอังกฤษ · สลับคำบรรยายภาษาอังกฤษและภาษาจีนได้
ในบทเรียนนี้
| English | ความหมาย |
|---|---|
| key/kiː/ | คีย์ |
| collision-resolution strategy | A method for handling keys that produce the same hash address. |
| hash function/hæʃ ˈfʌŋkʃn/ | ฟังก์ชันแฮช |
| hashing algorithms | Methods that calculate a storage address from a record's key. |
| slot/slɒt/ | A position in a hash table where a record can be stored. |
| search/sɜːtʃ/ | To look through data for an item that matches a given value. |
| insert/ˈɪnsɜːt/ | To add a new item to a data structure, file or table. |
| address/əˈdres/ | ที่อยู่ |
| deterministic/dɪˌtɜːmɪˈnɪstɪk/ | แบบกำหนดได้ |
| modulo/məˈdjʊləʊ/ | A method that uses the remainder after division to find an address. |
| folding | A method that splits a key into parts and adds them before using modulo. |
| string hash | A method that adds character codes and uses modulo to find an address. |
| character codes | Numbers used to represent characters in a computer. |
| collision/kəˈlɪʒn/ | การชนกัน |
| hash tables | Data structures that use keys to calculate where records are stored. |
| Linear probing/ˈlɪnɪə ˈprəʊbɪŋ/ | A method that checks slots one after another until it finds a free slot. |
| cluster/ˈklʌstə/ | To form a group of records in slots next to each other. |
| Chaining/ˈtʃeɪnɪŋ/ | A method that keeps records with the same hash address in a linked list. |
| linked list/lɪŋkt lɪst/ | A sequence of nodes connected by references or pointers; the nodes need not occupy adjacent memory locations. |
| clustering/ˈklʌstərɪŋ/ | The forming of groups of records in slots next to each other. |
| pointers/ˈpɔɪntəz/ | A value that identifies a memory location or refers to another stored object. |
| Rehashing | A method that uses a second hash function to find another slot. |
| load factor/ləʊd ˈfæktə/ | ปัจจัยการโหลด |
| probe sequences | The slots checked, in order, when searching for or inserting a record. |
| linear search/ˈlɪnɪə sɜːtʃ/ | Checking items in sequence until the required item is found or the list ends. |
การหาแถวหนึ่งในห้าสิบล้านโดยไม่ดู
- เครื่องคิดเงินในซูเปอร์มาร์เก็ตสแกนบาร์โค้ดและราคาปรากฏขึ้นก่อนที่คุณจะปล่อยสินค้า ไฟล์ผลิตภัณฑ์มีห้าสิบล้านบรรทัด
- ไม่มีใครค้นหาพวกมัน หมายเลขบาร์โค้ดผ่านการคำนวณสั้นๆ ที่สร้างตำแหน่งขึ้น และคอมพิวเตอร์อ่านตำแหน่งนั้น: อ่านครั้งเดียว เปรียบเทียบไม่ได้ ใช้เวลาเท่าเดิมไม่ว่าไฟล์จะมีแถวห้าสิบแถวหรือห้าสิบล้านแถว
- การคำนวณนี้คือ ฟังก์ชันแฮช และแนวคิดทั้งหมดขึ้นอยู่กับมัน: อย่าจัดเก็บข้อมูลในที่ที่มันจะพอดี แต่จัดเก็บในที่ที่คีย์ของมันบอกให้ไปอยู่
- This lesson is hashing algorithms, what happens when two keys want the same slot, and how to search and insert.
Hash function
- Hash function หรือ hashing algorithm รับ key ของเรคอร์ดและผลิต address ที่เรคอร์ดถูกจัดเก็บ
- ตัวที่ดีควร fast, deterministic (key เดียวกันให้ address เหมือนกันเสมอ) และกระจาย keys evenlyAcross the available slots
- สำหรับช่อง $N$ หลักสูตรคาดหวัง 3 วิธี: โมดูลัส (modulo),
address ← key MOD N; การพับ (folding), แบ่งคีย์เป็นส่วน แล้วบวกกัน จากนั้น MOD $N$; แฮชสตริง (string hash), บวกค่าโค้ดของตัวอักษรทั้งหมด แล้ว MOD $N$
A hash function:
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
A hash function maps a key to an address, enabling near-instant direct lookup.
What makes a hash function a good one? Select ทั้งหมด that apply.
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Fast, deterministic and evenly spread. No realistic hash avoids collisions entirely, which is why every design includes a resolution strategy.
ตัวอย่างทำแบบฝึกหัด: ใช้แต่ละอัลกอริทึม
- ไฟล์มี 10 ช่อง หมายเลข 0 ถึง 9 คีย์ 4517 จะไปอยู่ที่ไหน?
- โมดูลัส: $4517 \bmod 10 = 7$, ดังนั้นอยู่ที่ช่อง 7
- การพับ คู่ละสองหลัก: $45 + 17 = 62$, แล้ว $62 \bmod 10 = 2$, ดังนั้นอยู่ที่ช่อง 2
- และคีย์ "CAB" โดยใช้แฮชสตริง? $67 + 65 + 66 = 198$, แล้ว $198 \bmod 10 = 8$, ดังนั้นอยู่ที่ช่อง 8
- แสดงการคำนวณ คะแนนให้ที่วิธีคิด ไม่ใช่แค่เลขช่อง

Using the modulo hash address ← key MOD N with key = 27 and N = 10, what address is produced?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
27 MOD 10 = 7 (the remainder when 27 is divided by 10).
A table has 10 slots. Using folding in pairs on key 4517 (add 45 and 17, then MOD 10), which slot does it go to?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
45 + 17 = 62, and 62 MOD 10 = 2. The same key under a modulo hash would go to slot 7 instead.
การชนกัน (Collisions)
- การชนกัน เกิดขึ้นเมื่อคีย์ที่แตกต่างกันสองตัวแฮชไปที่ ที่อยู่เดียวกัน ในระบบแฮชใดๆ และไฟล์จริงใด การชนกันเป็นเรื่องแน่นอน ดังนั้นกลยุทธ์การจัดการจึงเป็นส่วนหนึ่งของการออกแบบตั้งแต่ต้น ไม่ใช่สิ่งที่มาคิดทีหลัง
- Linear probing จะบันทึกข้อมูลลงใน ช่องว่างถัดไป โดยวนกลับไปยังจุดเริ่มต้นเมื่อถึงท้ายตาราง ทำง่ายแต่ข้อมูลจะ รวมกลุ่ม (cluster): พื้นที่เต็มจะขยายออก และทุกคีย์ที่ตกลงในพื้นที่นี้จะใช้เวลาค้นหานานขึ้น
- Chaining ทำให้แต่ละช่องเป็นหัวของ ลิสต์เชื่อมโยง (linked list) ของบันทึกทั้งหมดที่แฮชมาที่ช่องนั้น ไม่มีการรวมกลุ่ม แต่ใช้หน่วยความจำเพิ่มสำหรับพอยน์เตอร์และต้องเดินตามลิสต์เล็กน้อย
- Rehashing ใช้ฟังก์ชันแฮชที่สองเพื่อหาช่องอื่น กระจายคีย์ได้ดีขึ้นแต่เสียเวลาในการคำนวณมากขึ้น

A collision occurs when:
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Two keys mapping to the same slot is a collision; it must be resolved by probing, chaining or rehashing.
Match each collision-handling idea to what it does.
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Collisions are resolved by chaining or probing; keeping the load factor low keeps lookups near O(1).
การค้นหาและการแทรก
- การแทรก: แฮชคีย์ หากช่องว่าง ให้บันทึกข้อมูลลง หากไม่ว่าง ให้ทำตามกลยุทธ์แก้ไข คือช่องว่างถัดไปสำหรับ linear probing หรือหน้าสุดของลิสต์ในช่องนั้นสำหรับ chaining
- การค้นหา: แฮชคีย์แล้วอ่านช่องนั้น หากคีย์ที่เก็บไว้ตรงกัน พบบันทึกแล้ว หากไม่ตรง ให้ทำตามกลยุทธ์เดิม จนกว่าคีย์จะตรงหรือพบ ช่องว่าง ซึ่งพิสูจน์ว่าไม่มีบันทึกนี้อยู่ในไฟล์
- ทั้งสองกระบวนการใช้ กลยุทธ์เดียวกัน การค้นหาที่หยุดทันทีเมื่อคีย์ไม่ตรงจะพลาดบันทึกที่ถูกย้ายตำแหน่งจากการชนกันไปแล้วทั้งหมด
ตัวอย่างทำแบบฝึกหัด: ติดตามการชนกัน
- ตารางขนาด 10 ช่อง ใช้
key MOD 10พร้อม linear probing แทรก 23, 33, 43 ตามลำดับ แล้วค้นหา 43 - 23 hashes to 3; slot 3 is free, so it goes there. 33 hashes to 3; slot 3 is taken by 23, so linear probing puts it in slot 4. 43 hashes to 3; slots 3 and 4 are taken, so it goes in slot 5.
- ค้นหา 43: แฮชไปที่ 3 อ่านช่อง 3 คีย์คือ 23 ไม่ตรง จึงprobe ต่อ; ช่อง 4 มี 33 ไม่ตรง; ช่อง 5 มี 43 พบแล้ว หลังอ่านทั้งหมดสามครั้ง
- ช่วงยาวสามช่องนี้คือการรวมกลุ่มที่ linear probing ก่อให้เกิด
Hash each key straight to a bucket
A hash function turns a key into a bucket number, so you jump straight to the record instead of searching. When two keys land in the same bucket that is a collision — they chain together in that bucket.
When searching a hash table, the record is not in the file as soon as the first slot read holds a different key.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
The record may have been displaced by a collision. The search follows the same resolution strategy until a match is found or the strategy proves the key absent.
ปัจจัยโหลด (Load factor)
- ปัจจัยโหลด คือจำนวนบันทึกหารด้วยจำนวนช่อง เป็นตัวเลขเดียวที่ใช้ทำนายประสิทธิภาพของตาราง
- ต่ำกว่าประมาณ 70% การค้นหาค่าเฉลี่ยจะอยู่ใกล้การอ่านเพียงครั้งเดียว เหนือจากนี้ ลำดับการ probe จะยาวขึ้นอย่างมากและประสิทธิภาพจะลดลงสู่การค้นหาแบบเชิงเส้น
- ทางแก้คือทำให้ตารางใหญ่ขึ้นและ rehash บันทึกทุกอย่างเข้าไปใหม่ นี่คือเหตุผลที่ตารางแฮชถูกกำหนดขนาดตามข้อมูลที่มันจะรองรับในอนาคต ไม่ใช่ข้อมูลที่มีอยู่ในปัจจุบัน

A 10-slot table uses key MOD 10 with linear probing. After inserting 23, 33 and 43 in that order, which slot holds 43?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
All three hash to 3. 23 takes slot 3, 33 is probed to 4, and 43 to 5. Three keys in a row is exactly the clustering linear probing causes.
การ์ดนี้ได้รับการแก้ไขแล้ว จึงแสดงเป็นภาษาอังกฤษจนกว่าคำแปลจะได้รับการอัปเดต
Marks that slip away
- A collision is two keys, one address. It is not an error and not a lost record; it is the normal case a strategy handles.
- A search must follow the same resolution strategy as the insert, stopping on a match or when that strategy proves the key absent.
- Linear probing clusters 聚集; chaining costs memory. Give the trade-off, not just the mechanism.
- The load factor is records divided by slots, and the threshold is about 70%, not 100%.
To keep hash lookups fast, the load factor (records ÷ slots) should be kept:
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
A lower load factor means fewer collisions, so lookups stay close to O(1).
การ์ดนี้ได้รับการแก้ไขแล้ว จึงแสดงเป็นภาษาอังกฤษจนกว่าคำแปลจะได้รับการอัปเดต
You've got it
- a hash function turns a key into an address: fast, deterministic, evenly spread; modulo, folding and string hashes are the three to know
- a collision is two keys hashing to one address, resolved by linear probing 线性探测 (next free slot, clusters), chaining (a linked list per slot, more memory) or rehashing
- insert and search both follow the same strategy; a search ends on a match or when that strategy proves the key absent
- keep the load factor, records divided by slots, below about 70% for near one-read lookups
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้