| Quick Reference Graphs CheatsheetA graph on one page — lists, searches, and the traps around visited marks, negative weights, and directed cycles. 10 sections 10 tables 14 code snippets Vertices and edgesUndirected, both ways Directed, one way The parts
This page is a graph. Network flow is left out. How the edges are storedSparse graphs prefer a list List, matrix, or edge list
Breadth-first searchMark it, then enqueue it A queue, fewest edges first
Depth-first searchGray is still on the path Three colors
Where a cycle shows upIgnore the parent Directed and undirected differ
Shortest pathsKeep a strictly better distance Hops are not weights Match the algorithm to the weights
Negative weightsOne pass per vertex, almost Bellman-Ford and a DAG
Topological orderA neighbor just lost its last incoming edge Only a DAG has this order
A minimum spanning treeSkip an edge inside one set Lightest set of edges that stays connected
Interview trapsThe way back is missing Final too early Finished is not a back edge Traps worth memorising
How to use this page: store an undirected edge both ways, mark a vertex when you enqueue it, and use Bellman-Ford when a weight can be negative. |