Hash Table

Context: FIT1008_MOC · the Array (Data Structure)-backed implementation of a Dictionary (ADT) · backbone clustering the whole hashing pipeline (hash function → collision resolution → probing → load factor)

Quick Revision

  • 🎯 Objective: map keys directly to array indices ➔ expected add/search/delete, beating sorted-list , but unordered.
  • 📦 Core Components: Hash Function ➔ uniform spread | Collision Resolution ➔ chaining vs open addressing | Load Factor ➔ cost dial + rehash trigger.
  • ⚡ Key Constraint: expected , worst-case on collisions ➔ hinges on a uniform hash + bounded .

📝 Core

1. The Hash Table (Expected )

  • Core mechanism ➔ keys → index via hash into a large array access ⟹ expected ops.
  • SUHA analysis; uniform hashing ⟹ expected chain length while .
  • Birthday paradox ➔ collisions appear by ⟹ resolution mandatory even at low load.

2. Hash Function (“what” → “where”)

  • Propertiesdeterministic + fast () + uniform (short chains/clusters).
  • Design rules ➔ use all the key, weight by position (else anagrams collide), Horner’s method (mod each step).
  • Prime discipline ➔ multiplier coprime to (prime base + prime ); avoid zero divisors of .

3. Collision Resolution

  • Collision ➔ two keys → one slot ➔ inevitable (finite table, huge key space).
  • Two familiesseparate chaining (a LinkList per cell) | open addressing (probe in-array).
  • Cost ➔ chained ops are (duplicate check); a bad hash collapses to one chain.

4. Open Addressing — Probe Sequences

  • Mechanism ➔ store every key in-array; probe a deterministic sequence to empty (add) or match (search); required.
  • Schemeslinear → primary clustering | quadratic → secondary + may miss slots | double → cures both.
  • hash2 rulenever return 0 and coprime to (else can’t visit every slot).

5. Linear Probing

  • Mechanism ➔ home taken ⟹ walk +1 (wrapping); each cell holds (key, data).
  • Invariant ➔ key with hash=N lies between slot N and the first empty (Invariant) ⟹ search stops at first empty.
  • Delete ➔ naïve blank severs the run ⟹ blank + reinsert trailing cluster or use tombstones; keep .

6. Load Factor — Cost Dial

  • Definition predicts cost: low ⟹ ; high ⟹ toward .
  • Caps ➔ chaining tolerates (linear ); open addressing (probes climb steeply as ).
  • Rehash ➔ cross threshold ⟹ double , reinsert all (, but amortised).

⚙️ Core Implementation

🔹 Hash Function — polynomial / Horner

🔹 Linear Probing — +1 probe, rehash-on-full, correct delete

🔹 Quadratic & Double Hashing — breaking clusters

⚖️ Core Decision Matrix

Variant / StrategyTrigger ConditionAdvantage (Pro)Disadvantage (Con) / Complexity BoundCache / Memory Impact
Separate chaining may exceed 1trivial delete (unlink); if one chain pointer/node, poor
Linear probingLow , cache-criticalexcellent localityprimary clustering; worstnone, in-array
Quadratic probingAvoid primary clusteringcures primarysecondary remains; may miss slotsnone
Double hashingNear-uniform probingcures both clusteringsawkward delete; cache-missing jumpsnone

When It Flips: dictionary backings — unsorted List (ADT) , Sorted List (ADT) search/ add, balanced Binary Tree + ordered, Hash Table but unordered. Use a hash table for big + no ordering; a BST for range/successor/worst-case guarantees.

📊 Exam Execution Trace

Manual Execution Trace

Linear probing, , hash(k)=k % 7, insert 10, 17, 24, 3:

Step / StateTrigger OpHome k%7Probe PathFinal Slot
0 (Init)init[_,_,_,_,_,_,_]
1insert 103slot 3 empty3
2insert 1733 taken → 44
3insert 2433,4 taken → 55
4insert 333,4,5 taken → 66

A primary cluster now spans slots 3–6; any new key hashing to 3–6 must probe to slot 0.

Applied Exercise

Problem: Show rehash amortises to and derive the chained SUHA search cost. Derivation Proof / Hand-Calculation Walkthrough:

Final Extracted Output: per-insert amortised (geometric doubling); keyed ops at bounded load.

🧠 Active Recall