markdown
ハッシュテーブルの基本md 5d32ade
lecture/information/algorithm/data-structures/hash-table-basics.lecture.n.md
Download PDF

ハッシュテーブルの基本きほん

date2026-07-14document_iddoc_6e5b2ec4e3a69f6ebc84b37f111e6b97descriptionハッシュを、値そのものを探すのでなく置き場所を素早く決める仕組みとして整理し、平均的に高速な探索がなぜ可能かを説明します。prerequisites計算量の基本 / データ構造ポータル / 配列の基本操作type講義statusactiverelateddata/lecture/information/algorithm/data-structures/data-structures-portal.lecture.n.md / data/lecture/information/algorithm/search/linear-search.lecture.n.md
algorithmdata-structuresundergraduatelecture
data/lecture/information/algorithm/data-structures/data-structures-portal.lecture.n.md

1導入どうにゅう

この講義こうぎでは、辞書じしょ ADT をハッシュテーブルで実装じっそうする方法ほうほう説明せつめいする。衝突しょうとつ連鎖法れんさほう処理しょりする場合ばあいについて、負荷率ふかりつ計算量けいさんりょう関係かんけい整理せいりする。

2辞書じしょ ADT

辞書じしょ ADTDictionary ADT は、キーの集合しゅうごう管理かんりし、キーに対応たいおうするあたい記録きろくする抽象ちゅうしょうデータがたである。検索けんさくは、キーが存在そんざいすれば対応たいおうするあたいかえし、存在そんざいしなければ「未登録みとうろく」をかえす。挿入そうにゅうは、未登録みとうろくのキーとあたい追加ついかし、登録済とうろくずみのキーには対応値たいおうち置換ちかんする。削除さくじょは、登録済とうろくずみのキーと対応値たいおうち除去じょきょし、未登録みとうろくのキーには状態じょうたい変更へんこうしない。キーの順序じゅんじょ仕様しようふくまれない。

3ハッシュ関数かんすうとバケット

キーの全体ぜんたいU、バケットすうm とする。ハッシュ関数かんすうHash function

h:U{0,1,,m-1}

によってキーを配列はいれつ添字そえじ対応たいおうさせる関数かんすうである。バケットBucket は、同一どういつ添字そえじ対応たいおうしたキーを格納かくのうする領域りょういきである。キー k操作そうさでは、まず h(k)対応たいおうするバケットを選択せんたくする。

衝突しょうとつCollision は、相異あいことなるキー k1,k2たいして h(k1)=h(k2)成立せいりつすることである。U要素数ようそすうm よりおおきい場合ばあいはと巣原理すげんりにより衝突しょうとつ不可避ふかひである。

4連鎖法れんさほう

連鎖法れんさほうSeparate chaining は、かくバケットにキーとあたいれつ保持ほじし、衝突しょうとつした要素ようそ同一どういつれつ格納かくのうする方式ほうしきである。検索けんさくでは h(k) のバケットだけを走査そうさする。挿入そうにゅう削除さくじょでも、おなじバケットないでキーを照合しょうごうする。連鎖れんさ連結れんけつリストで実装じっそうする場合ばあい操作時間そうさじかんはバケットない要素数ようそすう依存いぞんする。

5負荷率ふかりつ期待計算量きたいけいさんりょう

格納かくのうされているキーの個数こすうn とする。負荷率ふかりつLoad factor

α=nm

である。入力にゅうりょくとなるキーれつ固定こていし、ハッシュ関数族かんすうぞくから関数かんすう無作為むさくい選択せんたくしたとき、かくキーが m のバケットへ一様いちようかつ独立どくりつ対応たいおうするという単純一様たんじゅんいちようハッシュ仮定かていSimple uniform hashing assumptionく。期待値きたいちはこの無作為選択むさくいせんたくについてる。この仮定かていもとでは、バケットの期待要素数きたいようそすうα である。したがって、連鎖法れんさほうによる検索けんさく挿入そうにゅう削除さくじょ期待時間きたいじかんO(1+α) となる。α定数ていすう以下いか維持いじすれば、各操作かくそうさ期待時間きたいじかんO(1) である。

これは任意にんいのハッシュ関数かんすうたいする決定的けっていてき保証ほしょうではない。すべてのキーが単一たんいつのバケットへ集中しゅうちゅうする場合ばあい検索けんさく挿入そうにゅう削除さくじょ最悪時間さいあくじかんO(n) となる。

6リサイズ

リサイズResizing は、負荷率ふかりつ所定しょてい閾値しきいち超過ちょうかしたときに、よりおおきいバケット配列はいれつ確保かくほし、すべてのキーを再配置さいはいちする操作そうさである。バケットすう変化へんかするとハッシュ範囲はんい変化へんかするため、既存きそん要素ようそをそのまま複製ふくせいするだけでは不十分ふじゅうぶんであり、かくキーのバケットを再計算さいけいさんする必要ひつようがある。

1 かいのリサイズには O(n) 時間じかんようする。ただし、バケットすう定数倍ていすうばい拡大かくだいする方式ほうしきでは、多数たすう挿入そうにゅうにリサイズ費用ひよう配分はいぶんできる。リサイズにも単純一様たんじゅんいちようハッシュ仮定かていたす関数かんすう選択せんたくし、α定数ていすう以下いか維持いじし、1 かいのハッシュ計算けいさんO(1) とする。このとき、挿入そうにゅう償却期待時間しょうきゃくきたいじかんO(1) となる。

7適用範囲てきようはんい

  • キーの完全一致かんぜんいっちによる検索けんさくにはハッシュテーブルがてきする。
  • キーの大小順だいしょうじゅん範囲検索はんいけんさく最小値さいしょうち直接ちょくせつあつか場合ばあいには、平衡探索木へいこうたんさくぎなどの順序じゅんじょ保持ほじするデータ構造こうぞうてきする。

8まとめ

ハッシュテーブルは、ハッシュ関数かんすうでバケットを選択せんたくし、連鎖法れんさほう衝突しょうとつ処理しょりする。単純一様たんじゅんいちようハッシュ仮定かてい有界ゆうかい負荷率ふかりつもとでは辞書操作じしょそうさ期待きたい O(1) 時間じかん実行じっこうできるが、最悪時間さいあくじかんO(n) である。

Hash Table Basics

data/lecture/information/algorithm/data-structures/data-structures-portal.lecture.n.md

1Introduction

This lecture explains how a hash table implements the dictionary ADT. For separate chaining, it relates the load factor to operation complexity.

2Dictionary ADT

The dictionary ADTDictionary ADT is an abstract data type that maintains a set of keys and records a value associated with each key. Lookup returns the associated value for a present key and an “absent” result otherwise. Insertion adds an absent key-value pair and replaces the associated value of a present key. Deletion removes a present key and its value and leaves the state unchanged for an absent key. The specification does not require an ordering of the keys.

3Hash Functions and Buckets

Let U be the universe of keys and let m be the number of buckets. A hash functionHash function is a function

h:U{0,1,,m-1}

that maps a key to an array index. A bucketBucket is the storage region for keys mapped to the same index. An operation on key k first selects the bucket indexed by h(k).

A collisionCollision occurs when h(k1)=h(k2) for two distinct keys k1 and k2. If the cardinality of U exceeds m, the pigeonhole principle makes collisions unavoidable.

4Separate Chaining

Separate chainingSeparate chaining stores a sequence of key-value pairs in every bucket and places colliding entries in the same sequence. Lookup scans only the bucket selected by h(k). Insertion and deletion likewise compare keys within that bucket. When each chain is implemented as a linked list, an operation's running time depends on the number of entries in its bucket.

5Load Factor and Expected Complexity

Let n be the number of stored keys. The load factorLoad factor is

α=nm.

Fix the input sequence of keys and choose a function randomly from a hash-function family. Assume simple uniform hashingSimple uniform hashing assumption: each key maps independently and uniformly to one of the m buckets. The expectation is over this random choice. Under the assumption, the expected number of entries in a bucket is α. Lookup, insertion, and deletion with separate chaining therefore take expected time O(1+α). If α is kept below a constant, each operation takes expected time O(1).

This conclusion is not a deterministic guarantee for every hash function. If all keys map to one bucket, lookup, insertion, and deletion take O(n) time in the worst case.

6Resizing

ResizingResizing allocates a larger bucket array and redistributes all keys when the load factor exceeds a prescribed threshold. Because changing the bucket count changes the range of hash values, merely copying the existing entries is insufficient: the bucket of every key must be recomputed.

A single resize takes O(n) time. If the bucket count grows by a constant factor, however, its cost can be distributed over many insertions. Assume that after resizing a function satisfying simple uniform hashing is selected, that α remains bounded by a constant, and that one hash computation takes O(1) time. Insertion then takes O(1) amortized expected time.

7Scope of Application

  • Hash tables are appropriate for exact-key lookup.
  • When an application directly requires sorted keys, range queries, or a minimum key, an order-preserving data structure such as a balanced search tree is more appropriate.

8Summary

A hash table selects a bucket with a hash function and resolves collisions by separate chaining. Under simple uniform hashing and a bounded load factor, dictionary operations take expected O(1) time, whereas their worst-case time is O(n).

raw .n.md をコピー
loc をコピー (filepath:line ~ line)
copy share link
copy encoded share link
path をコピー
copy share link
copy encoded share link
copy share link
copy encoded share link
タブを全て閉じる