Hash tables and graphs
Finding a value by its key in constant time with a hash table, and modelling connections with graphs that are searched breadth-first or depth-first.
About 8 hours of study
Learning objectives
- Handle collisions with chaining and with linear probing
- Store a graph as a matrix or as adjacency lists
- Find the fewest edges with breadth-first search
- Explore a graph and count its components with depth-first search
Lessons
- 1
Hash tables
A hash function picks a bucket: collisions handled by chaining and by linear probing, and why the table must not get too full.
- 2
Graphs
Vertices and edges, directed and undirected, stored as an adjacency matrix or as adjacency lists.
- 3
Graph traversal: BFS and DFS
Breadth-first search with a queue finds the fewest edges to every vertex; depth-first search goes deep with recursion.
Module practice
- Which bucket?Easy
- Linear probingMedium
- Count the wordsHard
- Vertex degreesEasy
- Neighbour listsMedium
- Count the trianglesHard
- Can it be reached?Easy
- Distances from vertex 0Medium
- Connected componentsMedium
- Shortest way through a gridHard