Module 10 · 6 lessons · about 9 hours 50 minutes
Graphs
Adjacency lists and grids, depth-first and breadth-first search, shortest steps and multi-source spreads, dependency order, union-find, and Dijkstra's shortest paths.
- Lessons
- 0/6
- Exercises
- 0/44
- Code
- 0/6
- Problems
- 0/22
- Checkpoint
- Not taken
Already comfortable with this? Take the checkpoint cold. If you pass, move on and come back only for what you missed.
- 1What a graph isA graph is a set of things joined by connections. Problems hand you one as a list of edges or as a grid. You turn the edges into an adjacency list so a node's neighbors cost one lookup, and you spread out from a start with a set and a stack to find everything it can reach.1 h 10 min
- 2Depth-first searchDepth-first search walks a graph by going as deep as it can, then backing up to the last place with a path it has not tried. With a visited set it reaches every node once, and counting how many searches you start counts the islands on a map.1 h 45 min
- 3Breadth-first searchBreadth-first search walks a graph in rings, nearest first. A queue keeps that order, so the first time the walk reaches a node, it got there by a path with the fewest edges.2 h
- 4Dependencies and topological sortWhen tasks wait on other tasks, a topological order runs each task after everything it waits for. Kahn's algorithm builds one by counting what each task still waits for and releasing a task the moment that count reaches 0, and it spots a cycle when tasks are left over.1 h 35 min
- 5Union-findUnion-find keeps groups that only ever merge. Each item points at a parent, the root at the top names the group, and two small tricks keep the trees so flat that each lookup costs nearly O(1) over a run, so counting groups and catching the edge that closes a cycle both take one pass over the edges.1 h 40 min
- 6Weighted shortest pathsWhen edges have different costs, the route with the fewest edges is not always the cheapest. Dijkstra's algorithm settles the closest node first, keeps a heap of (distance, node) pairs, and finds every shortest distance in O((V + E) log V).1 h 40 min
- Checkpoint12 questions. Pass with 80% to finish the module.
Problems for this module
The lessons teach some of these step by step (each says which). The rest are practice: try each in its target time, and solved problems come back for spaced re-solves.
- easyIs there a path15 min
- easyFlood fill15 min
- mediumNumber of islands25 min
- mediumLargest island25 min
- mediumWater to both oceans25 min
- mediumClone a graph25 min
- hardMake the largest island40 min
- mediumShortest clear path in a grid25 min
- mediumRotting oranges25 min
- hardWord ladder40 min
- mediumDistance to the nearest zero25 min
- mediumCan every course be finished25 min
- mediumFind an order for the courses25 min
- hardRecover an alien alphabet40 min
- mediumCount the provinces25 min
- mediumRedundant connection25 min
- mediumConnected components25 min
- mediumNetwork delay time25 min
- mediumPath with minimum effort25 min
- mediumCheapest flights within k stops25 min
- mediumMerge the accounts25 min
- mediumThe best hub in a tree25 min