Algorithmic Cheatsheet: Typesetting Math: 97%
Algorithmic Cheatsheet: Typesetting Math: 97%
Algorithmic cheatsheet
2014-10-02
General results
Data structures
Sorting
Graph algorithms
This page sums up some important results from computer science. They are extracted from theIntroduction to Algorithms (Third Edition), by
Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest and Clifford Stein. We highly recommend it.
The following information is organized in several sections grouping definitions. Each definition points to the Introduction to Algorithms for
further information using the abbreviation CLRS (from the authors' names).
The formul are written using LATEX and the final render of the page is done by the Javascript library MathJax (currently the simplest way to
use LATEX in HTML).
The current figures use images from external websites. Clicking on it will redirect you to their original webpages.
General results
Master Theorem
CLRS: p93
Recurrence of the form: T(n)=aT(n/b)+O(f(n))
Case
f(n)=O()
T(n)=O()
nlogba
nlogba
nlogba
lognnlogba
Case
f(n)=O()
T(n)=O()
f(n)
Matroid
CLRS: p437
Pair (S,I) with:
S is a finite set
ABI,AI (heredity)
A,BI,|A|<|B|xBA,A{x}I(exchange)
(S,I) is a matroid. w is a weight function over S. Greedy algorithm choosing iteratively an element maximizing w:
Data structures
Heap
CLRS: p151
Sorted data structure (priority queue) Min-heap: parent is bigger than its children Max-heap: parent is bigger than its children
Speed
we do have 84 > 38,29 and 38 > 18,17
Operation
Complexity
Memory use
O(n)
Insert
O(logn)
Maximum
O(1)
Extract-Max
O(logn)
Increase-Key
O(logn)
Hash Table
CLRS: p256
Arbitrarily indexed table h is a hash function to [1..m]
Space: O(m)
Locate: O(1+n/m)
Note: Locate finds a location in the array where the user can read or write as usual.
Space: O(n)
Search: O(h)
Insert: O(h)
Delete: O(h)
Red-Black Tree
CLRS: p308
BST maintaining h=logn with:
red nodes cannot have a red parent there are no more red nodes than black ones;
all simple paths from the root to a leaf have the same number of black nodes, limiting h.
any simple path from the root to a leaf has 3 black nodes, there are no more red nodes than black ones. (source)
B-Tree
CLRS: p484
Variant of the BST using a large fan-out t to reduce disk-accesses Search, insert and delete takeO(\log_t n) disk accesses and O(t \log_t n) CPU
time.
the fan-out is usually around 1000, making two levels enough most of the time (source)
Union-find
CLRS: p568
Data structure for sets, supporting union of sets and finding the representative m is the number of previously run Make-Set operations. \alpha is
the Ackerman function (\alpha(m) \leq 4 in practise).
Make-Set: O(1)
Union: O(1)
Find-Set: O(\alpha(m))
http://scienceblogs.com/goodmath/2007/09/06/graph-searches-and-disjoint-se/
Sorting
Bubblesort
CLRS: p40 (problem 2-2) Complexity: O(n^2)
Fixing inverted adjacent values n times
the red squares highlight the currently checked pairs, all pairs have to be checked n times (source)
Insertion sort
CLRS: p16 (correctness), p24 (complexity) Complexity: O(n^2)
Merge sort
CLRS: p29 Complexity: O(n \log n)
Divide-and-conquer (sort each half, then merge)
Heapsort
CLRS: p159 Complexity: O(n \log n)
Building a heap on the n elements and extracting them all
Quicksort
CLRS: p170 Complexity: O(n \log n)
Divide-and-conquer (choose a pivot, then filter lower and greater values)
the elements are split in two sub-arrays using pivot as the limit value and each part is sorted
recursively (source)
Graph algorithms
{Breadth,Depth}-first search
CLRS: p594 (breadth), p603 (depth) Complexity: O(E + V)
Listing the vertices of a graph. Breadth-first search list the siblings, then the siblings' siblings
visit order of a breadth-first-search algorithm (source)
Kruskal
CLRS: p631 Complexity: O(E \log V)
Finding a minimum spanning tree Consider every edge in nondecreasing order, add to current forest if links two components. The components
are handled using union-find.
Prim
CLRS: p634 Complexity: O(E \log V) (binary heap) Complexity: O(E + V \log V) (Fibonacci heap)
Finding a minimum spanning tree Iteratively extract closest vertex u and relax all edges (u, v). The set of vertices is handled using a priority
queue.
Bellman-Ford
CLRS: p651 Complexity: O(E V)
Finding all shortest paths from a single source Just relax every edge of the graph |V| times Detects negative weight cycles
Finding all shortest paths from a single source in Directed Acyclic Graphs Consider every vertex uin topological order and relax all edges (u, v)
Dijkstra
CLRS: p658 Complexity: O(V^2 + E) (array) Complexity: O((E+V) \log V) (binary heap)Complexity: O(V \log V + E) (Fibonacci heap)
Finding all shortest paths from a single source with positive weights Iteratively extract closest vertex u and relax all edges (u, v). The set of
vertices is handled using a priority queue.
Floyd-Warshall
CLRS: p693 Complexity: O(V^3)
Finding the shortest path from all vertex pairs Relaxing pair distance |V| times