Algorithms¶
Introduction¶
The previous parts of this guide describe data structures: how to store items and the relations between them. This part is about algorithms: procedures that compute something from a structure, such as a sorted order, a shortest path or a posterior probability.
In sds the two are kept strictly apart. Structures live in
sds.linear, sds.tree, sds.graph, sds.advanced and
sds.probabilistic; algorithms live in sds.algorithms and receive
the structure they work on as a parameter.
graph LR
subgraph "Structures (store)"
L[sds.linear]
T[sds.tree]
G[sds.graph]
P[sds.probabilistic]
end
subgraph "sds.algorithms (compute)"
S[sorting]
TA[tree_algorithms]
GA[graph_algorithms]
PA[probabilistic_algorithms]
end
L --> S
T --> TA
G --> GA
P --> PA
style S fill:#3498db,color:#fff
style TA fill:#3498db,color:#fff
style GA fill:#3498db,color:#fff
style PA fill:#3498db,color:#fff
Note
Why separate them? A structure that also implemented every
algorithm on it would grow without bound, and each algorithm would be
tied to one representation. As free functions, an algorithm is written
once against an abstract interface (AbstractGraph,
AbstractBinaryTree, AbstractGraphicalModel) and runs unchanged on
every structure implementing it: dijkstra() works on both
WeightedGraph and WeightedDirectedGraph.
Measuring Algorithms¶
Algorithms are compared by how their cost grows with the size \(n\) of their input, ignoring constant factors. The three standard notations are:
\(O\) is an upper bound, \(\Omega\) a lower bound, \(\Theta\) a tight bound. For graphs, costs are expressed in the number of vertices \(V\) and edges \(E\); for trees, in the number of nodes \(n\) and the height \(h\).
Growth |
Name |
Example |
|---|---|---|
\(O(\log n)\) |
logarithmic |
search in a balanced tree |
\(O(n)\) |
linear |
tree traversal, BFS, DFS |
\(O(n \log n)\) |
linearithmic |
merge sort, Kruskal |
\(O(n^2)\) |
quadratic |
quicksort worst case |
\(O(d^w)\) |
exponential in a parameter |
variable elimination (\(w\) = induced width) |
Contents¶
Question |
Page |
|---|---|
How are algorithms designed and proved? |
|
How do I sort, and which sort should I choose? |
|
How do I walk or rebalance a binary tree? |
|
Shortest paths, spanning trees, reachability? |
|
Posterior probabilities, likelihoods, decoding? |
For signatures and parameters, see the Algorithms (sds.algorithms).