OPEN ADRESSING IN DSA, Slides of Data Structures and Algorithms

The concept of open addressing in hashing, probing strategies, uniform hashing, analysis, and cryptographic hashing. It explains the difference between open addressing and chaining, and the desirable properties of cryptographic hash functions. The document also provides examples of applications of cryptographic hash functions. a lecture note from a course on algorithms and data structures.

Typology: Slides

2021/2022

Available from 08/18/2022

SamenKhan
SamenKhan 🇵🇰

231 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lecture 10 Hashing III: Open Addressing 6.006 Fall 2011
Lecture 10: Hashing III: Open Addressing
Lecture Overview
Open Addressing, Probing Strategies
Uniform Hashing, Analysis
Cryptographic Hashing
Readings
CLRS Chapter 11.4 (and 11.3.3 and 11.5 if interested)
Open Addressing
Another approach to collisions:
no chaining; instead all items stored in table (see Fig. 1)
item2
item1
item3
Figure 1: Open Addressing Table
one item per slot =mn
hash function specifies order of slots to probe (try) for a key (for insert/search/delete),
not just one slot; in math. notation:
We want to design a function h, with the property that for all k: U
h:U×{0,1,...,m1}{0,1,...,m1}
universe of keys trial count slot in table
hh(k, 0), h(k, 1), . . . , h(k , m 1)i
1
pf3
pf4
pf5
pf8

Partial preview of the text

Download OPEN ADRESSING IN DSA and more Slides Data Structures and Algorithms in PDF only on Docsity!

Lecture 10: Hashing III: Open Addressing

Lecture Overview

  • Open Addressing, Probing Strategies
  • Uniform Hashing, Analysis
  • Cryptographic Hashing

Readings

CLRS Chapter 11.4 (and 11.3.3 and 11.5 if interested)

Open Addressing

Another approach to collisions:

  • no chaining; instead all items stored in table (see Fig. 1)

item (^2)

item (^1) item (^3)

Figure 1: Open Addressing Table

  • one item per slot =⇒ m ≥ n
  • hash function specifies order of slots to probe (try) for a key (for insert/search/delete), not just one slot; in math. notation: We want to design a function h, with the property that for all k ∈ U:

h : U × { 0 , 1 ,... , m − 1 } → { 0 , 1 ,... , m − 1 }

universe of keys trial count slot in table

〈h(k, 0), h(k, 1),... , h(k, m − 1)〉

is a permutation of 0, 1 ,... , m − 1. i.e. if I keep trying h(k, i) for increasing i, I will eventually hit all slots of the table.

Ø 1

m -

Figure 2: Order of Probes

Insert(k,v) : Keep probing until an empty slot is found. Insert item into that slot.

for i in xrange(m): if T [h(k, i)] is None: ] empty slot T [h(k, i)] = (k, v) ] store item return raise ‘full’

Example: Insert k = 496 Search(k): As long as the slots you encounter by probing are occupied by keys = k, keep probing until you either encounter k or find an empty slot—return success or failure respectively.

for i in xrange(m): if T [h(k, i)] is None: ] empty slot? return None ] end of “chain” elif T [h(k, i)][∅] == k: ] matching key return T [h(k, i)] ] return item return None ˙ ] exhausted table

Ø 1

m -

cluster

if h ( k ,0) is any of these, the cluster will get bigger

Figure 4: Primary Clustering

  • actually hit all slots (permutation) if h 2 (k) is relatively prime to m for all k why?

h 1 (k) + i · h 2 (k) mod m = h 1 (k) + j · h 2 (k) mod m ⇒ m divides (i − j)

  • e.g. m = 2r, make h 2 (k) always odd

Uniform Hashing Assumption (cf. Simple Uniform Hashing

Assumption)

Each key is equally likely to have any one of the m! permutations as its probe sequence

  • not really true
  • but double hashing can come close

Analysis

Suppose we have used open addressing to insert n items into table of size m. Under 1 the uniform hashing assumption the next operation has expected cost of ≤ , 1 = n/m(< 1).

− α where α Example: α = 90% =⇒ 10 expected probes

Proof:

Suppose we want to insert an item with key k. Suppose that the item is not in the table.

  • probability first probe successful: mm−n^ =: p (n bad slots, m total slots, and first probe is uniformly random)
  • if first probe fails, probability second probe successful: mm−−n 1 ≥ mm−n^ = p (one bad slot already found, m − n good slots remain and the second probe is uniformly random over the m − 1 total slots left)
  • if 1st & 2nd probe fail, probability 3rd probe successful: mm−−n 2 ≥ mm−n^ = p (since two bad slots already found, m−n good slots remain and the third probe is uniformly random over the m − 2 total slots left)
  • ...

⇒ Every trial, success with probability at least p. Expected Number of trials for success?

1 1 =. p 1 − α

With a little thought it follows that search, delete take time O(1/(1 − α)). Ditto if we attempt to insert an item that is already there.

Open Addressing vs. Chaining

Open Addressing: better cache performance (better memory usage, no pointers needed)

Chaining: less sensitive to hash functions (OA requires extra care to avoid clustering) and the load factor α (OA degrades past 70% or so and in any event cannot support values larger than 1)

Cryptographic Hashing

A cryptographic hash function is a deterministic procedure that takes an arbitrary block of data and returns a fixed-size bit string, the (cryptographic) hash value, such that an accidental or intentional change to the data will change the hash value. The data to be encoded is often called the message, and the hash value is sometimes called the message digest or simply digest.

require is CR. We don’t want an adversary to ask Alice to sign x and then claim that she signed x′, where h(x) = h(x′).

Implementations

There have been many proposals for hash functions which are OW, CR and TCR. Some of these have been broken. MD-5, for example, has been shown to not be CR. There is a competition underway to determine SHA-3, which would be a Secure Hash Algorithm certified by NIST. Cryptographic hash functions are significantly more complex than those used in hash tables. You can think of a cryptographic hash as running a regular hash function many, many times with pseudo-random permutations interspersed.

MIT OpenCourseWare http://ocw.mit.edu

6.00 6 Introduction to Algorithms Fall 2011

For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.