Algorithms and Data Structures Cheatsheet
We summarize the performance characteristics of classic algorithms and
data structures for sorting, priority queues, symbol tables, and graph processing.
We also summarize some of the mathematics useful in the analysis of algorithms, including commonly encountered functions;
useful formulas and
approximations; properties of logarithms;
asymptotic notations; and solutions to divide-and-conquer recurrences.
Sorting.
The table below summarizes the number of compares for a variety of sorting
algorithms, as implemented in this textbook.
It includes leading constants but
ignores lower-order terms.
ALGORITHM CODE IN PLACE STABLE BEST AVERAGE WORST REMARKS
n exchanges;
selection sort Selection.java ✔ ½n2 ½n2 ½n2
quadratic in best case
use for small or
insertion sort Insertion.java ✔ ✔ n ¼n2 ½n2
partially-sorted arrays
rarely useful;
bubble sort Bubble.java ✔ ✔ n ½n2 ½n2
use insertion sort instead
tight code;
shellsort Shell.java ✔ n log3 n unknown c n 3/2 subquadratic
n log n guarantee;
mergesort Merge.java ✔ ½ n lg n n lg n n lg n
stable
n log n probabilistic guarantee;
quicksort Quick.java ✔ n lg n 2 n ln n ½n2
fastest in practice
n log n guarantee;
heapsort Heap.java ✔ n† 2 n lg n 2 n lg n
in place
†n lg n if all keys are distinct
Priority queues.
The table below summarizes the order of growth of the running time of
operations for a variety of priority queues, as implemented in this textbook.
It ignores
leading constants and lower-order terms.
Except as noted, all running times are worst-case running times.
DATA STRUCTURE CODE INSERT DEL-MIN MIN DEC-KEY DELETE MERGE
array BruteIndexMinPQ.java 1 n n 1 1 n
binary heap IndexMinPQ.java log n log n 1 log n log n n
d-way heap IndexMultiwayMinPQ.java logd n d logd n 1 logd n d logd n n
binomial heap IndexBinomialMinPQ.java 1 log n 1 log n log n log n
Fibonacci heap IndexFibonacciMinPQ.java 1 log n † 1 1† log n † 1
† amortized guarantee
Symbol tables.
The table below summarizes the order of growth of the running time of
operations for a variety of symbol tables, as implemented in this textbook.
It ignores
leading constants and lower-order terms.
worst case average case
DATA STRUCTURE CODE SEARCH INSERT DELETE SEARCH INSERT DELETE
sequential search
SequentialSearchST.java n n n n n n
(in an unordered list)
binary search
BinarySearchST.java log n n n log n n n
(in a sorted array)
binary search tree
BST.java n n n log n log n sqrt(n)
(unbalanced)
red-black BST
RedBlackBST.java log n log n log n log n log n log n
(left-leaning)
AVL AVLTreeST.java log n log n log n log n log n log n
hash table SeparateChainingHashST.java n n n 1† 1† 1†
(separate-chaining)
hash table
(linear-probing)
LinearProbingHashST.java n n n 1† 1† 1†
† uniform hashing assumption
Graph processing.
The table below summarizes the order of growth of the worst-case running time and memory usage (beyond the memory for the graph itself)
for a variety of
graph-processing problems, as implemented in this textbook.
It ignores leading constants and lower-order terms.
All running times are worst-case running times.
PROBLEM ALGORITHM CODE TIME SPACE
path DFS DepthFirstPaths.java E+V V
shortest path (fewest edges) BFS BreadthFirstPaths.java E+V V
cycle DFS Cycle.java E+V V
directed path DFS DepthFirstDirectedPaths.java E+V V
shortest directed path (fewest edges) BFS BreadthFirstDirectedPaths.java E+V V
directed cycle DFS DirectedCycle.java E+V V
topological sort DFS Topological.java E+V V
bipartiteness / odd cycle DFS Bipartite.java E+V V
connected components DFS CC.java E+V V
strong components Kosaraju–Sharir KosarajuSharirSCC.java E+V V
strong components Tarjan TarjanSCC.java E+V V
strong components Gabow GabowSCC.java E+V V
Eulerian cycle DFS EulerianCycle.java E+V E+V
directed Eulerian cycle DFS DirectedEulerianCycle.java E+V V
transitive closure DFS TransitiveClosure.java V (E + V) V2
minimum spanning tree Kruskal KruskalMST.java E log E E+V
minimum spanning tree Prim PrimMST.java E log V V
minimum spanning tree Boruvka BoruvkaMST.java E log V V
shortest paths (nonnegative weights) Dijkstra DijkstraSP.java E log V V
shortest paths (no negative cycles) Bellman–Ford BellmanFordSP.java V (V + E) V
shortest paths (no cycles) topological sort AcyclicSP.java V+E V
all-pairs shortest paths Floyd–Warshall FloydWarshall.java V3 V2
maxflow–mincut Ford–Fulkerson FordFulkerson.java E V (E + V) V
bipartite matching Hopcroft–Karp HopcroftKarp.java V ½ (E + V) V
assignment problem successive shortest paths AssignmentProblem.java n 3 log n n2
Commonly encountered functions.
Here are some functions that are commonly encountered
when analyzing algorithms.
FUNCTION NOTATION DEFINITION
floor ⌊x⌋ greatest integer ≤ x
ceiling ⌈x⌉ smallest integer ≥ x
binary logarithm lg x or log2 x y such that 2
y
= x
natural logarithm ln x or loge x y such that e y = x
common logarithm log
10
x y such that 10
y
= x
∗ ∗
iterated binary logarithm lg x 0 if x ≤ 1; 1 + lg (lg x) otherwise
harmonic number Hn 1 + 1/2 + 1/3 + … + 1/n
factorial n! 1 × 2 × 3 × … × n
binomial coefficient ( )
n n!
k
k! (n−k)!
Useful formulas and approximations.
Here are some useful formulas for approximations that are widely used in the analysis of algorithms.
Harmonic sum:
1 + 1/2 + 1/3 + … + 1/n ∼ ln n
Triangular sum:
1 + 2 + 3 + … + n = n (n + 1) / 2 ∼ n
2
/2
Sum of squares:
12 + 2
2
+ 3
2
+ … + n
2
∼ n
3
/3
Geometric sum:
If r ≠ 1 , then
1 + r + r2 + r
3
+ … + r
n
= (r
n+1
− 1) / (r − 1)
r = 1/2 :
1 + 1/2 + 1/4 + 1/8 + … + 1/2n ∼ 2
r = 2 :
1 + 2 + 4 + 8 + … + n/2 + n = 2n − 1 ∼ 2n , when n is a power of 2
Stirling's approximation:
lg(n!) = lg 1 + lg 2 + lg 3 + … + lg n ∼ n lg n
Exponential:
(1 + 1/n)n ∼ e; (1 − 1/n)
n
∼ 1/e
n
Binomial coefficients:
( k) ∼ n
k
/ k! when k is a small constant
n n n+1
Approximate sum by integral:
If f (x) is a monotonically increasing function, then
∫ f (x) dx ≤ ∑ f (i) ≤ ∫ f (x) dx
0 1
i=1
Properties of logarithms.
Definition: logb a = c means bc = a .
We refer to b as the base of the logarithm.
Special cases: logb b = 1, logb 1 = 0
Inverse of exponential: blog b
x
= x
Product: logb (x × y) = log
b
x + log
b
y
Division: logb (x ÷ y) = logb x − logb y
Finite product:
logb (x1 × x2 × … × xn ) = logb x1 + logb x2 + … + logb xn
Changing bases: logb x = log x / log b
c c
Rearranging exponents: xlog b
y
= y
logb x
Exponentiation: logb (xy ) = y log
b
x
Asymptotic notations: definitions.
NAME NOTATION DESCRIPTION DEFINITION
f (n) is equal to g(n) asymptotically f (n)
Tilde f (n) ∼ g(n)
lim = 1
(including constant factors) n→∞ g(n)
f (n) is bounded above by g(n) asymptotically there exist constants c and n0 ≥
> 0 0 such that
Big Oh f (n) is O(g(n))
(ignoring constant factors) 0 ≤ f (n) ≤ c ⋅ g(n) for
all n ≥ n0
f (n) is bounded below by g(n) asymptotically
Big Omega f (n) is Ω(g(n))
g(n) is O(f (n))
(ignoring constant factors)
f (n) is bounded above and below by g(n)
Big Theta f (n) is Θ(g(n)) asymptotically f (n) is both O(g(n)) and Ω(g(n))
(ignoring constant factors)
f (n) is dominated by g(n) asymptotically f (n)
Little oh f (n) is o(g(n))
lim = 0
(ignoring constant factors) n→∞ g(n)
f (n) dominates g(n) asymptotically
Little omega f (n) is ω(g(n))
g(n) is o(f (n))
(ignoring constant factors)
Common orders of growth.
NAME NOTATION EXAMPLE CODE FRAGMENT
array access
Constant O(1)
arithmetic operation op();
function call
binary search in a sorted array
for (int i = 1; i <= n; i = 2*i)
Logarithmic O(log n) insert in a binary heap op();
search in a red–black tree
sequential search
for (int i = 0; i < n; i++)
Linear O(n) grade-school addition op();
BFPRT median finding
mergesort for (int i = 1; i <= n; i++)
Linearithmic O(n log n) heapsort
for (int j = i; j <= n; j
= 2*j)
fast Fourier transform op();
enumerate all pairs for (int i = 0; i < n; i++)
Quadratic O(n )
2
insertion sort
for (int j = i+1; j < n;
j++)
grade-school multiplication op();
enumerate all triples for (int i = 0; i < n; i++)
for (int j = i+1; j < n;
j++)
Cubic O(n )
3
Floyd–Warshall
for (int k = j+1; k < n; k++)
grade-school matrix multiplication
op();
ellipsoid algorithm for LP
Polynomial O(n )
c
AKS primality algorithm
Edmond's matching algorithm
enumerating all subsets
Exponential O(n )
c
enumerating all permutations
2
backtracking search
Asymptotic notations: properties.
Reflexivity:
f (n) is O(f (n)).
Constants: If f (n) is O(g(n)) and c > 0 ,
then c ⋅ f (n) is O(g(n))).
Products: If f1 (n) is O(g 1 (n)) and f2 (n) is O(g 2 (n))) ,
then f1 (n) ⋅ f2 (n) is O(g 1 (n) ⋅ g 2 (n))) .
Sums: If f1 (n) is O(g 1 (n)) and f2 (n) is O(g 2 (n))) ,
then f1 (n) + f2 (n) is O(max{g 1 (n), g 2 (n)}) .
Transitivity: If f (n) is O(g(n)) and g(n) is O(h(n)) ,
then f (n) is O(h(n)) .
Polynomials: Let f (n) = a0 + a1n + … + ad n
d
with
a d > 0 . Then, f (n) is Θ(nd ) .
Logarithms and polynomials:
logb n is O(nd ) for every b > 0 and every d > 0 .
Exponentials and polynomials:
nd is O(rn ) for every r > 0 and every d > 0 .
Factorials:
n! is 2Θ(n log n) .
f (n)
Limits:
If lim = c
for some constant 0 < c < ∞ , then
f (n) is Θ(g(n)) .
n→∞ g(n)
f (n)
Limits:
If lim = 0 ,
then f (n) is O(g(n)) but not Θ(g(n)) .
n→∞ g(n)
f (n)
Limits:
If lim = ∞ ,
then f (n) is Ω(g(n)) but not O(g(n)) .
n→∞ g(n)
Here are some examples.
FUNCTION o(n )
2
O(n )
2
Θ(n )
2
Ω(n )
2
ω(n )
2
∼ 2n
2
∼ 4n
2
log
2
n ✔ ✔
10n + 45 ✔ ✔
2n
2
+ 45n + 12 ✔ ✔ ✔ ✔
4n
2 −
−
− 2√n ✔ ✔ ✔ ✔
3n
3
✔ ✔
2
n
✔ ✔
Divide-and-conquer recurrences.
For each of the following recurrences we assume T (1) = 0
and that n / 2 means either ⌊n / 2⌋ or
⌈n / 2⌉ .
RECURRENCE T (n) EXAMPLE
T (n) = T (n / 2) + 1 ∼ lg n binary search
T (n) = 2T (n / 2) + n ∼ n lg n mergesort
T (n) = T (n − 1) + n ∼
1
n
2
insertion sort
2
T (n) = 2T (n / 2) + 1 ∼ n tree traversal
T (n) = 2T (n − 1) + 1 ∼ 2
n
towers of Hanoi
T (n) = 3T (n / 2) + Θ(n) Θ(n
log2 3
) = Θ(n
1.58...
) Karatsuba multiplication
T (n) = 7T (n / 2) + Θ(n )
2
Θ(n
log2 7
) = Θ(n
2.81...
) Strassen multiplication
T (n) = 2T (n / 2) + Θ(n log n) Θ(n log
2
n) closest pair
Master theorem.
Let a ,
≥ 1 b ≥ 2 , and c > 0 and suppose that
T (n) is a function on the non-negative integers that satisfies
the divide-and-conquer recurrence
c
T (n) = a T (n / b) + Θ(n )
with T (0) = 0 and T (1) = Θ(1) , where n / b means
either ⌊n / b⌋ or either ⌈n / b⌉ .
If c < log
b
, then T (n)
a = Θ(n
log b
a
)
c
If c = logb a , then T (n) = Θ(n log n)
If c > log a , then T (n)
b
= Θ(n )
c