Summary: Principles of Algorithmics
Everything on one page, to review the chapter before moving on. Each section links to the lesson where it is explained.
Key ideas
- An algorithm solves a problem for all its instances, defined by a precondition and a postcondition. It must be finite, definite and effective (Algorithmic Thinking).
- We judge algorithms mainly by their efficiency in time and memory, and above all by how their cost grows with the size of the input (Measuring Efficiency).
- is obtained by counting elementary operations; when the cost depends on the data, we distinguish the best, worst and average cases (Complexity Analysis).
- Asymptotic notation keeps only the growth rate: is an upper bound, a lower bound and an exact order (Asymptotic Notation).
- Implementation, language and hardware change only the constant, never the complexity (principle of invariance) (Growth Rates).
- Data structures and algorithms are designed together: the same algorithm can be fast or slow depending on how the data is organized (Data Structures).
Complexity classes
From best to worst, with an example from the course (Growth Rates):
| Class | Name | Example |
|---|---|---|
| Constant | Accessing an element of an array | |
| Logarithmic | Binary search | |
| Linear | Sequential search, sum of a vector | |
| Linearithmic | Mergesort, Quicksort (average) | |
| Quadratic | Bubble, insertion and selection sort | |
| Cubic | Matrix multiplication by definition | |
| Exponential | Exhaustive search over subsets | |
| Factorial | Exhaustive search over permutations |
Polynomial classes are considered tractable; exponential ones are not. For exponential algorithms, faster hardware only adds a constant to the size that can be solved.
Complexity of loops
| Pattern | Cost (Complexity of Loops) |
|---|---|
| Additive step over a range proportional to | |
Multiplicative step (i *= 2, i /= 2) | , even if the range is or |
| Independent nested loops | Multiply the iterations |
| Dependent nested loops | Use a summation |
| Sequential blocks | Add; the largest term dominates |
| Loop up to | |
| Harmonic sum () | |
| Geometric sum () |
Useful formulas
- Predicting times with : .
- Predicting sizes: .
- Doubling test: if doubling multiplies the time by about 2, 4 or 8, the algorithm is linear, quadratic or cubic (Measuring Efficiency).
Searching
| Algorithm | Requires | Best | Worst and average |
|---|---|---|---|
| Sequential search | Any sequence | ||
| Binary search | Sorted data with direct access |
Sorting first pays off when the number of searches is large enough (Case Studies: Searching).
Saved only in your browser
Questions, or something wrong?
Errors, suggestions and public questions open a form on GitHub, visible to everyone.