Graphs and pathfinding lesson plan
Guide a Mars rover with the algorithms behind every map app and game: graphs as data, BFS, DFS, Dijkstra and A* written from scratch, mazes carved and solved, and a rover console that plans around rocks on a Raspberry Pi.
Objectives and materials
By the end, students can
- Stations, roads and terrain maps as data a program can search.
- Breadth first and depth first: rings, dives and what each one is for.
- Cheapest routes with Dijkstra, then A* with a sense of direction.
- Carve and solve mazes, then run Rover 7 through one.
Materials for the whole unit
- Pushbutton
- Raspberry Pi 4 Model B
- Traffic light module
Every part is already on the simulator’s bench, so there is nothing to buy or hand out. The counts are the most any one lesson uses, if you build it on real breadboards too.
Graphs
Stations, roads and terrain maps as data a program can search.
| Lesson | Time | What students do |
|---|---|---|
| Graphs as dataA Mars rover's road map as a graph: stations and roads in a dict of lists, degrees and the busiest hub, a checker that finds a one-way road, counting edges, and a terrain map turned into a graph of cells. | 55 min |
|
Materials
- Pushbutton
- Raspberry Pi 4 Model B
- Traffic light module
Check for understanding
Each student finishes "Graphs as data" on their own, without opening the hints. The simulator checks 6 of its 6 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "How big is the map graph?".
Search
Breadth first and depth first: rings, dives and what each one is for.
| Lesson | Time | What students do |
|---|---|---|
| Breadth-first search (BFS)Breadth-first search with a deque: the order it visits stations, distance in hops, parents that rebuild the route, the same function on the rover's grid, the path drawn on the map, and a red lamp when a rock walls the goal in. | 55 min |
|
| Depth-first search (DFS)Depth-first search, recursive and with a stack: the order it dives, which stations a dust storm cuts off, why its first path on the map is not the shortest, and the recursion limit a big field runs into. | 55 min |
|
Materials
- Pushbutton
- Raspberry Pi 4 Model B
- Traffic light module
Check for understanding
Each student finishes "Depth-first search (DFS)" on their own, without opening the hints. The simulator checks 6 of its 6 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Your own stack never runs out".
Costs and guesses
Cheapest routes with Dijkstra, then A* with a sense of direction.
| Lesson | Time | What students do |
|---|---|---|
| DijkstraWhen roads have costs, fewest roads is not fastest: drive times as edge weights, heapq as a priority queue, Dijkstra's algorithm on the stations and then on terrain with soft sand, and a count of the squares it had to look at. | 45 min |
|
| A* searchDijkstra with a sense of direction: a heuristic, Manhattan distance, the f = g + h priority, the same best route for far fewer squares, a big plain where the saving is huge, and what an overconfident heuristic costs. | 45 min |
|
Materials
- Pushbutton
- Raspberry Pi 4 Model B
- Traffic light module
Check for understanding
Each student finishes "A* search" on their own, without opening the hints. The simulator checks 5 of its 5 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "An overconfident guess".
Mazes and the boss
Carve and solve mazes, then run Rover 7 through one.
| Lesson | Time | What students do |
|---|---|---|
| MazesMake mazes with a randomized DFS, prove they are perfect by counting passages, solve them with BFS, draw the solution, count dead ends, and change the seed for a new maze every time. | 55 min |
|
| Boss: a robot that solves a mazeRover 7's console: a maze, A* planning on command, status lamps for planning, route and walled in, drive commands with turns, rocks typed in live that force a re-plan, and a drive to the goal. | 55 min |
|
Materials
- Pushbutton
- Raspberry Pi 4 Model B
- Traffic light module
Check for understanding
Each student finishes "Boss: a robot that solves a maze" on their own, without opening the hints. The simulator checks 6 of its 6 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Drive to the goal".
Teach this unit as a class
Start a class from this course and its 7 lessons are assigned in one go, in order. Students join with a six-character code or printed login cards, and each one’s work comes back to you.
The class tools are free for 30 days for one class of up to 20 students, with no card. The lessons themselves are free either way.