Skip to main content

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).
  • T(n)T(n) 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: OO is an upper bound, Ω\Omega a lower bound and Θ\Theta 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):

ClassNameExample
O(1)O(1)ConstantAccessing an element of an array
O(log⁡n)O(\log n)LogarithmicBinary search
O(n)O(n)LinearSequential search, sum of a vector
O(nlog⁡n)O(n \log n)LinearithmicMergesort, Quicksort (average)
O(n2)O(n^2)QuadraticBubble, insertion and selection sort
O(n3)O(n^3)CubicMatrix multiplication by definition
O(2n)O(2^n)ExponentialExhaustive search over subsets
O(n!)O(n!)FactorialExhaustive 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​

PatternCost (Complexity of Loops)
Additive step over a range proportional to nnO(n)O(n)
Multiplicative step (i *= 2, i /= 2)O(log⁡n)O(\log n), even if the range is n2n^2 or n3n^3
Independent nested loopsMultiply the iterations
Dependent nested loopsUse a summation
Sequential blocksAdd; the largest term dominates
Loop up to n\sqrt{n}O(n)O(\sqrt{n})
Harmonic sum (n/1+n/2+⋯+n/nn/1 + n/2 + \dots + n/n)O(nlog⁡n)O(n \log n)
Geometric sum (n+n/2+n/4+…n + n/2 + n/4 + \dots)O(n)O(n)

Useful formulas​

  • Predicting times with T(n)∈O(f(n))T(n) \in O(f(n)): t2=f(n2)f(n1)⋅t1t_2 = \frac{f(n_2)}{f(n_1)} \cdot t_1.
  • Predicting sizes: n2=f−1(t2t1⋅f(n1))n_2 = f^{-1}\left(\frac{t_2}{t_1} \cdot f(n_1)\right).
  • Doubling test: if doubling nn multiplies the time by about 2, 4 or 8, the algorithm is linear, quadratic or cubic (Measuring Efficiency).

Searching​

AlgorithmRequiresBestWorst and average
Sequential searchAny sequenceO(1)O(1)O(n)O(n)
Binary searchSorted data with direct accessO(1)O(1)O(log⁡n)O(\log n)

Sorting first pays off when the number of searches is large enough (Case Studies: Searching).

Saved only in your browser
Was this lesson helpful?
Questions, or something wrong?

Errors, suggestions and public questions open a form on GitHub, visible to everyone.