Lecture 6/1: Hashing


June 1, 2020

đź“‚Associated files

Hashing and Hash Tables

CS 106B: Programming Abstractions

Spring 2020, Stanford University Computer Science Department

Lecturers: Chris Gregg and Julie Zelenski

A picture of delicious hashbrowns


Slide 2

Announcements


Slide 3

Today's Topics


Slide 4

Motivation


Slide 5

Hasham! (courtesy of Chris Piech)

A stylized version of the Shazam! application logo


Slide 6

Hashing


Slide 7

Hashing – an introduction


Slide 8

Our first hash function

values (ints): a b c d e f g h i j k l m n o p q r s t u v w x y z
index:  0   1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25 

Slide 9

Definition of a hash function


Slide 10

Hashing – all words?

Word Letters Characteristics
Methionylthreonylthreonylglutaminylarginyl...isoleucine 189,819 Chemical name, disputed...
Methionylglutaminylarginyltyrosylglutamyl...serine 1,909 Longest published word
Lopadotemachoselachogaleokranioleipsano...pterygon 183 Longest word coined by a major author
Pneumonoultramicroscopicsilicovolcanoconiosis 45 Longest word in a major dictionary
Supercalifragilisticexpialidocious 34 Famous for being created for Mary Poppins
Pseudopseudohypoparathyroidism 30 Longest non-coined word in a major dictionary
Floccinaucinihilipilification 29 Longest unchallenged nontechnical word
Antidisestablishmentarianism 28 Longest non-coined and nontechnical word
Honorificabilitudinitatibus 27 Longest word in Shakespeare's works; longest word in the English language featuring alternating consonants and vowels.

Slide 11

What if we wanted to store all words?

An image of a place in New Zealand with the name Taumatawhakatangihangakoauauotamateaturipukakapikimaungahoronukupokaiwhenuakitanatahu


Slide 12

Hashing long words


Slide 13

How to hash to a fixed number of buckets


Slide 14

Hashing You!


Slide 15

Birthday hashing


Slide 16

Collisions


Slide 17

Hash table chaining

A set of links from a metal chain


Slide 18

Limiting the size of the linked list


Slide 19

Hash Codes and Compression Functions



Slide 20

What makes a good or bad compression function?


Slide 21

A good hash function for strings


Slide 22

Back to Hasham

A stylized version of the Shazam! application logo