Algorithms and Data Structures Cheatsheet
Algorithms and Data Structures Cheatsheet
1. Fundamentals
1.1 Programming Model
1.2 Data Abstraction
1.3 Stacks and Queues
1.4 Analysis of Algorithms
1.5 Case Study: Union-Find
2. Sorting
2.1 Elementary Sorts
2.2 Mergesort
2.3 Quicksort
2.4 Priority Queues
2.5 Sorting Applications
3. Searching
3.1 Symbol Tables
3.2 Binary Search Trees
3.3 Balanced Search Trees
3.4 Hash Tables
3.5 Searching Applications
4. Graphs
4.1 Undirected Graphs
4.2 Directed Graphs
4.3 Minimum Spanning Trees
4.4 Shortest Paths
5. Strings
5.1 String Sorts
5.2 Tries
5.3 Substring Search
5.4 Regular Expressions
5.5 Data Compression
6. Context
6.1 Event-Driven Simulation
6.2 B-trees
6.3 Suffix Arrays
6.4 Maxflow
6.5 Reductions
6.6 Intractability
:
Related Booksites
Web Resources
FAQ
Data
Code
Errata
Lectures
Cheatsheet
References
Online Course
Programming Assignments
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.
n log n
½ n lg
mergesort Merge.java n lg n n lg n guarantee;
n
stable
n log n
probabilistic
quicksort Quick.java n lg n 2 n ln n ½n2 guarantee;
fastest in
practice
n log n
heapsort Heap.java n † 2 n lg n 2 n lg n guarantee;
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
CODE INSERT DEL-MIN MIN DEC-KEY DELETE MERGE
STRUCTURE
array BruteIndexMinPQ.java 1 n n 1 1 n
binary heap IndexMinPQ.java log n log n 1 log n log n n
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.
V ½ (E +
bipartite matching Hopcroft–Karp HopcroftKarp.java V
V)
successive shortest
assignment problem AssignmentProblem.java n 3 log n n2
paths
(k )
n n!
binomial coefficient k! (n−k)!
1 + r + r 2 + 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
lg(n!) = lg 1 + lg 2 + lg 3 + … + lg n ∼ n lg n
Stirling's approximation:
∫0 ∑ ∫1
f (x) dx ≤ f (i) ≤ f (x) dx
i=1
:
Properties of logarithms.
blogb x = x
Inverse of exponential:
Products: If f1 (n) is O(g1 (n)) and f2 (n) is O(g2 (n))), then f1 (n) ⋅ f2 (n) is
O(g1 (n) ⋅ g2 (n))).
Sums: If f1 (n) is O(g1 (n)) and f2 (n) is O(g2 (n))), then f1 (n) + f2 (n) is
O(max{g1 (n), g2 (n)}) .
Transitivity: If f (n) is O(g(n)) and g(n) is O(h(n)), then f (n) is O(h(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)
2n2 + 45n + 12
4n2 − 2√n‾
:
3n3
2n
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
1 2
T(n) = T(n − 1) + n ∼ 2
n insertion sort
Master theorem.
Leta≥1b≥2 , , and c>0 and suppose that T(n) is a function on the non-negative integers that
satisfies the divide-and-conquer recurrence
Remark: there are many different versions of the master theorem. The Akra–Bazzi theorem is among the most powerful.