From b3ce28dceb4bd0b2451d59e9d391030c623bec82 Mon Sep 17 00:00:00 2001 From: Claudio Maggioni Date: Thu, 4 Apr 2019 13:38:48 +0200 Subject: [PATCH] 2019-04-04 in class --- notes.md | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/notes.md b/notes.md index 294312b..cb61c61 100644 --- a/notes.md +++ b/notes.md @@ -173,7 +173,58 @@ Queue has always 1 cell free to avoid confusion with full/empty Data structure for fast search +A way to implement a dictionary is a *Direct-access table* + +## API of dictionary + +- `Insert(D, k)` insert a ket `k` to dictionary `D` +- `Delete(D, k)` removes key `k` +- `Search(D, k)` tells whether `D` contains a key `k` + +Many different implementations + ## Direct-access table -Implementation of dictionary +- universe of keys = {1,2,...,M} +- array `T` of size M +- each key has its own position in T +```python + +def Insert(D, x): + D[x] == True + +def Delete(D, x): + D[x] == False + +def Search(D, k): + return D[x] + +``` + +Everything is O(1), but memory complexity is awful. + +Solve this by mapping keys to smaller keys using an hash function. + +Table T is a table many times smaller than the universe and different by a small +constant factor from the set of keys stored S. + +Instead of using keys directly, the index returned by the hash function is used. + +**Pigeon hole problem:** More keys (pigeons) in the universe than holes (indexes +for table t) + +### Solution + +For every cell in table T don't store True/False but a linked list of keys for +each index in the table. This is called *Chained hash table*. + +```python + +def Chained_hash_insert(T, k): + return List_insert(T[hash(k)], k) + +def Chained_hash_search(T, k): + return List_search(T[hash(k)], k) + +```