ハッシング
導入済み
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. |
Finding one row in fifty million without looking
- A supermarket till scans a barcode and the price appears before your hand leaves the item. The product file holds fifty million lines.
- Nothing searched them. The barcode number went through a short calculation that produced a position, and the computer read that position: one read, no comparisons, the same time whether the file holds fifty rows or fifty million.
- この計算はハッシュ関数であり、全体のアプローチはこの関数に依存している:データを収まりそうな場所に格納せず、自身のキーが指す場所に格納する。
- この教訓はハッシュアルゴリズム、2つのキーが同じスロットを要求した際の挙動、そして検索と挿入の方法である。
ハッシュ関数
- ハッシュ関数、またはハッシュアルゴリズムは、レコードのキーを受け取り、レコードが格納されるアドレスを生成する。
- 良質なハッシュ関数は高速であり、決定論的(同じキーは必ず同じアドレスを返す)、かつキーを利用可能なスロット間に均等に分散させる。
- $N$スロットに対して、カリキュラムで期待される3つの手法:モジュロ,
address ← key MOD N; フォールディング, キーを分割して加算し、その後 $N$ でMOD; 文字列ハッシュ, 文字コードを加算し、その後 $N$ でMOD。
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.
worked example: 各アルゴリズムを適用する
- ファイルに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.
コリジョン
- コリジョンとは、異なる2つのキーが同じアドレスにハッシュされることです。あらゆるハッシュ関数と現実的なファイルにおいてコリジョンは避けられず、それに対する戦略は設計の一部であり、後付けのものではありません。
- 線形プロービングでは、レコードを次の空きスロットに配置し、テーブルの端に達したら先頭へラップします。単純ですが、レコードがクラスター化します。满した領域が大きくなり、そこに到達するキーごとに検索時間が長くなります。
- チェーンリングでは、各スロットがそこにハッシュされたすべてのレコードのリンクedListの先頭となります。クラスター化はありませんが、ポインタのための追加メモリと、リストをたどるための短時間が必要です。
- 再ハッシュでは、別のスロットを見つけるために第2のハッシュ関数を適用し、より良い分散を実現しますが、計算量が増える代償があります。

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).
検索と挿入
- 挿入する: キーをハッシュする。スロットが空いている場合はそこにレコードを書き込みます。そうでない場合は解決戦略に従います。線形プロービングなら次の空きスロット、チェーンリングならそのスロットのリストの先頭へ。
- 検索する: キーをハッシュしてそのスロットを読む。格納されているキーが一致すればレコードが見つかりました。一致しない場合は、上記と同じ戦略に従い、キーが一致するか、または空きスロット出现してレコードが存在しないことが証明されるまで続けます。
- 両操作は同一の戦略を使用します。最初の不一致で停止する検索では、過去にコリジョンによって追放されたレコードを見落とすことになります。
worked example: コリジョンのトレース
- 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があり、3回の読み取り後に発見。
- この3つの連続した連なりは、線形プロービングが引き起こすクラスター化です。
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%**未満であれば平均的な照合はほぼ1回の読み取りで済みます。それを超えるとプローブシーケンスが急激に長くなり、パフォーマンスは線形検索に近い方向に低下します。
- 対策は、テーブルを大きくしてすべてのレコードを再ハッシュすることです。そのため、ハッシュテーブルは今日保持しているデータではなく、将来保持する予定のデータに合わせてサイズが設定されます。

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
何が問題でしたか?この内容はサイトの運営チームだけが確認します。