Hash Tables and Maps, Lecture notes of Data Structures and Algorithms

The concept of hash tables and maps, which are tables of (key,value) pairs indexed by unique keys. It covers the advantages and disadvantages of using TreeSet and TreeMap, which are classes that implement a sorted Set and Map respectively. The document also explains the use of a balanced binary search tree called a red-black tree, and the concept of hash functions and collisions. It provides examples of hash functions for integers and strings. from a Fall 2020 course (15121) taught by Margaret Reid-Miller.

Typology: Lecture notes

2019/2020

Uploaded on 05/11/2023

tanvir
tanvir 🇺🇸

5

(4)

224 documents

1 / 30

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Hash Tables
15-121 Fall 2020
Margaret Reid-Miller
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e

Partial preview of the text

Download Hash Tables and Maps and more Lecture notes Data Structures and Algorithms in PDF only on Docsity!

Hash Tables

15 - 121 Fall 2020

Margaret Reid-Miller

Today

• Sets and Maps review

• Hash Tables

• Next time

• hashCodes

• Priority Queues

Set

(sometimes called a bag) A set is “bag” of objects

  • No duplicates with respect to .equals()
  • Membership Operations I want to be fast:
    • Does the set contain this element?
    • Add this element to the set
  • Remove an element from the set Tom Sue Sally Fred Dave

Map

(also called dictionary or associative array)

A map is a table of (key,value) pairs.

• Indexed by key (must be unique).

• Many keys can “map” to the same value.

Operations I want to be fast:

• Get Tom’s section

• Set Dave’s section to B

• Remove Fred from the class

Name Section Tom A Fred B Dave A Sally C

Both TreeSet & TreeMap use a balanced binary search tree called a “red-black tree". Red-Black balanced binary search trees:

  • The height of a red-black tree is guaranteed to be 2 log n.
  • Every time you add or remove an element, the tree may be restructured to maintain balance.
  • The runtime to rebalance the tree is worst case O(log n).

Thus

  • Operations contains/add/remove for Sets and get/set/remove for map have worst-case O(log n) runtime.

Worst case O(log n) time! Great! We’re done! The course is over! Yay! Unless…

Really Big Array (?)

Use the student id as the index into a really big array: contains: O(1), add: O(1), remove: O(1). Yay!!! Problem: 0 1 … 151212018 à id: 151- 21 - 2018 name: Dave … 999999999 Memory hog: The range of student id values is independent on the number of students (size of the set).

Moderate Size Array (better)

Key Idea: Use the key to compute an index into a moderate size array.

  • Want: contains, add, remove: O(1), memory: O(n) Example : Use last two digits of the student id
  • Problem: 0 1 18 à (^) id: 151 - 21 - 2020 name: Dave 99 Two or more students might have the same last two digits.

Collisions

  • Can we prevent collisions when we don’t know in advance which keys will be used in the set? - No. Since the number of possible keys is much greater than the size of the hash table, there must be two keys that map to the same index. - Any set that contains those two keys will have a collision.
  • Pigeonhole Principle : If you put more than n items into n bins, then at least one bin contains more than one item. all possible keys S S

The Birthday Paradox

  • How likely are two keys going to hash to the same index?

• Probability that none of n people have the same

birthday:

p’ = 1(364/365)(363/365)((365- n +1)/365)

  • Probability at least two people have the same birthday is p = 1 – p’ When n = 23, p = 0.5. When n = 30, p = 0. When n = 50, p = 0.97 !! Surprisingly likely!

Handling Collisions

1. Open Addressing (topic for 15 - 451) 2. Separate chaining – Each index of array contains all the elements that hash to that index (called a bucket ) What data structure should we use to maintain a bucket?

  • Often a linked list because:
    • Buckets are small (few collisions)
    • Linked lists easy to implement
    • Many buckets can be empty and empty linked lists take no storage
    • No additional constraints such as Comparable

Separate Chaining using a linked list 10 … Dave … Tom … Sue 65 null null null null null

Runtime

What is the worst-case runtime for contains, add, remove? O(n) – all the keys hash to the same index What is the best-case runtime? O(1) – only a few keys map to any one index What is the expected runtime? O(1) – assuming the hash function is good, and the hash table is not too full

Load Factor

Load Factor: (number of elements) / (length of array) What is the expected size of a bucket? The load factor What is a good load factor? A small constant so that the linked list stay short, even the longest ones. Java uses a default value of 0. Can the load factor be larger than 1? Yes