גישור (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/ | פונקציית גישור (Hash) |
| 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. |
מציאת שורה אחת מתוך חמישים מיליון ללא בדיקה
- קופה במרכול זורק ברקוד ברקוד והמחיר מופיע לפני ידכם עוזבת את המוצר. תיק המוצרים מכיל חמישים מיליון שורות.
- אף אחד לא בדק אותם. מספר הרקוד עבר דרך חישוב קצר שייצר מיקום, והמחשב קרא את המיקום הזה: קריאה אחת, ללא השוואות, אותו זמן בין אם הקובץ מכיל חמישים שורות או חמישים מיליון.
- החישוב הוא פונקציית גיבוי, והרעיון כולו נשען עליו: אל תאגרו נתונים במקום שישתלבו, אלא במקום שמפתח עצמו אומר שהוא שייך לו.
- שיעור זה עוסק באלגוריתמי גיבוי, מה קורה כשני מפתחות רוצים את אותו מקום, וכיצד לבצע חיפוש והוספה.
פונקציית הגיבוי
- פונקציית גיבוי, או אלגוריתם גיבוי, לוקחת את המפתח של שטח ומייצרת את הכתובת בה השטח מאוחסן.
- פונקציה טובה היא מהירה, דיטרמיניסטית (אותו מפתח תמיד נותן אותו כתובת), ומפיצה מפתחות באופן שווה על פני המקומות הזמינים.
- עבור $N$ מקומות, שלושת האלו שתיבת הסילבוס מצפה להם: מודולו,
address ← key MOD N; קיפול, לחלק את המפתח לחלקים, לחבר אותם, ואז MOD $N$; גיבוי מחרוזת, לחבר קודי האותיות, ואז 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" באמצעות hash מחרוזת? $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.
התנגשויות
- התנגשות מתרחשת כאשר שני מפתחות שונים מתמזגים לאותו כתובת. עם כל פונקציית hash וכדי קובץ ריאליסטי, התנגשויות בלתי נמנעות, ולכן אסטרטגיה להתמודדות איתן הן חלק מהעיצוב, ולא תוספת מאוחרת.
- חיפוש ליניארי מכניס את הרקורד לתא הפנוי הבא, ומתחבר לחלק העליון של הטבלה בסוף הטבלה. פשוט, אך רקורדים צברים: מקטע מלא גדל וכל מפתח שנופל בו לוקח יותר זמן.
- שילוב (Chaining) הופך כל תא לראש של רשימה מקושרת של כל הרקורדים שהתמזגו שם. אין צבירה, אך יש צורך במרחב נוסף לשערי רמייה והליכה קצרה לאורך הרשימה.
- Rehashing מיישם פונקציית hash שנייה למציאת תא אחר, ומפיץ מפתחות טוב יותר על חשבון חישוב נוסף.

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).
חיפוש והכנסה
- להכניס: המזג את המפתח. אם התא פנוי, כתוב את הרקורד שם. אם לא, עקוב אחר אסטרטגיה לפתרון ההתנגשות, התא הפנוי הבא עבור חיפוש ליניארי, או ראש רשימת התא עבור שילוב.
- לחפש: המזג את המפתח וקרא את התא. אם המפתח השמור תואם, הרקורד נמצא. אם לא, עקוב אחר אותה אסטרטגיה, עד שמצאים התאמה או שתא ריק מוכיח שהרקורד אינו בקובץ.
- שתי הפעולות משתמשות באותה אסטרטגיה. חיפוש שיעצור בהתנגדות הראשונה יפספס כל רקורד שהוזז לעולם על ידי התנגשות.
דוגמה פתורה: מעקב אחרי התנגשות
- טבלה בעלת 10 תאים משתמשת ב-
key MOD 10עם חיפוש ליניארי. הכנס 23, 33, 43 בסדר זה, ואז חפש את 43. - 23 מתמזג ל-3; תא 3 פנוי, ולכן הוא נכנס שם. 33 מתמזג ל-3; תא 3 תפוס על ידי 23, ולכן חיפוש ליניארי מכניס אותו לתא 4. 43 מתמזג ל-3; תאים 3 ו-4 תפוסים, ולכן הוא נכנס לתא 5.
- חיפוש אחר 43: המזג ל-3, קרא תא 3, המפתח הוא 23, אין התאמה, ולכן המשך בחיפוש; תא 4 מכיל 33, אין התאמה; תא 5 מכיל 43, נמצא, לאחר שלושה קריאות.
- רצף הגדל הזה של שלושה הוא הצבירה שחיפוש ליניארי גורם לה.
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.
מקדם העומס
- מקדם העומס הוא מספר הרשומות חלקי מספר התאים. זהו המספר היחיד שחוזה את ביצועי הטבלה.
- מתחת ל-70% בערך, חיפוש ממוצע דורש קריאה אחת. מעל סף זה, אורך רצפי החיפוק מתאר בחוד וביצועים יורדים לעבר חיפוש ליניארי.
- הפתרון הוא להגדיל את הטבלה ולעשות TODO rehash לכל רשומה לתוכה; לכן טבלת TODO hash נבנית בהתאם למידע שתכיל, ולא למידע הקיים היום.

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
מה השתבש? רק צוות האתר רואה זאת.