Algorithms and Data Structures Cheatsheet
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.
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.
hash table
(linear-probing)
LinearProbingHashST.java n n n 1† 1† 1†
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.
factorial n! 1 × 2 × 3 × … × n
n n!
binomial coefficient ( )
k k! (n−k)!
Triangular sum: 1 + 2 + 3 + … + n = n (n + 1) / 2 ∼ n
2
/2
Sum of squares: 1
2
+ 2
2
+ 3
2
+ … + n
2
∼ n
3
/3
Exponential: (1 + 1/n)
n
∼ e; (1 − 1/n)
n
∼ 1/e
n
Binomial coefficients: ( ) ∼ n
k
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.
Inverse of exponential: b
logb x
= x
Rearranging exponents: x
logb y
= y
logb x
Exponentiation: y
logb (x ) = y logb x
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)}).
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)
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) = 2T (n − 1) + 1 ∼ 2
n
towers of Hanoi
T (n) = 3T (n / 2) + Θ(n)
log2 3
Θ(n ) = Θ(n
1.58...
) Karatsuba multiplication
T (n) = 7T (n / 2) + Θ(n )
2 log2 7
Θ(n ) = Θ(n
2.81...
) Strassen multiplication
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
a , then T (n) = Θ(n
c
log n)
Remark: there are many different versions of the master theorem. The Akra–Bazzi theorem is among the most powerful.
Copyright © 2000–2023 Robert Sedgewick and Kevin Wayne. All rights reserved.