iq.lab
Python starts when a code cell comes near or you run one

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.

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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.

Next moduleBacktracking