Skip to main content

Updated Sep 11, 2026

Graph Algorithms Cheat Sheet

This page is a reference, not a tutorial — see Graph Algorithms Overview for a first read through the folder. Every complexity below names its case (best / average / amortized / worst); the full argument for each lives on that algorithm's own page, alongside its worked trace.

Representation cost​

RepresentationSpaceEdge lookup (u,v)?Iterate u's neighbours
Adjacency listO(V+E)O(V + E)O(deg⁡(u))O(\deg(u)) worst caseO(deg⁡(u))O(\deg(u))
Adjacency matrixO(V2)O(V^2)O(1)O(1)O(V)O(V) — every column must be checked
Edge listO(E)O(E)O(E)O(E) — a full scanO(E)O(E) — a full scan, no per-vertex index

Adjacency lists win whenever the graph is sparse (E≪V2E \ll V^2), which is the common case — most of the algorithms on this page's table assume one. Matrices win only when edge-existence queries dominate and the graph is dense enough that O(V2)O(V^2) space is not itself the problem (Kruskal's sort or an edge-list input format is the exception that wants an edge list directly, to avoid rebuilding one). See CLRS 4th ed., §20.1, for the same comparison stated formally.

"The question is X" → run Y​

QuestionAlgorithmPage
Visit every reachable vertex, or find shortest paths in an unweighted graphBFSTraversal: BFS & DFS
Detect a cycle, or compute finish times / a valid ordering probeDFSTraversal: BFS & DFS
Order tasks so every dependency precedes its dependentsKahn's or DFS-based topological sortTopological Sort
Is there a cycle, and whereThree-colour DFS (directed) or union-find (undirected)Cycle Detection
Shortest path, one source, non-negative weightsDijkstra'sShortest Paths
Shortest path, one source, negative weights allowedBellman–FordShortest Paths
Shortest path, all pairs, dense graphFloyd–WarshallShortest Paths
Shortest path to one known target, with a distance hint availableA*A* & Heuristic Search
Connect every vertex for least total edge weightKruskal's or Prim'sMinimum Spanning Trees
Which vertices can all reach each other (directed graph)Kosaraju's or Tarjan'sStrongly Connected Components
Split into two groups with every relationship crossing between themBFS 2-colouringBipartite Graphs & 2-Coloring
Pair up two sides for the largest possible matchingAugmenting paths / Hopcroft–KarpBipartite Graphs & 2-Coloring
Maximum throughput from a source to a sink under capacitiesFord-Fulkerson / Edmonds-KarpNetwork Flow
A combinatorial problem (selection, scheduling) with a source/sink shapeModel it as flow, then Edmonds-KarpNetwork Flow

Decision flow​

The first three questions — reachability, ordering, shortest path — cover most of what a real system asks a graph. Weighted-vs-unweighted and negative-vs-non-negative are the two branches that most often get skipped by habit (reaching for Dijkstra's on a graph that turns out to have a negative edge is the single most common mistake this flow is meant to prevent — see the Dijkstra danger box on Shortest Paths).

Complexity matrix​

AlgorithmTime (worst)SpaceNotes
BFS / DFSO(V+E)O(V + E)O(V)O(V)The primitive everything else on this page builds from
Topological sort (Kahn's or DFS)O(V+E)O(V + E)O(V)O(V)Undefined / errors on a cyclic graph
Cycle detectionO(V+E)O(V + E)O(V)O(V)Three-colour DFS (directed) or union-find (undirected)
Dijkstra's (binary heap)O((V+E)log⁡V)O((V+E)\log V)O(V)O(V)Wrong, silently, on any negative edge
Bellman–FordO(V⋅E)O(V \cdot E)O(V)O(V)The only one of these that also detects negative cycles
Floyd–WarshallO(V3)O(V^3)O(V2)O(V^2)All-pairs; simple to implement, poor on sparse graphs
A*, admissible heuristicO((V+E)log⁡V)O((V+E)\log V)O(V)O(V)Same worst case as Dijkstra's; usually far fewer expansions
Kruskal'sO(Elog⁡E)O(E \log E)O(V)O(V)Dominated by the one sort; needs union-find
Prim's (binary heap)O(Elog⁡V)O(E \log V)O(V)O(V)Better than Kruskal's on dense, adjacency-list graphs
Kosaraju's / Tarjan's (SCC)O(V+E)O(V + E)O(V)O(V)Two DFS passes vs. one with a lowlink array
Bipartite 2-colouringO(V+E)O(V + E)O(V)O(V)Also the odd-cycle witness when it fails
Bipartite matching, Kuhn'sO(V⋅E)O(V \cdot E)O(V)O(V)Hopcroft–Karp improves this to O(EV)O(E\sqrt{V})
Edmonds-Karp max flowO(VE2)O(VE^2)O(V+E)O(V + E)BFS-selected augmenting paths is what earns this bound

Recall​

References​

  • Cormen, Leiserson, Rivest & Stein, Introduction to Algorithms, 4th ed., §20.1 (representations), Ch. 20-22 (traversal, topological sort), Ch. 24-26 (shortest paths, MST, flow) — the chapters this page's matrices summarise.
  • Sedgewick & Wayne, Algorithms, 4th ed., Ch. 4 — the same algorithms with an empirical, implementation-first treatment.