Graph Algorithms

Difficulty
Importance

The problem

Once data is modeled as a graph, real questions follow — what's reachable, what's the cheapest path, is there a cycle — and answering them by hand doesn't scale past a handful of nodes. Graph algorithms are the general-purpose machinery for answering these questions on graphs of any size.

Why now

Graph theory basics defined what a graph is and stated the key facts; stacks and queues supply the exact machinery (a stack for depth-first, a queue for breadth-first) those facts are computed with. This topic turns graph theory from something you reason about by hand into something a program executes.

Mental model

Depth-first search commits to one path and backtracks only when stuck — a stack, explicit or via recursion. Breadth-first search explores everything one step away before going further — a queue. Shortest-path algorithms (Dijkstra) are breadth-first search generalized to weighted edges: always expand the cheapest known frontier next.

Requires

Helps

Unlocks

Used in

Projects

  • Implement BFS and DFS on an adjacency-list graph and use each to solve a different problem (BFS for shortest unweighted path, DFS for cycle detection)
  • Implement topological sort and use it to compute a valid build order for a project with dependencies

Examples

  • BFS from a starting page finds the fewest-click path to any other page in a website's link graph
  • npm's dependency resolution is a topological sort over the package dependency DAG

Resources

Mastery checklist