?dsa? Cheatsheets by Princeton - Edu
?dsa? Cheatsheets by Princeton - Edu
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⌉ .
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 − 1) + 1 ∼ 2
n
towers of Hanoi
T (n) = 7T (n / 2) + Θ(n )
2
Θ(n
log2 7
) = Θ(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 )
If c < log
b
, then T (n)
a = Θ(n
log b
a
)
c
If c = logb a , then T (n) = Θ(n log n)
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.
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.
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.
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.
∗ ∗
iterated binary logarithm lg x 0 if x ≤ 1; 1 + lg (lg x) otherwise
factorial n! 1 × 2 × 3 × … × n
binomial coefficient ( )
n n!
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: log
b
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: b
y
log (x ) = y log
b
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)}) .
Logarithms and polynomials: logb n is O(nd ) for every b > 0 and every d > 0 .
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⌉ .
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 − 1) + 1 ∼ 2
n
towers of Hanoi
T (n) = 7T (n / 2) + Θ(n )
2
Θ(n
log2 7
) = Θ(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 )
If c < log
b
, then T (n)
a = Θ(n
log b
a
)
c
If c = logb a , then T (n) = Θ(n log n)