Quick Reference

Graph Algorithms Cheat Sheet: Complexity, Uses, and How to Pick

One page to scan before an interview or while coding. Every major graph algorithm, its time and space complexity, what it is best for, and a decision guide for choosing the right one when the clock is running.

11 Min Read Updated: July 2026 All Levels
Mohammed Islam Hadjoudj
Mohammed Islam Hadjoudj
Expert Operations Research Engineer
Traversal BFS DFS Shortest Paths Dijkstra Bellman-Ford Floyd-Warshall A* Spanning Trees Kruskal Prim Ordering Topological Sort Cycle Detection Connectivity Union-Find Tarjan / Kosaraju (SCC) Network Flow Ford-Fulkerson Edmonds-Karp
The six families of graph algorithms. Almost every graph problem falls into one of them.

This cheat sheet is built for two moments: the last hour before a technical interview, and the middle of a coding session when you know the shape of the problem but need the exact tool. It is deliberately compact. For the full story behind each algorithm, follow the links to the deep-dive articles, and if you want a structured path through all of it, start with the graph theory study roadmap.

Throughout, V is the number of vertices (nodes) and E is the number of edges.

The Master Complexity Table

The single most useful thing to have memorized. Complexities assume the standard efficient implementation (binary heap for Dijkstra and Prim, path compression and union by rank for union-find).

AlgorithmBest forTimeSpace
BFSShortest path in unweighted graphs, level orderO(V + E)O(V)
DFSConnectivity, cycles, exploring structureO(V + E)O(V)
DijkstraShortest path, non-negative weightsO((V + E) log V)O(V)
Bellman-FordShortest path with negative weightsO(V · E)O(V)
Floyd-WarshallAll-pairs shortest paths, small dense graphsO(V³)O(V²)
A*Heuristic shortest path (maps, games)O(E) typicalO(V)
KruskalMinimum spanning tree, sparse graphsO(E log E)O(V)
PrimMinimum spanning tree, dense graphsO((V + E) log V)O(V)
Topological SortOrdering a DAG by dependenciesO(V + E)O(V)
Union-FindDynamic connectivity, groupingO(α(V)) per opO(V)
Tarjan / KosarajuStrongly connected componentsO(V + E)O(V)
Edmonds-KarpMaximum flow, minimum cutO(V · E²)O(V + E)
Need the actual code? This table lists 12 essentials at a glance. The Algorithms Handbook has all 55, each with the pseudocode and its complexity worked out step by step.

Note on A*: its running time depends entirely on the heuristic. With a perfect heuristic it walks almost straight to the goal; with a useless one it degrades to Dijkstra. The α in union-find is the inverse Ackermann function, effectively a small constant for any input you will ever see. For the reasoning behind every row, see the graph algorithms complexity guide.

Graph Representations

Before any algorithm, you choose how to store the graph. This one decision affects every complexity above.

RepresentationSpaceEdge lookupBest for
Adjacency ListO(V + E)O(degree)Sparse graphs, the default choice
Adjacency MatrixO(V²)O(1)Dense graphs, constant-time lookups

Rule of thumb: reach for the adjacency list unless the graph is dense or you need constant-time edge checks. A 2D grid is an implicit graph where each cell is a node connected to its neighbours, so you often need no explicit representation at all.

Traversal: BFS and DFS

The two algorithms everything else is built on. Learn the full comparison in BFS vs DFS.

Shortest Paths

The most common family in interviews and in practice. The right choice is dictated by the edge weights. The full deep dive is in understanding shortest path algorithms.

SituationUseWhy
Unweighted edgesBFSFirst arrival is the shortest path
Non-negative weightsDijkstraGreedy with a min-heap, always correct here
Negative weightsBellman-FordRelaxes edges V-1 times, detects negative cycles
All pairs at onceFloyd-WarshallThree nested loops, tiny code, great on small graphs
You have a heuristicA*Dijkstra guided toward the goal, see A*
The classic trap: never run Dijkstra on a graph with negative edges. It commits to a node as final too early and can return a wrong answer. Reach for Bellman-Ford instead.

Minimum Spanning Trees

Connect every vertex at the lowest total edge cost. Both algorithms are correct; pick by graph density. Full walk-throughs in minimum spanning trees.

Ordering and Connectivity

All three appear constantly in interviews. See the worked patterns in essential graph algorithms for coding interviews.

Network Flow

Model throughput, matching, and bottlenecks. The elegant result here is that the maximum flow equals the minimum cut. Full treatment in network flow and the max-flow min-cut theorem.

Which Algorithm Should I Use?

The fastest way to use this cheat sheet: read the left column, jump to the right.

If you need to…Reach for
Visit or explore every nodeBFS or DFS
Find the shortest path in an unweighted graphBFS
Find the shortest path with non-negative weightsDijkstra
Handle negative edge weightsBellman-Ford
Get shortest paths between all pairsFloyd-Warshall
Find a fast path with a heuristic (maps, games)A*
Connect everything at minimum costKruskal or Prim
Order tasks by their dependenciesTopological Sort
Check if two nodes are connected, or group itemsUnion-Find
Find clusters in a directed graphTarjan or Kosaraju (SCC)
Maximise throughput or find a bottleneckEdmonds-Karp (max-flow)

Turn the table into intuition

A cheat sheet tells you which algorithm; watching it run tells you why. Step through any of these on a live graph.

Open the Algorithm Visualizer

Frequently Asked Questions

What is the time complexity of Dijkstra's algorithm?

With a binary heap (priority queue), Dijkstra's algorithm runs in O((V + E) log V) time and O(V) space. With a simple array instead of a heap it is O(V squared), which can be faster on dense graphs.

Which graph algorithm should I use for shortest paths?

It depends on the graph. Use BFS for unweighted graphs, Dijkstra for non-negative weights, Bellman-Ford when there are negative weights, Floyd-Warshall for all-pairs shortest paths, and A* when you have a good heuristic (maps and games).

Should I use an adjacency list or an adjacency matrix?

Use an adjacency list for sparse graphs: it uses O(V + E) space and is the default for most problems. Use an adjacency matrix for dense graphs or when you need O(1) edge lookups, at the cost of O(V squared) space.

Which graph algorithms should I memorize for coding interviews?

The core five are BFS, DFS, Dijkstra's algorithm, topological sort, and union-find. Together they cover the large majority of graph questions asked in technical interviews.

Further Learning Resources

Bookmark This, Then Go Deeper

A cheat sheet gets you unstuck fast. Real fluency comes from watching these algorithms run. Pick one and press play.

Practice with the Algorithm Visualizer