Hashing Chapter 5 1 Objectives Understand the idea of hashing - - PowerPoint PPT Presentation

hashing
SMART_READER_LITE
LIVE PREVIEW

Hashing Chapter 5 1 Objectives Understand the idea of hashing - - PowerPoint PPT Presentation

Hashing Chapter 5 1 Objectives Understand the idea of hashing Compare hashing to sorting Design a hashtable Identify the applications that require the hashtable data structure Understand the terminology of hashtables Distinguish between


slide-1
SLIDE 1

Hashing

Chapter 5

1

slide-2
SLIDE 2

Objectives

Understand the idea of hashing Compare hashing to sorting Design a hashtable Identify the applications that require the hashtable data structure Understand the terminology of hashtables Distinguish between the different implementations of hash tables

2

slide-3
SLIDE 3

Definition

hash (verb | \’hæʃ\)

In Merriam-Webster

to chop (food, such as meat and potatoes) into small pieces confuse, muddle

3

slide-4
SLIDE 4

Why Hashing?

Do we keep everything in an ascending order? How do you compare a pair of glasses to a book?

4

slide-5
SLIDE 5

Hashing

You store something in a place When you want it back, you go and look for it where it is supposed to be A simple design: Keep your data elements in a big array of a fixed size so that each element has one fixed position What is good/bad about hashing?

5

slide-6
SLIDE 6

Hashtable ADT

Initialize(n): Initializes an empty hashtable initially with n (empty) slots Insert(k, v): Stores the value v with the key k Contains?(k): Returns true if there is some value with the key k in the hashtable Retrieve(k): Retrieves the value with the key k Erase(k): Deletes the value with the key k Clear(): Removes all key-value pairs Size(): Returns number of elements Empty?(): Returns true if the hashtable is empty

6

slide-7
SLIDE 7

Elements of a Hashtable

7

K V Key Value

Hashtable Key-value pair Hash function Hash bucket

slide-8
SLIDE 8

Design Issues

What is a good size for a hashtable? What are the good and bad properties of a hash function?

Fast computation Dispersal (Scatters things around) Memoryless (A must)

Examples of (bad) hash functions

The initial of the last name The student ID modulo number of buckets

8

slide-9
SLIDE 9

A Simple Hashtable

Key: State names Value: Population Capacity: 6 Hash function: Initial letter modulo capacity Insert(‘CA’, 40)

9

Key Value

slide-10
SLIDE 10

A Simple Hashtable

Key: State names Value: Population Capacity: 6 Hash function: Initial letter modulo capacity Insert(‘CA’, 40) Insert(‘MN’, 5)

10

Key Value CA 40

slide-11
SLIDE 11

A Simple Hashtable

Key: State names Value: Population Capacity: 6 Hash function: Initial letter modulo capacity Insert(‘CA’, 40) Insert(‘MN’, 5) Insert(‘NY’, 8)

11

Key Value MN 5 CA 40

slide-12
SLIDE 12

A Simple Hashtable

Key: State names Value: Population Capacity: 6 Hash function: Initial letter modulo capacity Insert(‘CA’, 40) Insert(‘MN’, 5) Insert(‘NY’, 8) Insert(‘OK’, 4)

12

Key Value MN 5 NY 8 CA 40

slide-13
SLIDE 13

Collision

The biggest problem with hashtables is the collision problem Pigeonhole principle Birthday paradox Hashtables differ mainly on how collisions are handled

13

slide-14
SLIDE 14

Separate Chaining

14

Hash