0% found this document useful (0 votes)
1K views5 pages

Data Structures & Algorithms Cheatsheet

The document summarizes performance characteristics of algorithms and data structures for sorting, priority queues, symbol tables, and graph processing. It provides tables that outline the time complexity of operations like search, insert, and delete for common algorithms for each task, including sorting algorithms like merge sort and quicksort, priority queue implementations like binary heaps and binomial heaps, symbol table structures like hash tables and balanced binary search trees, and graph algorithms for problems like finding shortest paths and connected components.

Uploaded by

Arfizur Rahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views5 pages

Data Structures & Algorithms Cheatsheet

The document summarizes performance characteristics of algorithms and data structures for sorting, priority queues, symbol tables, and graph processing. It provides tables that outline the time complexity of operations like search, insert, and delete for common algorithms for each task, including sorting algorithms like merge sort and quicksort, priority queue implementations like binary heaps and binomial heaps, symbol table structures like hash tables and balanced binary search trees, and graph algorithms for problems like finding shortest paths and connected components.

Uploaded by

Arfizur Rahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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 [Link] ✔ ½n2 ½n2 ½n2

quadratic in best case


use for small or
insertion sort [Link] ✔ ✔ n ¼n2 ½n2

partially-sorted arrays
rarely useful;
bubble sort [Link] ✔ ✔ n ½n2 ½n2

use insertion sort instead


tight code;
shellsort [Link] ✔ n log3 n unknown c n 3/2 subquadratic

n log n guarantee;
mergesort [Link] ✔ ½ n lg n n lg n n lg n
stable

n log n probabilistic guarantee;


quicksort [Link] ✔ n lg n 2 n ln n ½n2

fastest in practice
n log n guarantee;
heapsort [Link] ✔ 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 [Link] 1 n n 1 1 n
binary heap [Link] log n log n 1 log n log n n
d-way heap [Link] logd n d logd n 1 logd n d logd n n
binomial heap [Link] 1 log n 1 log n log n log n
Fibonacci heap [Link] 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

[Link] n n n n n n
(in an unordered list)
binary search

[Link] log n n n log n n n
(in a sorted array)
binary search tree
[Link] n n n log n log n sqrt(n)
(unbalanced)

red-black BST
[Link] log n log n log n log n log n log n
(left-leaning)

AVL [Link] log n log n log n log n log n log n


hash table [Link] n n n 1† 1† 1†

(separate-chaining)
hash table

(linear-probing)
[Link] 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 [Link] E+V V
shortest path (fewest edges) BFS [Link] E+V V
cycle DFS [Link] E+V V
directed path DFS [Link] E+V V
shortest directed path (fewest edges) BFS [Link] E+V V
directed cycle DFS [Link] E+V V
topological sort DFS [Link] E+V V
bipartiteness / odd cycle DFS [Link] E+V V
connected components DFS [Link] E+V V
strong components Kosaraju–Sharir [Link] E+V V
strong components Tarjan [Link] E+V V
strong components Gabow [Link] E+V V
Eulerian cycle DFS [Link] E+V E+V
directed Eulerian cycle DFS [Link] E+V V
transitive closure DFS [Link] V (E + V) V2
minimum spanning tree Kruskal [Link] E log E E+V
minimum spanning tree Prim [Link] E log V V
minimum spanning tree Boruvka [Link] E log V V
shortest paths (nonnegative weights) Dijkstra [Link] E log V V
shortest paths (no negative cycles) Bellman–Ford [Link] V (V + E) V
shortest paths (no cycles) topological sort [Link] V+E V
all-pairs shortest paths Floyd–Warshall [Link] V3 V2
maxflow–mincut Ford–Fulkerson [Link] E V (E + V) V
bipartite matching Hopcroft–Karp [Link] V ½ (E + V) V

assignment problem successive shortest paths [Link] 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

You might also like