Lecture notes · Algorithms · Lecture 4

Hashing

MIT 6.006 Introduction to Algorithms, lecture 4, explained in plain English

Want this lecture to stick? In LessonBound you get checked practice questions for every part, can ask about anything you don't get, and can listen with your screen off. Free, no ads.

Learn this lecture free

Watch or read the original lecture: MIT OpenCourseWare.

Part 1 · 0:00–10:00

This part sets up a proof that finding an item by its key cannot beat log n time when an algorithm can only compare keys.

Key ideas

Words to know

key
The part of an item that you search by. Two items are the same for searching if their keys match.
comparison model
A limited way of working where the only thing you can do with items is compare their keys two at a time. You cannot look inside the keys.
decision tree
A diagram that shows every possible path a program can take, where each step is a yes or no question that splits the path into two branches.
leaf
The end point of a branch in a decision tree, where the program stops and gives its output.

Example

Imagine you have n sealed boxes, and one of them holds the prize. You are only allowed to ask questions like 'is this box heavier than that one?' Each answer can only send you down one of two paths, so the questions form a branching tree, and the tree must have room for every box to be the final answer.

3 checked practice questions for this part are in the app.

Part 2 · 10:00–20:00

This part explains why comparison-based searching cannot beat logarithmic time, and how a direct access array can search in constant time but wastes space when keys are large.

Key ideas

Words to know

direct access array
A list where each item's key is used as its exact position, so you can jump straight to it without searching.
height
How many steps it takes to get from the top of a tree down to the bottom in the longest case.
branching factor
How many different paths a single step in a tree can lead to.
universe
The full range of possible keys you could ever store, whether or not you use them all.

Example

Imagine a hotel with a locker for each guest, and the locker number is the guest's room number. If you know the room number, you walk straight to the right locker without checking any others. But if room numbers go up to ten million and only 300 guests are staying, most of the lockers would sit empty and waste space.

3 checked practice questions for this part are in the app.

Part 3 · 20:00–30:00

This part explains why a direct access array wastes space when keys come from a huge range, and how a hash function can shrink the array, which creates collisions that must then be handled.

Key ideas

Words to know

hash function
A rule that takes a key and turns it into a smaller number, which is used as the spot where the key is stored.
collision
When two different keys get the same spot from the hash function, so there is no room for both in that one spot.
open addressing
A way to handle collisions by searching the array for another empty spot and storing the item there.
pigeonhole principle
If you have more items than boxes, at least one box must hold more than one item.

Example

Imagine a hotel with 10 rooms and 8 guests, where each guest gets a room based on the first letter of their name. Two guests with names starting with the same letter may be sent to the same room, so the staff have to find another empty room for the second guest. That is like a collision, and finding a nearby empty room is like open addressing.

3 checked practice questions for this part are in the app.

Part 4 · 30:00–40:00

This part explains how hash tables handle two keys landing in the same spot, and why picking a hash function at random helps keep lookups fast.

Key ideas

Words to know

chaining
A way to handle collisions where each slot in the table holds a small list of all the items that landed there.
collision
When two different keys get sent to the same slot in a hash table.
deterministic
Always producing the same result for the same input. A deterministic hash function gives the same answer every time you run it.
universal hash function
A hash function picked at random from a special group of functions, built so that the chance of two keys colliding stays low.

Example

Imagine a coat check with numbered hooks, where each coat goes on the hook matching the first letter of its owner's name. Sometimes two coats land on the same hook, so you hang the extra coats on a small rack attached to that hook. When you look for a coat, you only need to check that small rack, not the whole room.

3 checked practice questions for this part are in the app.

Part 5 · 40:00–52:48

This part explains how a random choice of hash function, called a universal hash family, keeps the lists in a hash table short on average.

Key ideas

Words to know

hash family
A group of many possible hash functions. The table picks one at random from the group when it starts.
collision
When two different keys get sent to the same spot in the table.
indicator random variable
A simple counter that is 1 when something happens (like a collision) and 0 when it does not.
expected value
The average result you would get if you repeated a random process many times.

Example

Think of a coat check where each coat is put on a hook chosen by a secret random rule picked when the room opens. Because nobody knows the rule, nobody can plan to pile all the coats on one hook. With enough hooks, the piles stay small on average.

3 checked practice questions for this part are in the app.

← Lecture 3: Sets and Sorting · Lecture 6: Binary Trees, Part 1 →

Notes written by AI (Claude) from the lecture's official transcript, so check anything important against the lecture. MIT OpenCourseWare · CC BY-NC-SA 4.0 · not affiliated with or endorsed by MIT. These notes are shared under the same license.

LessonBound · Free lecture notes · Privacy · Terms · New York