0% found this document useful (0 votes)
32 views

Algorithms and Data Structures Cheatsheet

Uploaded by

rrjk6861
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Algorithms and Data Structures Cheatsheet

Uploaded by

rrjk6861
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

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 dominated by insertion sort

n log3 n tight code;


shellsort Shell.java ✔ unknown c n 3/2 subquadratic

½ n log2 n n log2 n n log2 n n log n guarantee;


mergesort Merge.java ✔ stable

n log2 n n log n probabilistic guarantee;


quicksort Quick.java ✔ 2 n ln n ½n2 fastest in practice

2 n log2 n 2 n log2 n n log n guarantee;


heapsort Heap.java ✔ n† in place
† n log2 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
(separate-chaining)
SeparateChainingHashST.java n n n 1† 1† 1†

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 log


2
x y such that 2 y = x
natural logarithm ln x or loge x y such that e y = x

common logarithm log10 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

n n!
binomial coefficient ( )
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: 1
2
+ 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/2


n
∼ 2

r = 2 : 1 + 2 + 4 + 8 + … + n/2 + n = 2n − 1 ∼ 2n , when n is a power of 2

Stirling's approximation: log2 (n!) = log2 1 + log2 2 + log2 3 + … + log2 n ∼ n log2 n

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.

Special cases: logb b = 1, logb 1 = 0

Inverse of exponential: b
logb x
= x

Product: logb (x × y) = logb x + logb y

Division: logb (x ÷ y) = logb x − logb y

Finite product: log (x1 × x2 × … × xn )


b
= log
b
x1 + log
b
x2 + … + log
b
xn

Changing bases: logb x = logc x / logc b

Rearranging exponents: x
logb y
= y
logb x

Exponentiation: y
logb (x ) = y logb x

Asymptotic notations: definitions.


NAME NOTATION DESCRIPTION DEFINITION
f (n) is equal to g(n) f (n)
Tilde f (n) ∼ g(n) asymptotically lim = 1
n→∞ g(n)
(including constant factors)
there exist constants
f (n)is bounded above by g(n) c > 0 and n0 ≥ 0
f (n) is
Big Oh asymptotically such that
O(g(n))
(ignoring constant factors) 0 ≤ f (n) ≤ c ⋅ g(n)
for all n ≥ n0

is f (n)is bounded below by g(n)


Big f (n)
asymptotically g(n) is O(f (n))
Omega Ω(g(n))
(ignoring constant factors)
f (n) is bounded above and below
f (n) is f (n) is both O(g(n))
Big Theta by g(n) asymptotically
Θ(g(n)) and Ω(g(n))
(ignoring constant factors)

f (n) is f (n)is dominated by g(n) f (n)


Little oh asymptotically lim = 0
o(g(n)) n→∞ g(n)
(ignoring constant factors)

is f (n) dominates g(n)


Little f (n)
asymptotically g(n) is o(f (n))
omega ω(g(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 2
O(n ) 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 3
O(n ) Floyd–Warshall for (int k = j+1; k < n; k++)
grade-school matrix multiplication op();

ellipsoid algorithm for LP


Polynomial c
O(n ) AKS primality algorithm
Edmond's matching algorithm
enumerating all subsets
Exponential enumerating all permutations
c
O(n )
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: n


d
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 ∼ log2 n binary search

T (n) = 2T (n / 2) + n ∼ n log2 n mergesort


1
T (n) = T (n − 1) + n ∼
2
n
2
insertion sort

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)
log2 3
Θ(n ) = Θ(n
1.58...
) Karatsuba multiplication

T (n) = 7T (n / 2) + Θ(n )
2 log2 7
Θ(n ) = Θ(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 < logb a , then T (n) = Θ(n


log b
a
)

If c = log
b
a , then T (n) = Θ(n
c
log n)

If c > logb a , then T (n) = Θ(n )


c

Remark: there are many different versions of the master theorem. The Akra–Bazzi theorem is among the most powerful.

Last modified on February 03, 2023.

Copyright © 2000–2023 Robert Sedgewick and Kevin Wayne. All rights reserved.

You might also like