Algorithm complexity

A quick reference for the searching, graph, heap, and interval algorithms that come up most in interviews. Detailed breakdowns follow in the next section.

AlgorithmBestAverageWorstSpace
Linear searchO(1)O(n)O(n)O(1)
Binary searchO(1)O(log n)O(log n)*O(1)*
BFSO(V + E)O(V + E)O(V + E)O(V)
DFSO(V + E)O(V + E)O(V + E)O(V)*
Heap insert—O(log n)O(log n)O(1) auxiliary
Heap peekO(1)O(1)O(1)O(1)
Heap extract min or max—O(log n)O(log n)O(1) auxiliary
Merge intervals—O(n log n)O(n log n)O(n)*
Union-Find operation—Amortized O(α(n))—O(n) total structure
Dijkstra with binary heap——O((V + E) log V)*O(V + E)

Graph and tree algorithms

V is the number of vertices, E the number of edges, n the number of tree nodes, and h the tree height (about log n when balanced, up to n when skewed). Graph bounds assume an adjacency list unless noted.

AlgorithmTimeSpaceNotes
Breadth-first search (BFS)O(V + E)O(V)Uses a queue. Finds shortest paths by edge count. O(V2) with an adjacency matrix.
Depth-first search (DFS)O(V + E)O(V)Uses recursion or an explicit stack, so depth can reach V. Basis for cycle detection and topological sort. O(V2) with an adjacency matrix.
Binary search on a sorted arrayO(log n)O(1) iterative, O(log n) recursiveRequires sorted, random-access data. Also works on any monotonic yes/no condition, not just arrays.
Binary search tree lookupO(h)O(1) iterativeO(log n) when balanced, O(n) when skewed.
Preorder, inorder, postorder traversalO(n)O(h)Recursion or stack depth is the height. Inorder on a binary search tree visits keys in sorted order.
Level-order traversalO(n)O(w)Uses a queue; w is the widest level, which can be about n/2 in a complete tree.
Morris traversalO(n)O(1)Inorder or preorder with no stack. Temporarily rewires tree pointers, so it is not safe for concurrent readers.

Heap operations

For a binary heap holding n items.

OperationTimeNotes
Peek min or maxO(1)Read the root.
InsertO(log n)Append at the end, then sift up. O(1) on average for random data.
Extract min or maxO(log n)Swap the root with the last item, remove it, then sift down.
Build heap from an arrayO(n)Sift down from the last parent to the root. Faster than n separate inserts, which cost O(n log n).
Change priority or delete at a known indexO(log n)Finding the index without a position map costs O(n).
Search for an arbitrary valueO(n)A heap only orders parents against children.
HeapsortO(n log n)O(1) extra space. See the sorting table.

Shortest-path algorithms

AlgorithmEdge weightsTimeSpaceNotes
BFSUnweightedO(V + E)O(V)Single source.
0-1 BFS0 or 1 onlyO(V + E)O(V)Single source. Uses a deque instead of a heap.
Shortest path in a DAGAny, including negativeO(V + E)O(V)Single source. Relax edges in topological order.
Dijkstra with binary heapNon-negativeO((V + E) log V)O(V + E)*Single source. The usual choice for sparse graphs.
Dijkstra with arrayNon-negativeO(V2)O(V)Single source. Beats the heap version on dense graphs.
Dijkstra with Fibonacci heapNon-negativeO(E + V log V)O(V + E)Single source. Best bound, rarely fastest in practice.
Bellman–FordNegative allowedO(V·E)O(V)Single source. Detects negative cycles reachable from the source.
Floyd–WarshallNegative allowed, no negative cyclesO(V3)O(V2)All pairs. Works on an adjacency matrix.

Minimum spanning trees

These algorithms take a connected, undirected, weighted graph. On a disconnected graph they produce a minimum spanning forest.

AlgorithmTimeSpaceNotes
KruskalO(E log E)O(V + E)Sort edges, then add each one that joins two components, using Union-Find. O(E log E) equals O(E log V) because E is at most V2. Works from an edge list.
Prim with binary heapO(E log V)O(V + E)Grows one tree from a start vertex. Good for sparse graphs.
Prim with arrayO(V2)O(V)Good for dense graphs stored as a matrix.
Prim with Fibonacci heapO(E + V log V)O(V + E)Best bound, rarely fastest in practice.
BorůvkaO(E log V)O(V)Each round adds the cheapest edge leaving every component. Components at least halve each round, so it needs at most log V rounds. Easy to parallelize.

Topics on this page

Each one has its own page with an explanation, code, and the mistakes people make.

Keyboard shortcuts

/
Search topics
?
Show this list of shortcuts
r
Open the revision sheet
b
Save or unsave the focused card, or the topic you are reading
l
Mark the focused card, or the topic you are reading, as learned
Space Left arrow Right arrow
In the visualizer: play or pause, step back, step forward
1 to 4 Enter
In the quiz: pick an answer, then go to the next question
Escape
Close this list